From nobody Thu Nov 28 01:46:47 2024 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 5474813CABC; Sat, 5 Oct 2024 10:32:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124366; cv=none; b=bmIDGzFg0x/SELFKWZ7Zx9ECogbI6ZNjt3UyFOxN7Y//7PrK3BUIIl4mivecw8w5qRFA1e09lxb3j0A3Wy8IA6czP2U/3fvDdhFRMSD0pqO0jMr9//jlrHtPby2o55WvTc4Z6Ky9wXUQC9jssoDFsdYWim0YbpdhZBwOCe6yfC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124366; c=relaxed/simple; bh=rf+MEDxyq5lpSjEzC17VYDLpx49FdZdEIHA+inm7+3I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GEfHnawQ8bpW+SGdaAsG0NHUiAs59o95CrYshBCm4misVK121w8kMG1cnLEr/TuHI/o/ZjM92ds3os6WN+43+6Dkx5ouwzDXzIV1+nPZlVpyJX1zY8A14hfca3RnF5jNAXD3NjUghxlIeP9TBUnJ8Dl8wNZDe3SWWkpGE8GhUkI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fdNcZ9tl; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fdNcZ9tl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CFBEC4CECC; Sat, 5 Oct 2024 10:32:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124366; bh=rf+MEDxyq5lpSjEzC17VYDLpx49FdZdEIHA+inm7+3I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fdNcZ9tlGNhkuO5FXRMkZdrubzKst+VlgcAYKKWu/gOF77q6lgripJK2t/NWaKywk gZEauIlVabQp1g3+K/Vxt5yqc2XbKBjmzDAPGwtpPAvdtVDzeV80ltU8l7iQkeKXkw gtFnBzFPn63iByZw/tWQXYH3+3+on23cf6wq1s6HxALCBIIQppI3fhmqcI2+GAiTFW N6SUycq0quVRCf1E8xX1ufAU6sIH7ORpetglhCqjcL6F4UYmatFQjWUE/OZzSaXs2K qa15JXTM7bH+K9+Z0YgiqYHeUwuAiW4oaGc0uwIB8gCrGenC6lVdlWKZaimweeLx41 qvp2jxSdexOtg== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:28 +0100 Subject: [PATCH RFT v11 1/8] arm64/gcs: Return a success value from gcs_alloc_thread_stack() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-1-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=3830; i=broonie@kernel.org; h=from:subject:message-id; bh=rf+MEDxyq5lpSjEzC17VYDLpx49FdZdEIHA+inm7+3I=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARW7jkAXlWGa990NpPMJQi9DYqZqsGTxzykS7RGO QpQ8MtyJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVuwAKCRAk1otyXVSH0J7TB/ 40ecD3G67gCAjpR/gGTsXhHLA0alA2Hh4B8XtOuGXArMi1pLWe8HiYF8wGUHkgAoDMEabZJfuXZpDS du88xSgnlMaZSnsCGPDJVc6IwBqaJjitnzImoiUJdpbjtZZjsYTnQyalOaIKsPV99kRdAMjExm9Q1y HKDPZ1xBa+3S7dEu1/W6rB0Fr0hvuuMr2JBUloQxLqvDsfHUKi+r/W6Kn3eyuL/VVe0/EsKuJRBmNc smadltIUsePM+VqSKb+3L9cf+XZLAVmeGF3qqyv2tzz/TJJIofSYTcfy1xkSqgPmS/SIO2h9ga/7sn byvUEfZskrjPo9p/ZD9FdPZCcIeZIA X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Currently as a result of templating from x86 code gcs_alloc_thread_stack() returns a pointer as an unsigned int however on arm64 we don't actually use this pointer value as anything other than a pass/fail flag. Simplify the interface to just return an int with 0 on success and a negative error code on failure. Signed-off-by: Mark Brown Acked-by: Deepak Gupta --- arch/arm64/include/asm/gcs.h | 8 ++++---- arch/arm64/kernel/process.c | 8 ++++---- arch/arm64/mm/gcs.c | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index f50660603ecf5dc09a92740062df3a089b02b219..d8923b5f03b776252aca76ce316= ef57399d71fa9 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -64,8 +64,8 @@ static inline bool task_gcs_el0_enabled(struct task_struc= t *task) void gcs_set_el0_mode(struct task_struct *task); void gcs_free(struct task_struct *task); void gcs_preserve_current_state(void); -unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, - const struct kernel_clone_args *args); +int gcs_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args); =20 static inline int gcs_check_locked(struct task_struct *task, unsigned long new_val) @@ -91,8 +91,8 @@ static inline bool task_gcs_el0_enabled(struct task_struc= t *task) static inline void gcs_set_el0_mode(struct task_struct *task) { } static inline void gcs_free(struct task_struct *task) { } static inline void gcs_preserve_current_state(void) { } -static inline unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, - const struct kernel_clone_args *args) +static inline int gcs_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) { return -ENOTSUPP; } diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index fdd095480c3ffb8c13fd4e7c9abc79e88143e08b..8ebd11c29792524dfeeade9cc78= 26b007329aa6a 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -297,7 +297,7 @@ static void flush_gcs(void) static int copy_thread_gcs(struct task_struct *p, const struct kernel_clone_args *args) { - unsigned long gcs; + int ret; =20 if (!system_supports_gcs()) return 0; @@ -305,9 +305,9 @@ static int copy_thread_gcs(struct task_struct *p, p->thread.gcs_base =3D 0; p->thread.gcs_size =3D 0; =20 - gcs =3D gcs_alloc_thread_stack(p, args); - if (IS_ERR_VALUE(gcs)) - return PTR_ERR((void *)gcs); + ret =3D gcs_alloc_thread_stack(p, args); + if (ret !=3D 0) + return ret; =20 p->thread.gcs_el0_mode =3D current->thread.gcs_el0_mode; p->thread.gcs_el0_locked =3D current->thread.gcs_el0_locked; diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 5c46ec527b1cdaa8f52cff445d70ba0f8509d086..1f633a482558b59aac5427963d4= 2b37fce08c8a6 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -38,8 +38,8 @@ static unsigned long gcs_size(unsigned long size) return max(PAGE_SIZE, size); } =20 -unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, - const struct kernel_clone_args *args) +int gcs_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) { unsigned long addr, size; =20 @@ -59,13 +59,13 @@ unsigned long gcs_alloc_thread_stack(struct task_struct= *tsk, size =3D gcs_size(size); addr =3D alloc_gcs(0, size); if (IS_ERR_VALUE(addr)) - return addr; + return PTR_ERR((void *)addr); =20 tsk->thread.gcs_base =3D addr; tsk->thread.gcs_size =3D size; tsk->thread.gcspr_el0 =3D addr + size - sizeof(u64); =20 - return addr; + return 0; } =20 SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size= , unsigned int, flags) --=20 2.39.2 From nobody Thu Nov 28 01:46:47 2024 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 BE40913A250; Sat, 5 Oct 2024 10:32:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124372; cv=none; b=UZ82oGOGrSIu352RhBB8eSK6FaDUrD4FGbo3MxgT5AQ4HADuWZRrqdphrntiXx76yU3ULQc6i2P5wMRkjKu06mEr4rvPDhDhnwY9gFoJIc7HNoziBlgJyIiugLqj7mt4yV2868yFf3C7qdDSil8N/WHnxg3J12d+jHITvHgZHIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124372; c=relaxed/simple; bh=tec4v7YIW6lSCaG+Grv8LNbscxtN/Lm0HS7zRkM5EGk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uHMYvsG0Y0p3lsalP67YPQlycFSEBoUa2+3jsjQzaQ7s72TM6bNZWGSIPsDy5J8X45M54u4m8Qv8PMTmIEufZ93QdHFJ5p6ev3LGB2+1IFa531VcAUTDR1i6pDRLgLbMqXqZVWvx7Wad0UywuDIIToo4Ns5Y/xUU4fAbCj5UqK4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AxVRdiJH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AxVRdiJH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67A34C4CECF; Sat, 5 Oct 2024 10:32:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124372; bh=tec4v7YIW6lSCaG+Grv8LNbscxtN/Lm0HS7zRkM5EGk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=AxVRdiJHniLUMm9xJo6RstC2rz4Q7adj3zUc4DaLQuJJwft0JcshjJVatI5Wu756e 9uSdA+XwGZwdYnYHN+KxN0Q0iSdI8WfS1fRKPPuyxz3+kO4O2zR5TOTBha14/itWaa gskoLSZz01XK4DA5iN6cwdpwVv8XvtpJ/iZs+hEyzCdEk9eCjNeeWAMCny8I+3ZgGt st8az0NKEd+TpxcdCP6omqozrzk9QKyVTw2ga+zEIwi0X/j5WX4M8OXieDF5A6BsW1 vCIdXSZ7ba2G+r5Q6zZ46zYGk+94kVAA0RmbOSibw666z6eyakzWx1pVI+lxeT2D1D 6D8AlCbPHs/HA== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:29 +0100 Subject: [PATCH RFT v11 2/8] Documentation: userspace-api: Add shadow stack API documentation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-2-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=3137; i=broonie@kernel.org; h=from:subject:message-id; bh=tec4v7YIW6lSCaG+Grv8LNbscxtN/Lm0HS7zRkM5EGk=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARW8SkYUT0D/Hh1aLXz4STfUnladQ+MlUroGKZIR 78H1KVOJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVvAAKCRAk1otyXVSH0IfAB/ 9dmXLrxIwAFg60Ni/ida60svBBatUI0FLf34hoHpyMsqfiA2XNe2kLSAMJ5AR16AtHKT5m8zceCdOr WC0de3ohrka+wdCUPKPpL3SSsVeBeipUDAJIWnGO7wwF7UJxlUkZVsb4u0v0QLkYT+92XuMv9SXYFc CV3NWrqEpCZzddjqvGOOqNTlwtx6TjAUXs/q+W7DcJrobFKU0QBqnqsWpkXsHNUgrzAoHbqRHiEHBr nwRZTXlWldf0Cc8SmV3cDfjKnikzK7TbeQbyrdZu8Ty8qvt9QNs/tG9CV41zbetenWY5eqv30bWqN2 uqbvyZ2heC7V3q5GUYjN/LnObt7zJv X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB There are a number of architectures with shadow stack features which we are presenting to userspace with as consistent an API as we can (though there are some architecture specifics). Especially given that there are some important considerations for userspace code interacting directly with the feature let's provide some documentation covering the common aspects. Reviewed-by: Catalin Marinas Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/shadow_stack.rst | 41 ++++++++++++++++++++++++= ++++ 2 files changed, 42 insertions(+) diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspac= e-api/index.rst index 274cc7546efc2a042d2dc00aa67c71c52372179a..c39709bfba2c5682d0d1a22444d= b17c17bcf01ce 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -59,6 +59,7 @@ Everything else =20 ELF netlink/index + shadow_stack sysfs-platform_profile vduse futex2 diff --git a/Documentation/userspace-api/shadow_stack.rst b/Documentation/u= serspace-api/shadow_stack.rst new file mode 100644 index 0000000000000000000000000000000000000000..c576ad3d7ec12f0f75bffa4e2ba= fd0c9d7230c9f --- /dev/null +++ b/Documentation/userspace-api/shadow_stack.rst @@ -0,0 +1,41 @@ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Shadow Stacks +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Introduction +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Several architectures have features which provide backward edge +control flow protection through a hardware maintained stack, only +writeable by userspace through very limited operations. This feature +is referred to as shadow stacks on Linux, on x86 it is part of Intel +Control Enforcement Technology (CET), on arm64 it is Guarded Control +Stacks feature (FEAT_GCS) and for RISC-V it is the Zicfiss extension. +It is expected that this feature will normally be managed by the +system dynamic linker and libc in ways broadly transparent to +application code, this document covers interfaces and considerations. + + +Enabling +=3D=3D=3D=3D=3D=3D=3D=3D + +Shadow stacks default to disabled when a userspace process is +executed, they can be enabled for the current thread with a syscall: + + - For x86 the ARCH_SHSTK_ENABLE arch_prctl() + +It is expected that this will normally be done by the dynamic linker. +Any new threads created by a thread with shadow stacks enabled will +themselves have shadow stacks enabled. + + +Enablement considerations +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + +- Returning from the function that enables shadow stacks without first + disabling them will cause a shadow stack exception. This includes + any syscall wrapper or other library functions, the syscall will need + to be inlined. +- A lock feature allows userspace to prevent disabling of shadow stacks. +- Those that change the stack context like longjmp() or use of ucontext + changes on signal return will need support from libc. --=20 2.39.2 From nobody Thu Nov 28 01:46:47 2024 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 ED9D5148307; Sat, 5 Oct 2024 10:32:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124380; cv=none; b=HobEiV4Dg/qMpASaTGl/T3QZsyjNPKG80Pv/lPXzHOk8s9JUANKoobEZUFC6+JPGGa2/ItAM4M982dk2xKsa5xpuswANu6me6HvFfq1jbvGsjOCWyJYLLcblBM6yJQzlbHxvF0xD5JGfugVlfYd9+cK6TmPzhds/H+oziHX30+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124380; c=relaxed/simple; bh=5R2x8naFdQiQL8xwTf6W/tsDu9xiZhpQtf9OK3u1Lgo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EDoAfjDu/mImkEz1Qk8ltygfUaVFik5ZJtbg/C9awespvPjxQ3aNPGW9yDwlwPVZO8o0+ledYxCdFxetcE0kKwVaZVahoqiC5gdSFnERP4LuqxqLqSIHuoGrDSa7Cx08bRVM9EAsa7kqvQdkOOFWpu4pgHRx8hNJFlw6Hz87isk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=chTHwqWb; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="chTHwqWb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B36E8C4CED0; Sat, 5 Oct 2024 10:32:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124378; bh=5R2x8naFdQiQL8xwTf6W/tsDu9xiZhpQtf9OK3u1Lgo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=chTHwqWb619AnRNG396WYEeqvvdB9FEVZUbWNrlwaCyKungTW7oIKbpziDVU0VYYt yT49fSu69CMQ4f0iP2MwOqpaTP8dbpvvggZCjluuwgOrITBDxNq+JwdJpQOZLipHXE 4vxVK543p1qeqJwNFpK6nVcW2BsCr00WgJxNm7p0ztGSNChtaY5GNEPHqKjjOigqnk llgv8c57zwRpe4Zze8vSJBfV/mQQ+BPjsQue1ryUvTuV8B+fnSaGAhNBpHDrjihfWr kRAIAQZiJXx/eYP/8OZOoQGsbxmvKEsYqEaE/Hi/QiP3bK3Pdw/AY0tFnfSzpJY8/H VlRLgyxR99HFg== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:30 +0100 Subject: [PATCH RFT v11 3/8] selftests: Provide helper header for shadow stack testing Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-3-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=4486; i=broonie@kernel.org; h=from:subject:message-id; bh=5R2x8naFdQiQL8xwTf6W/tsDu9xiZhpQtf9OK3u1Lgo=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARW9l13Q/URJlro/qKJBEDZNVr3sOrKNsNDeoBFP 3BIn/5CJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVvQAKCRAk1otyXVSH0I39B/ 91+8sD63KbXfuIMCuGIU4QtYu+5RGzTPwzPsI0jmsQLaO2s2r2COTzi1yAK3L/ny4stkdQLwpYBfSV /AcLu2VEuCZ2uyjjaJ6bGK9l2Tma/C4DZ+GfioFbxmmP1X2KhiMfzJMYhk9YMPadmmoGUd/g7Pg89g +phIKXEH4DfGkHiDBlC6+kjZiiFzPbbpfSBsCTlRp0Obnt1AvhsvM/+fVp9CA4r3DxF74pQbeEKeAw cy5mdN0tco0qMcDQ/8VgF5HchWNgVdNrnagYBJ+uZtlSyM01I8b/70np1HdN61kzb33hcw8fr+r695 qs/lUuQet2p8HaFn1Neza2LvPAHsO8 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB While almost all users of shadow stacks should be relying on the dynamic linker and libc to enable the feature there are several low level test programs where it is useful to enable without any libc support, allowing testing without full system enablement. This low level testing is helpful during bringup of the support itself, and also in enabling coverage by automated testing without needing all system components in the target root filesystems to have enablement. Provide a header with helpers for this purpose, intended for use only by test programs directly exercising shadow stack interfaces. Reviewed-by: Rick Edgecombe Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/ksft_shstk.h | 98 ++++++++++++++++++++++++++++++++= ++++ 1 file changed, 98 insertions(+) diff --git a/tools/testing/selftests/ksft_shstk.h b/tools/testing/selftests= /ksft_shstk.h new file mode 100644 index 0000000000000000000000000000000000000000..869ecea2bf3ea3d30cead9819d2= b3a75f5397754 --- /dev/null +++ b/tools/testing/selftests/ksft_shstk.h @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Helpers for shadow stack enablement, this is intended to only be + * used by low level test programs directly exercising interfaces for + * working with shadow stacks. + * + * Copyright (C) 2024 ARM Ltd. + */ + +#ifndef __KSFT_SHSTK_H +#define __KSFT_SHSTK_H + +#include + +/* This is currently only defined for x86 */ +#ifndef SHADOW_STACK_SET_TOKEN +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) +#endif + +static bool shadow_stack_enabled; + +#ifdef __x86_64__ +#define ARCH_SHSTK_ENABLE 0x5001 +#define ARCH_SHSTK_SHSTK (1ULL << 0) + +#define ARCH_PRCTL(arg1, arg2) \ +({ \ + long _ret; \ + register long _num asm("eax") =3D __NR_arch_prctl; \ + register long _arg1 asm("rdi") =3D (long)(arg1); \ + register long _arg2 asm("rsi") =3D (long)(arg2); \ + \ + asm volatile ( \ + "syscall\n" \ + : "=3Da"(_ret) \ + : "r"(_arg1), "r"(_arg2), \ + "0"(_num) \ + : "rcx", "r11", "memory", "cc" \ + ); \ + _ret; \ +}) + +#define ENABLE_SHADOW_STACK +static inline __attribute__((always_inline)) void enable_shadow_stack(void) +{ + int ret =3D ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK); + if (ret =3D=3D 0) + shadow_stack_enabled =3D true; +} + +#endif + +#ifdef __aarch64__ +#define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) + +#define my_syscall2(num, arg1, arg2) = \ +({ = \ + register long _num __asm__ ("x8") =3D (num); \ + register long _arg1 __asm__ ("x0") =3D (long)(arg1); \ + register long _arg2 __asm__ ("x1") =3D (long)(arg2); \ + register long _arg3 __asm__ ("x2") =3D 0; \ + register long _arg4 __asm__ ("x3") =3D 0; \ + register long _arg5 __asm__ ("x4") =3D 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=3Dr"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define ENABLE_SHADOW_STACK +static inline __attribute__((always_inline)) void enable_shadow_stack(voi= d) +{ + int ret; + + ret =3D my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + PR_SHADOW_STACK_ENABLE); + if (ret =3D=3D 0) + shadow_stack_enabled =3D true; +} + +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#ifndef ENABLE_SHADOW_STACK +static inline void enable_shadow_stack(void) { } +#endif + +#endif --=20 2.39.2 From nobody Thu Nov 28 01:46:47 2024 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 EDD0C146593; Sat, 5 Oct 2024 10:33:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124385; cv=none; b=df2gzH2gwKVNIVYGOBRYb130P0qtvehEndbQ0VU89wj8wXWReDSiAKj0enKiyAVTwzfNFmG3c6uxRRIMc3ayJR687fb5KECmNWGb466CdUHh1N2oGUWzaK70R2gFjbs7Esyv2cAbhsVVarfK/1gDx5jYBK90hsKVX0UR+SxTPBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124385; c=relaxed/simple; bh=XmYalT4R87/DWvbsQyVmrZIIZ6DF/I/kJjYXRf7eYV4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZR4Yl86Z/FIy4StLqLj12Eba/uycAVMSEO1l2Kq9XSAHFAE3o4X6ubj8TXWLtF6szi1rS+tCQeIeq4sNsPG/tXwG6siOFeAcN8f8DBaoC8E3kVDy7+t4GHmpG63VEj2A8f1eAnkJY2Jt5+OkusoU22rjBdGHGYVg8JR5fAOLnH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sm/Rvbe1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sm/Rvbe1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04286C4CECE; Sat, 5 Oct 2024 10:32:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124384; bh=XmYalT4R87/DWvbsQyVmrZIIZ6DF/I/kJjYXRf7eYV4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=sm/Rvbe1pST5zIrFGFT6LztVHbDplwkC+BPnaz+nnIlwS99Kd9ZOH3owW/lKPX6CG 76uRdZmHbGPimGqVs+T5A9UZx9iGDiS4LwuGlZ+isPPtqG3TB38X+jKCW3vy8qp8Gy 9lw7/4fyys9zyB0oxWOfelyockW4vrnDdijFJ+CE+2figYc/sj1ucITwozFIMSM7ME g/2WNwKslFZ9CY2UOSZr7WgyT0X31QXfrje5bsU0Kwvm2c44Y2OAIBkG0dBejgiQCr 2FR4XGXMrYy/zzAfBz68aBTda6CdpN0hQzeYTUCMp0FdkmVqSryD4jByhlyh1C/msK AJ4jOck16/kAg== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:31 +0100 Subject: [PATCH RFT v11 4/8] fork: Add shadow stack support to clone3() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-4-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=16778; i=broonie@kernel.org; h=from:subject:message-id; bh=XmYalT4R87/DWvbsQyVmrZIIZ6DF/I/kJjYXRf7eYV4=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARW9Y0wULZzHV4wfeoNwJ39MtBjuR9KjXesQj9jc p1HExyqJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVvQAKCRAk1otyXVSH0KGIB/ 0VW3X9RlDI0PyxfkxR2vGXIBcmaHQ2zapo9BH5iQMK5Qauq//Xf86I+uHbEDuBVszKTYTcnUhOi8VW oifQaRiNvOyCCTiWhJZa0/hP/JOiTaVdfAXrL1dkv3JVtBvT0YDptVz+F30cPLX0+m6FNorK1wQbx8 am686zeXXfMomsZxJt9+trdTdQ5kYWAfggb9GHHUwjKLKBKrMcPYW1ymcjvnTcQstzVOlHOJ69csgm FQ6JfEBtnco6f+h86OH8x1EIR1kqoisOmpSSRX3VzYAYH68INNsB1B2eL0qtevd+yEn3ZTYgw6odBl Tp6BiY6y99Jq7ZteqYW9PGWrr1OJwq 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 a paramter to clone3() specifying the shadow stack pointer to use for the new thread, this is inconsistent with the way we specify the normal stack but during review concerns were expressed about having to identify where the shadow stack pointer should be placed especially in cases where the shadow stack has been previously active. If no shadow stack is specified then the existing implicit allocation behaviour is maintained. If a shadow stack pointer is specified then it is required to have an architecture defined token placed on the stack, this will be consumed by the new task. If no valid token is present then this will be reported with -EINVAL. This token prevents new threads being created pointing at the shadow stack of an existing running thread. If the architecture does not support shadow stacks the shadow stack pointer must be not be specified, architectures that do support the feature are expected to enforce the same requirement on individual systems that lack shadow stack support. Update the existing arm64 and x86 implementations to pay attention to the newly added arguments, in order to maintain compatibility we use the existing behaviour if no shadow stack is specified. Since we are now using more fields from the kernel_clone_args we pass that into the shadow stack code rather than individual fields. Portions of the x86 architecture code were written by Rick Edgecombe. Signed-off-by: Mark Brown --- arch/arm64/mm/gcs.c | 54 +++++++++++++++++++++- arch/x86/include/asm/shstk.h | 11 +++-- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 57 +++++++++++++++++++++--- include/asm-generic/cacheflush.h | 11 +++++ include/linux/sched/task.h | 17 +++++++ include/uapi/linux/sched.h | 10 +++-- kernel/fork.c | 96 +++++++++++++++++++++++++++++++++++-= ---- 8 files changed, 232 insertions(+), 26 deletions(-) diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 1f633a482558b59aac5427963d42b37fce08c8a6..c4e93b7ce05c5dfa1128923ad58= 7f9b5a7fb0051 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -43,8 +43,24 @@ int gcs_alloc_thread_stack(struct task_struct *tsk, { unsigned long addr, size; =20 - if (!system_supports_gcs()) + if (!system_supports_gcs()) { + if (args->shadow_stack_pointer) + return -EINVAL; + + return 0; + } + + /* + * If the user specified a GCS then use it, otherwise fall + * back to a default allocation strategy. Validation is done + * in arch_shstk_validate_clone(). + */ + if (args->shadow_stack_pointer) { + tsk->thread.gcs_base =3D 0; + tsk->thread.gcs_size =3D 0; + tsk->thread.gcspr_el0 =3D args->shadow_stack_pointer; return 0; + } =20 if (!task_gcs_el0_enabled(tsk)) return 0; @@ -68,6 +84,42 @@ int gcs_alloc_thread_stack(struct task_struct *tsk, return 0; } =20 +static bool gcs_consume_token(struct vm_area_struct *vma, struct page *pag= e, + unsigned long user_addr) +{ + u64 expected =3D GCS_CAP(user_addr); + u64 *token =3D page_address(page) + offset_in_page(user_addr); + + if (!cmpxchg_to_user_page(vma, page, user_addr, token, expected, 0)) + return false; + set_page_dirty_lock(page); + + return true; +} + +int arch_shstk_validate_clone(struct task_struct *tsk, + struct vm_area_struct *vma, + struct page *page, + struct kernel_clone_args *args) +{ + unsigned long gcspr_el0; + int ret =3D 0; + + /* Ensure that a token written as a result of a pivot is visible */ + gcsb_dsync(); + + gcspr_el0 =3D args->shadow_stack_pointer; + if (!gcs_consume_token(vma, page, gcspr_el0)) + return -EINVAL; + + tsk->thread.gcspr_el0 =3D gcspr_el0 + sizeof(u64); + + /* Ensure that our token consumption visible */ + gcsb_dsync(); + + return ret; +} + SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size= , unsigned int, flags) { unsigned long alloc_size; diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index 4cb77e004615dff003426a2eb594460ca1015f4e..252feeda69991e939942c74556e= 23e27c835e766 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -6,6 +6,7 @@ #include =20 struct task_struct; +struct kernel_clone_args; struct ksignal; =20 #ifdef CONFIG_X86_USER_SHADOW_STACK @@ -16,8 +17,8 @@ struct thread_shstk { =20 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 lon= g 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); @@ -28,8 +29,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 f63f8fd00a91f3d1171f307b92179556ba2d716d..59456ab8d93faee29c3b223b64e= b41659df76032 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 ker= nel_clone_args *args) * is disabled, new_ssp will remain 0, and fpu_clone() will know not to * update it. */ - new_ssp =3D shstk_alloc_thread_stack(p, clone_flags, args->stack_size); + new_ssp =3D shstk_alloc_thread_stack(p, args); if (IS_ERR_VALUE(new_ssp)) return PTR_ERR((void *)new_ssp); =20 diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 059685612362d7b1865eabf400888fbfa0659c1e..056e2c9ec30531d0901297da07f= 1842b47d2fcd5 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -191,18 +191,65 @@ void reset_thread_features(void) current->thread.features_locked =3D 0; } =20 -unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned l= ong clone_flags, - unsigned long stack_size) +int arch_shstk_validate_clone(struct task_struct *t, + struct vm_area_struct *vma, + struct page *page, + struct kernel_clone_args *args) +{ + /* + * SSP is aligned, so reserved bits and mode bit are a zero, just mark + * the token 64-bit. + */ + void *maddr =3D kmap_local_page(page); + int offset; + unsigned long addr, ssp; + u64 expected; + + if (!features_enabled(ARCH_SHSTK_SHSTK)) + return 0; + + ssp =3D args->shadow_stack_pointer; + addr =3D ssp - SS_FRAME_SIZE; + expected =3D ssp | BIT(0); + offset =3D offset_in_page(addr); + + if (!cmpxchg_to_user_page(vma, page, addr, (unsigned long *)(maddr + offs= et), + expected, 0)) + return -EINVAL; + set_page_dirty_lock(page); + + return 0; +} + +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) { struct thread_shstk *shstk =3D &tsk->thread.shstk; + unsigned long clone_flags =3D args->flags; unsigned long addr, size; =20 /* * 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 + * explicitly specify one. */ - if (!features_enabled(ARCH_SHSTK_SHSTK)) + if (!features_enabled(ARCH_SHSTK_SHSTK)) { + if (args->shadow_stack_pointer) + return (unsigned long)ERR_PTR(-EINVAL); + return 0; + } + + /* + * If the user specified a shadow stack then use it, otherwise + * fall back to a default allocation strategy. Validation is + * done in arch_shstk_validate_clone(). + */ + if (args->shadow_stack_pointer) { + shstk->base =3D 0; + shstk->size =3D 0; + return args->shadow_stack_pointer; + } =20 /* * For CLONE_VFORK the child will share the parents shadow stack. @@ -222,7 +269,7 @@ unsigned long shstk_alloc_thread_stack(struct task_stru= ct *tsk, unsigned long cl if (!(clone_flags & CLONE_VM)) return 0; =20 - size =3D adjust_shstk_size(stack_size); + size =3D adjust_shstk_size(args->stack_size); addr =3D alloc_shstk(0, size, 0, false); if (IS_ERR_VALUE(addr)) return addr; diff --git a/include/asm-generic/cacheflush.h b/include/asm-generic/cachefl= ush.h index 7ee8a179d1036e1d8010b8b18a8f3022e41c1695..96cc0c7a5c90fd7e899d0c5fe7c= 706302265efcf 100644 --- a/include/asm-generic/cacheflush.h +++ b/include/asm-generic/cacheflush.h @@ -124,4 +124,15 @@ static inline void flush_cache_vunmap(unsigned long st= art, unsigned long end) } while (0) #endif =20 +#ifndef cmpxchg_to_user_page +#define cmpxchg_to_user_page(vma, page, vaddr, ptr, old, new) \ +({ \ + bool ret; \ + \ + ret =3D try_cmpxchg(ptr, &old, new); \ + flush_icache_user_page(vma, page, vaddr, sizeof(*ptr)); \ + ret; \ +}) +#endif + #endif /* _ASM_GENERIC_CACHEFLUSH_H */ diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index 0f2aeb37bbb047335a399326b31bc8df81b75a3a..cd36389619d5c97401f7b90e177= c6027c232783b 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -16,6 +16,7 @@ struct task_struct; struct rusage; union thread_union; struct css_set; +struct vm_area_struct; =20 /* All the bits taken by the old clone syscall. */ #define CLONE_LEGACY_FLAGS 0xffffffffULL @@ -43,6 +44,7 @@ struct kernel_clone_args { void *fn_arg; struct cgroup *cgrp; struct css_set *cset; + unsigned long shadow_stack_pointer; }; =20 /* @@ -236,4 +238,19 @@ static inline void task_unlock(struct task_struct *p) =20 DEFINE_GUARD(task_lock, struct task_struct *, task_lock(_T), task_unlock(_= T)) =20 +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK +int arch_shstk_validate_clone(struct task_struct *p, + struct vm_area_struct *vma, + struct page *page, + struct kernel_clone_args *args); +#else +static inline int arch_shstk_validate_clone(struct task_struct *p, + struct vm_area_struct *vma, + struct page *page, + struct kernel_clone_args *args) +{ + return 0; +} +#endif + #endif /* _LINUX_SCHED_TASK_H */ diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h index 359a14cc76a4038aeacef14b2915d5ce60d0cf44..586a1c05a4e4ca05584d4d50022= 3bcf6c3add54c 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_pointer: Value to use for shadow stack pointer in 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_pointer; }; #endif =20 -#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_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 */ =20 /* * Scheduling policies diff --git a/kernel/fork.c b/kernel/fork.c index 60c0b4868fd4993920f7a615a47f7e915b9a47b5..d925e0c52a5ac4394abd1d46a5e= b14386d7ba2ca 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2109,6 +2109,51 @@ static void rv_task_fork(struct task_struct *p) #define rv_task_fork(p) do {} while (0) #endif =20 +static int shstk_validate_clone(struct task_struct *p, + struct kernel_clone_args *args) +{ + struct mm_struct *mm; + struct vm_area_struct *vma; + struct page *page; + unsigned long addr; + int ret; + + if (!IS_ENABLED(CONFIG_ARCH_HAS_USER_SHADOW_STACK)) + return 0; + + if (!args->shadow_stack_pointer) + return 0; + + mm =3D get_task_mm(p); + if (!mm) + return -EFAULT; + + mmap_read_lock(mm); + + addr =3D untagged_addr_remote(mm, args->shadow_stack_pointer); + page =3D get_user_page_vma_remote(mm, addr, FOLL_FORCE | FOLL_WRITE, + &vma); + if (IS_ERR(page)) { + ret =3D -EFAULT; + goto out; + } + + if (!(vma->vm_flags & VM_SHADOW_STACK) || + !(vma->vm_flags & VM_WRITE)) { + ret =3D -EFAULT; + goto out_page; + } + + ret =3D arch_shstk_validate_clone(p, vma, page, args); + +out_page: + put_page(page); +out: + mmap_read_unlock(mm); + mmput(mm); + return ret; +} + /* * This creates a new process as a copy of the old one, * but does not actually start it yet. @@ -2382,6 +2427,9 @@ __latent_entropy struct task_struct *copy_process( if (retval) goto bad_fork_cleanup_namespaces; retval =3D copy_thread(p, args); + if (retval) + goto bad_fork_cleanup_io; + retval =3D shstk_validate_clone(p, args); if (retval) goto bad_fork_cleanup_io; =20 @@ -2945,7 +2993,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) !=3D CLONE_ARGS_SIZE_VER2); - BUILD_BUG_ON(sizeof(struct clone_args) !=3D CLONE_ARGS_SIZE_VER2); + BUILD_BUG_ON(offsetofend(struct clone_args, shadow_stack_pointer) !=3D + CLONE_ARGS_SIZE_VER3); + BUILD_BUG_ON(sizeof(struct clone_args) !=3D CLONE_ARGS_SIZE_VER3); =20 if (unlikely(usize > PAGE_SIZE)) return -E2BIG; @@ -2978,16 +3028,17 @@ noinline static int copy_clone_args_from_user(struc= t kernel_clone_args *kargs, return -EINVAL; =20 *kargs =3D (struct kernel_clone_args){ - .flags =3D args.flags, - .pidfd =3D u64_to_user_ptr(args.pidfd), - .child_tid =3D u64_to_user_ptr(args.child_tid), - .parent_tid =3D u64_to_user_ptr(args.parent_tid), - .exit_signal =3D args.exit_signal, - .stack =3D args.stack, - .stack_size =3D args.stack_size, - .tls =3D args.tls, - .set_tid_size =3D args.set_tid_size, - .cgroup =3D args.cgroup, + .flags =3D args.flags, + .pidfd =3D u64_to_user_ptr(args.pidfd), + .child_tid =3D u64_to_user_ptr(args.child_tid), + .parent_tid =3D u64_to_user_ptr(args.parent_tid), + .exit_signal =3D args.exit_signal, + .stack =3D args.stack, + .stack_size =3D args.stack_size, + .tls =3D args.tls, + .set_tid_size =3D args.set_tid_size, + .cgroup =3D args.cgroup, + .shadow_stack_pointer =3D args.shadow_stack_pointer, }; =20 if (args.set_tid && @@ -3028,6 +3079,27 @@ static inline bool clone3_stack_valid(struct kernel_= clone_args *kargs) return true; } =20 +/** + * 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 *kar= gs) +{ + if (!kargs->shadow_stack_pointer) + return true; + + if (!IS_ALIGNED(kargs->shadow_stack_pointer, sizeof(void *))) + return false; + + /* + * The architecture must check support on the specific + * machine. + */ + return IS_ENABLED(CONFIG_ARCH_HAS_USER_SHADOW_STACK); +} + static bool clone3_args_valid(struct kernel_clone_args *kargs) { /* Verify that no unknown flags are passed along. */ @@ -3050,7 +3122,7 @@ static bool clone3_args_valid(struct kernel_clone_arg= s *kargs) kargs->exit_signal) return false; =20 - if (!clone3_stack_valid(kargs)) + if (!clone3_stack_valid(kargs) || !clone3_shadow_stack_valid(kargs)) return false; =20 return true; --=20 2.39.2 From nobody Thu Nov 28 01:46:47 2024 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 9479013BC11; Sat, 5 Oct 2024 10:33:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124392; cv=none; b=oz/ygrcLn+e9nv702W7YB9M8FIb14Mn5pnsq7eLZq4jlWnOdLTymtatxz2UEUY54F2zfIkVTy/6S/eIVKXu6mrIDltm58PkXM+Qml0cejD8VRg54DNNt8I6VEZ+KC94FwfX1Cjq5wKNmVtHHJt1W8f0MbqMWqv5zJ3Gy3YV5k7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124392; c=relaxed/simple; bh=429Rfem38BUZ9Ha+P7tmsQvwi4MZGvihbGBHtztpEwE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=j5yeELjkTWyyibP+onJ0CFioHkuxbOaozEHXWzL3v/bJ9qumFIvi6FfM7jiyOH1cf8bJd9xmTE5oaXCtqqwF5+QkGVaQE0AU5yMG3QcziQiFK8wC6afM1oAXtJCctV9/a+tX/X4vb1Rxl0FjWNMGgwfm/6QXjhwzQhhy691qCZs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GyH7ieI9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GyH7ieI9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D67AC4CECC; Sat, 5 Oct 2024 10:33:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124392; bh=429Rfem38BUZ9Ha+P7tmsQvwi4MZGvihbGBHtztpEwE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GyH7ieI9Jb8nOzKWNnRdtxc9ypNYVPYtPGOR4L0pGp+1DbjymDxzqO9CceAxdhKOS YAI2AgK1cdXkUi3f9YQzsEDg8txMPhFLBTanTYpf6bXDbP+GyLDPFuMy2Q3oDv48f5 8mRn5sJuPc05/QKCgaI1uEcu30OWcmJsxOWuI+MbXSuAMMU4k6JAmimhJhO2cQhujb evVfUio08stPuqTfGxnBXhHOPixCJJK0aKe1I9NPZ3seDlMik1n3fSG/zFO6sVI/sl xRbTXwwJadJLUha+piV8kGKXwkqXbVAKVzopGjDBBfxt1qoVTxvJHhTye53Ya1V6i7 dtNn4dSYbW/qw== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:32 +0100 Subject: [PATCH RFT v11 5/8] selftests/clone3: Remove redundant flushes of output streams Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-5-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=1114; i=broonie@kernel.org; h=from:subject:message-id; bh=429Rfem38BUZ9Ha+P7tmsQvwi4MZGvihbGBHtztpEwE=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARW+9td3kWcf+jMf9S+ta1Rptcq4kXdmGzx9uzgd DSbMsJWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVvgAKCRAk1otyXVSH0E7SB/ 9TYgkUROHh5SzmPnIP6PJ0VCqOATolw6jRR+E4HG5NjNtUg4gXy+hNWTEhQ24FqEm2DLHpyFDLK/rT ffryWZyS1iVfSniHMhKanbuPRmGdO+z1xoISwEKQUvJWcB68aHD9z1sLX5N12pisGr2KZqE/7pHF+F 0g5/q8IrB6gVBBQfaHh+Tg0sAnhBM4LbMP9a8RIYCGkxgiYBL6hHcNpB2wXs3S4kmTHjDOH8j0o6rg YJMUlRCfsdj69anI/ckbB7SFEXQ/It3PG3Cw/hQ0b3Q/3O1Ni0QbRFMCyIxtYRnbsymm/kBGDpYsJi Z8lMHPd2SeMIjG8KO4glBFOFnPhYjl X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Since there were widespread issues with output not being flushed the kselftest framework was modified to explicitly set the output streams unbuffered in commit 58e2847ad2e6 ("selftests: line buffer test program's stdout") so there is no need to explicitly flush in the clone3 tests. Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3_selftests.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/testing/selftests/clone3/clone3_selftests.h b/tools/test= ing/selftests/clone3/clone3_selftests.h index 3d2663fe50ba56f011629e4f2eb68a72bcceb087..39b5dcba663c30b9fc2542d9a0d= 2686105ce5761 100644 --- a/tools/testing/selftests/clone3/clone3_selftests.h +++ b/tools/testing/selftests/clone3/clone3_selftests.h @@ -35,8 +35,6 @@ struct __clone_args { =20 static pid_t sys_clone3(struct __clone_args *args, size_t size) { - fflush(stdout); - fflush(stderr); return syscall(__NR_clone3, args, size); } =20 --=20 2.39.2 From nobody Thu Nov 28 01:46:47 2024 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 299141494BB; Sat, 5 Oct 2024 10:33:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124401; cv=none; b=NoPW5YqyXY5WBXx4lspeWcIb67Pnh8ohY9g0+ZfAUXEJytEh5PqIFlw1Xn8RmIuHvbqwSgIVLVnFkFYhEdtvIQaoNZW3uryPjLciLk5eLShwtPiJIgqoo4NOclgIA+EOfavDZhyoO6G2a9svr7UqpVwNY5V9fVstaSjeql2nZ3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124401; c=relaxed/simple; bh=LVU4JnnFHFfKZn1aEt9WAigE6w0bsSGfbUSfCpk5ySk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HYqO/NP9kH+VNLsmhNJEbJDtvbp6XKuO5BkAo3/O0xbO6ECcgvz1YH72w/1afvBtPdVknyQgeildvJwpltEWWsQOTC0l7rpdlmO1svpW23kOusTELqUDwjW7yfMPEPcHH8SbhPWmM2WqLabh5wgYTU1ZVORz0QMhE+KszgBsgpM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CZZU0CaJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CZZU0CaJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4324C4CED0; Sat, 5 Oct 2024 10:33:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124400; bh=LVU4JnnFHFfKZn1aEt9WAigE6w0bsSGfbUSfCpk5ySk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CZZU0CaJP2ew4t7MuxgIQKW6bSouDYCLI3l8B1pYyE9WtJlKiZHRpUCukfKduH1QG k0ZuOxJGM//cpOFF1DchQwDGtPnEYqEzsnBTHJePM8tAfWtwnAHPViFZw9uiySeqT1 /8mB0erbeX9L2iNsas+rjdMJdiKlp76ajtcZIqKxJRqunRZPqMH+lx7z5e8qOJmm8c yAGcVWA2M8SpQ/XGn2o+IaYvegUoFqC2DcDNCsoTt8n750XV4oqFV+QjFJ2ecwWyuG dQNlvaesfg47RYI+rMn+JFEjH887ww6auzTjpIg4nSA9rY2WuswjCIw/V5ogW5Tz5Z ZCui7XedNRjRw== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:33 +0100 Subject: [PATCH RFT v11 6/8] selftests/clone3: Factor more of main loop into test_clone3() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-6-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=3931; i=broonie@kernel.org; h=from:subject:message-id; bh=LVU4JnnFHFfKZn1aEt9WAigE6w0bsSGfbUSfCpk5ySk=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARW/VhhjUlbX0fxRUJgMvr+uR0UKHQSJCU74PU08 zIB0tWSJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVvwAKCRAk1otyXVSH0Hw/B/ 9M9BeGoI+88rQnmdjz/ISliuB6oBHtfxhf5WK5KN75ApcnsrpA5XilHXkIBjRMMAe+1fbd7BKa5IQR 5t8AanP1Oqjp51i1OLO1DmLfun7keM3MWqZF/UZydMyt9rAh1mEVcMBQ98XrTWSHf5FDHs+PeBtQui UR8ISvw7EK9Q7cc7Bu3cnOLPY4cgNrW6oYq4UHJWAuUyttCpd+ThHhBkTuo6uFB2KsGc4Z1HYG2Iu/ whz1p4rSF897DbvbgSNmXfXsIjcnGiomHf5UgHgrJHQDDYf0syvUCKUvRY7QyiipGqr3lWq7nf3zkK 8s43dfIjjI6zTsi8hpCXn4CWujFb3F 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. Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan 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/selfte= sts/clone3/clone3.c index e61f07973ce5e27aff30047b35e03b1b51875c15..e066b201fa64eb17c55939b7cec= 18ac5d109613b 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, }; =20 +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_mo= de) { struct __clone_args args =3D { @@ -109,30 +122,40 @@ static int call_clone3(uint64_t flags, size_t size, e= num test_mode test_mode) return 0; } =20 -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; =20 + if (test->filter && test->filter()) { + ksft_test_result_skip("%s\n", test->name); + return; + } + + if (test->size_function) + size =3D test->size_function(); + else + size =3D 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 =3D call_clone3(flags, size, test_mode); + getpid(), test->flags, size); + ret =3D 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 !=3D expected) { + getpid(), ret, test->expected); + if (ret !=3D 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; } =20 - return true; + ksft_test_result_pass("%s\n", test->name); } =20 -typedef bool (*filter_function)(void); -typedef size_t (*size_function)(void); - static bool not_root(void) { if (getuid() !=3D 0) { @@ -160,16 +183,6 @@ static size_t page_size_plus_8(void) return getpagesize() + 8; } =20 -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[] =3D { { .name =3D "simple clone3()", @@ -319,24 +332,8 @@ int main(int argc, char *argv[]) ksft_set_plan(ARRAY_SIZE(tests)); test_clone3_supported(); =20 - for (i =3D 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 =3D tests[i].size_function(); - else - size =3D 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 =3D 0; i < ARRAY_SIZE(tests); i++) + test_clone3(&tests[i]); =20 ksft_finished(); } --=20 2.39.2 From nobody Thu Nov 28 01:46:47 2024 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 D6A991494BB; Sat, 5 Oct 2024 10:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124407; cv=none; b=Q7aFmsHp9Q+HrGYbJYHyj7eW0TqhFFC0rytHDUSPKRzjX7LRED1lzFKH6wOrMMLGUn5/J07BXuUW2G5wBNqRuJ8SOkuGmSV4fTcr7Om2e3onOV7Lxk/PISw+htfz33jFl6Q/zRC8n1QmSPl6EG2KvzVrhZCIADtLUVaorskb6tA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124407; c=relaxed/simple; bh=ewkcoFxIGUXL89FiyDyA2YmTyVasTMHDLRraAejpYow=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KANxyW9LzhUAncpcLJwLdp6sMQdV+0fAhM5n/V4J7DzMpUDYOasDCu+jEaqzvyelAqinxfRRVbArCft97NDcPiv8GBOlLNO3HFA2tLV6KoeN8sey5DSreJNm5VS5EkEtSb03NMv2Wwslt5Vk7SWfBAxtobiAp+YcVpeSAvb8A8o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iq2MPxUV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iq2MPxUV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57F06C4CEC2; Sat, 5 Oct 2024 10:33:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124407; bh=ewkcoFxIGUXL89FiyDyA2YmTyVasTMHDLRraAejpYow=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iq2MPxUVuM2WADq8mS7X0xIS2dpHTCo3n9bHt7wbUwro62Idzj4apa4aVk1q13jxB ZooXYJ8ujykjeV7hp0xrGRZZPr3VPAwbtSfw8wwRUT6vUfcJeMjddUkOu7rOD/TN7w ZP1TUjUrPXz9CvabP/XcB16xz3CA2qPhtZqq5MT5Qg0KVlw3lrGx2VPIfMEg8IJbRC X3wACE3fNJYz3DpJwjvQp6Y1q6gCgoSlYqKWoMbKSKldsm+RkUMouiebFS5530kl/y J6Gnfd8ViaQoAW+xY419hkvEeH91sa/Qwbd5GTsfqZ7eG79EsUKRuvbsMrqcFHUDrq hP7Rs/3pgj1Zw== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:34 +0100 Subject: [PATCH RFT v11 7/8] selftests/clone3: Allow tests to flag if -E2BIG is a valid error code Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-7-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=2075; i=broonie@kernel.org; h=from:subject:message-id; bh=ewkcoFxIGUXL89FiyDyA2YmTyVasTMHDLRraAejpYow=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARXAze7RkiwBwKf6+LuNfjV37wR/IjY8FreITLGw XNE7/YyJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVwAAKCRAk1otyXVSH0CPzB/ 9KoO7+Vb5jk+6ghKycsRFQy9Nvpcuk8pFGgkWEBtGE4txvdiDpjZnk5HpNjnaMgMcjg5lmgMPeovRM kEStv411wfmOPC5lC5/21deqkmsAQdcN0/zFfzG90jWJwev1xtEn/VS7UYaG+HrIXP3yT9B7WZkbzj zvXnsH5RPoudmBUmnT3dJLkh1NHRveU+tcpQJ6tN2aiIYORyTiTYVVa+PGzyTf/jm9wKCRagFpiDQv FAsfTzL1I0HL83UivqYcGYm73CJJ08DInSkWuH20MitXpLE9Kjlee3CcMu5KKeaBezmfGFae9MvT9U LdnFq0W+Qzd3L75xgpsqJP2aiJZr6U 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. Reviewed-by: Kees Cook Tested-by: Kees Cook Acked-by: Shuah Khan 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/selfte= sts/clone3/clone3.c index e066b201fa64eb17c55939b7cec18ac5d109613b..5b8b7d640e70132242fc6939450= 669acd0c534f9 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; }; @@ -146,6 +147,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 !=3D test->expected) { + if (test->e2big_valid && ret =3D=3D -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); --=20 2.39.2 From nobody Thu Nov 28 01:46:47 2024 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 225DD149C7A; Sat, 5 Oct 2024 10:33:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124414; cv=none; b=XxRo7u0AspQedOXbIcw5EQz5OavpE77PwxLyY51Zz2PvusrOAr5AEdoZD/T8agi8pXYWDj1pLhu2MND48sme41GGojyC5MexHR0ZniJ0WbAaQe0LX9vkXZIE0DGOayUigujk6qF8fD4/Id0McA3vXOWIxmqjoXXZQsO2Vrp1tVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728124414; c=relaxed/simple; bh=Asc7C0Q7KkIXRwzVZKZVujDysoWfmsSfVozRj/Ti9m8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FpTtPMjiE96eqvciY5dQ8CDcqwuM/20Eee/PEIctazr9b0INtJDpVENTAtCCuNuaBT1/FxINTcjDgLbmmF8ZdL6PyOpJpkwez/MphwSxGqZ05Nt/EwpZ7H2uUJ3sB13XR+JWRJhZGr+iWAQCzQPcEAgPYMWP9q9uNqWr7CynepE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Fd3o9dVg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Fd3o9dVg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6689C4CECF; Sat, 5 Oct 2024 10:33:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728124413; bh=Asc7C0Q7KkIXRwzVZKZVujDysoWfmsSfVozRj/Ti9m8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Fd3o9dVgffJ/jxguRI+vFsd8dGojs8L26/wliymcQzHDESfykIRlsypO0UzF8ra+2 Rwk1eb5os7fGELkeVh3GcleTN6Y9c6TfCgLtGlyt8fqJlQD01AU5UYpPE3e4KTQ5P0 4JbjCjfVwpLr4EIH+hvmXcr712sXGQPAHGYM32vT6/14ZRbbrgzeV35WcEKOCp3Uly 7fwURFsjBBhKCWjTQR06ISBsNHrgq+TkcV/q5u/Qo6cjTleS3pZiP/LxYliMUupfvR pkbIDI7sRHqk7y3UYvrWUSHF8Q/6xCwp78KKYq3Dq/+SrR5t6i7ZyI1EZy9t9z+w2F VBdB378RZuaEw== From: Mark Brown Date: Sat, 05 Oct 2024 11:31:35 +0100 Subject: [PATCH RFT v11 8/8] selftests/clone3: Test shadow stack support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241005-clone3-shadow-stack-v11-8-2a6a2bd6d651@kernel.org> References: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@kernel.org> In-Reply-To: <20241005-clone3-shadow-stack-v11-0-2a6a2bd6d651@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 , Valentin Schneider , Christian Brauner , Shuah Khan Cc: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , jannh@google.com, bsegall@google.com, Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook , Shuah Khan X-Mailer: b4 0.15-dev-9b746 X-Developer-Signature: v=1; a=openpgp-sha256; l=10296; i=broonie@kernel.org; h=from:subject:message-id; bh=Asc7C0Q7KkIXRwzVZKZVujDysoWfmsSfVozRj/Ti9m8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBnARXAHVFTZz2fpP/IlFfIdUYojAWZBCX/qD5OSpDf 5/GfQ3CJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZwEVwAAKCRAk1otyXVSH0IiIB/ 4qGOPUGIu5Ardm43irbJgaOKxTzSeACiDbk89SUpmcpRp5WL7LMjMmEFNAjdiOGJ9jSgJHvDSSyPqQ S7ulVyQ0YXlGYzTaaHnOZgN426RyZ7O7bJhjyjJXMJPDeWi+rP683K+tOhNACoIEnOjNWcW+HOjTD9 GEZ8OJOKXIHcY/B0GgrtF1vQ255HBcUMD5R2dFqul04PvcKnk8rXHXQalnaeP3pLQaVWv1dY1iWcEn Y3TbwrAkxEjFAuyNwtuSaQkBMmrIEdVwK333Zo8rZu+bVyKC7saBgvXIcx8RaXw9smkslFAxf+Csef NTZ4lsoiIKE7XE5ClYBY5hiqFbPgPI 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 check that a user specified shadow stack can be provided, and that invalid combinations of parameters are rejected. In order to facilitate testing on systems without userspace shadow stack support we manually enable shadow stacks on startup, this is architecture specific due to the use of an arch_prctl() on x86. Due to interactions with potential userspace locking of features we actually detect support for shadow stacks on the running system by attempting to allocate a shadow stack page during initialisation using map_shadow_stack(), warning if this succeeds when the enable failed. In order to allow testing of user configured shadow stacks on architectures with that feature we need to ensure that we do not return from the function where the clone3() syscall is called in the child process, doing so would trigger a shadow stack underflow. To do this we use inline assembly rather than the standard syscall wrapper to call clone3(). In order to avoid surprises we also use a syscall rather than the libc exit() function., this should be overly cautious. Acked-by: Shuah Khan Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 143 ++++++++++++++++++= +++- tools/testing/selftests/clone3/clone3_selftests.h | 63 ++++++++++ 2 files changed, 205 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selfte= sts/clone3/clone3.c index 5b8b7d640e70132242fc6939450669acd0c534f9..b0378d7418cc8b00caebc6f92f5= 8280bc04b0f80 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -3,6 +3,7 @@ /* Based on Christian Brauner's clone3() example */ =20 #define _GNU_SOURCE +#include #include #include #include @@ -11,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -19,8 +21,12 @@ #include =20 #include "../kselftest.h" +#include "../ksft_shstk.h" #include "clone3_selftests.h" =20 +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 +34,10 @@ 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, + CLONE3_ARGS_SHADOW_STACK_MISALIGNED, + CLONE3_ARGS_SHADOW_STACK_NO_TOKEN, + CLONE3_ARGS_SHADOW_STACK_NORMAL_MEMORY, }; =20 typedef bool (*filter_function)(void); @@ -44,6 +54,44 @@ struct test { filter_function filter; }; =20 + +/* + * We check for shadow stack support by attempting to use + * map_shadow_stack() since features may have been locked by the + * dynamic linker resulting in spurious errors when we attempt to + * enable on startup. We warn if the enable failed. + */ +static void test_shadow_stack_supported(void) +{ + long ret; + + ret =3D syscall(__NR_map_shadow_stack, 0, getpagesize(), 0); + if (ret =3D=3D -1) { + ksft_print_msg("map_shadow_stack() not supported\n"); + } else if ((void *)ret =3D=3D MAP_FAILED) { + ksft_print_msg("Failed to map shadow stack\n"); + } else { + ksft_print_msg("Shadow stack supportd\n"); + shadow_stack_supported =3D true; + + if (!shadow_stack_enabled) + ksft_print_msg("Mapped but did not enable shadow stack\n"); + } +} + +static void *get_shadow_stack_page(unsigned long flags) +{ + unsigned long long page; + + page =3D syscall(__NR_map_shadow_stack, 0, getpagesize(), flags); + if ((void *)page =3D=3D MAP_FAILED) { + ksft_print_msg("map_shadow_stack() failed: %d\n", errno); + return 0; + } + + return (void *)page; +} + static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mo= de) { struct __clone_args args =3D { @@ -57,6 +105,7 @@ static int call_clone3(uint64_t flags, size_t size, enum= test_mode test_mode) } args_ext; =20 pid_t pid =3D -1; + void *p; int status; =20 memset(&args_ext, 0, sizeof(args_ext)); @@ -89,6 +138,26 @@ static int call_clone3(uint64_t flags, size_t size, enu= m test_mode test_mode) case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG: args.exit_signal =3D 0x00000000000000f0ULL; break; + case CLONE3_ARGS_SHADOW_STACK: + p =3D get_shadow_stack_page(SHADOW_STACK_SET_TOKEN); + p +=3D getpagesize() - sizeof(void *); + args.shadow_stack_pointer =3D (unsigned long long)p; + break; + case CLONE3_ARGS_SHADOW_STACK_MISALIGNED: + p =3D get_shadow_stack_page(SHADOW_STACK_SET_TOKEN); + p +=3D getpagesize() - sizeof(void *) - 1; + args.shadow_stack_pointer =3D (unsigned long long)p; + break; + case CLONE3_ARGS_SHADOW_STACK_NORMAL_MEMORY: + p =3D malloc(getpagesize()); + p +=3D getpagesize() - sizeof(void *); + args.shadow_stack_pointer =3D (unsigned long long)p; + break; + case CLONE3_ARGS_SHADOW_STACK_NO_TOKEN: + p =3D get_shadow_stack_page(0); + p +=3D getpagesize() - sizeof(void *); + args.shadow_stack_pointer =3D (unsigned long long)p; + break; } =20 memcpy(&args_ext.args, &args, sizeof(struct __clone_args)); @@ -102,7 +171,12 @@ static int call_clone3(uint64_t flags, size_t size, en= um test_mode test_mode) =20 if (pid =3D=3D 0) { ksft_print_msg("I am the child, my PID is %d\n", getpid()); - _exit(EXIT_SUCCESS); + /* + * Use a raw syscall to ensure we don't get issues + * with manually specified shadow stack and exit handlers. + */ + syscall(__NR_exit, EXIT_SUCCESS); + ksft_print_msg("CHILD FAILED TO EXIT PID is %d\n", getpid()); } =20 ksft_print_msg("I am the parent (%d). My child's pid is %d\n", @@ -184,6 +258,26 @@ static bool no_timenamespace(void) return true; } =20 +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; @@ -327,6 +421,50 @@ static const struct test tests[] =3D { .expected =3D -EINVAL, .test_mode =3D CLONE3_ARGS_NO_TEST, }, + { + .name =3D "Shadow stack on system with shadow stack", + .size =3D 0, + .expected =3D 0, + .e2big_valid =3D true, + .test_mode =3D CLONE3_ARGS_SHADOW_STACK, + .filter =3D no_shadow_stack, + }, + { + .name =3D "Shadow stack with misaligned address", + .flags =3D CLONE_VM, + .size =3D 0, + .expected =3D -EINVAL, + .e2big_valid =3D true, + .test_mode =3D CLONE3_ARGS_SHADOW_STACK_MISALIGNED, + .filter =3D no_shadow_stack, + }, + { + .name =3D "Shadow stack with normal memory", + .flags =3D CLONE_VM, + .size =3D 0, + .expected =3D -EFAULT, + .e2big_valid =3D true, + .test_mode =3D CLONE3_ARGS_SHADOW_STACK_NORMAL_MEMORY, + .filter =3D no_shadow_stack, + }, + { + .name =3D "Shadow stack with no token", + .flags =3D CLONE_VM, + .size =3D 0, + .expected =3D -EINVAL, + .e2big_valid =3D true, + .test_mode =3D CLONE3_ARGS_SHADOW_STACK_NO_TOKEN, + .filter =3D no_shadow_stack, + }, + { + .name =3D "Shadow stack on system without shadow stack", + .flags =3D CLONE_VM, + .size =3D 0, + .expected =3D -EINVAL, + .e2big_valid =3D true, + .test_mode =3D CLONE3_ARGS_SHADOW_STACK, + .filter =3D have_shadow_stack, + }, }; =20 int main(int argc, char *argv[]) @@ -334,9 +472,12 @@ int main(int argc, char *argv[]) size_t size; int i; =20 + enable_shadow_stack(); + ksft_print_header(); ksft_set_plan(ARRAY_SIZE(tests)); test_clone3_supported(); + test_shadow_stack_supported(); =20 for (i =3D 0; i < ARRAY_SIZE(tests); i++) test_clone3(&tests[i]); diff --git a/tools/testing/selftests/clone3/clone3_selftests.h b/tools/test= ing/selftests/clone3/clone3_selftests.h index 39b5dcba663c30b9fc2542d9a0d2686105ce5761..26ff1554408a59af26bd708dc9c= 852210e370828 100644 --- a/tools/testing/selftests/clone3/clone3_selftests.h +++ b/tools/testing/selftests/clone3/clone3_selftests.h @@ -31,12 +31,75 @@ 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_pointer; +#ifndef CLONE_ARGS_SIZE_VER3 +#define CLONE_ARGS_SIZE_VER3 96 /* sizeof fourth published struct */ +#endif }; =20 +/* + * For architectures with shadow stack support we need to be + * absolutely sure that the clone3() syscall will be inline and not a + * function call so we open code. + */ +#ifdef __x86_64__ +static pid_t __always_inline sys_clone3(struct __clone_args *args, size_t = size) +{ + long ret; + register long _num __asm__ ("rax") =3D __NR_clone3; + register long _args __asm__ ("rdi") =3D (long)(args); + register long _size __asm__ ("rsi") =3D (long)(size); + + __asm__ volatile ( + "syscall\n" + : "=3Da"(ret) + : "r"(_args), "r"(_size), + "0"(_num) + : "rcx", "r11", "memory", "cc" + ); + + if (ret < 0) { + errno =3D -ret; + return -1; + } + + return ret; +} +#elif defined(__aarch64__) +static pid_t __always_inline sys_clone3(struct __clone_args *args, size_t = size) +{ + register long _num __asm__ ("x8") =3D __NR_clone3; + register long _args __asm__ ("x0") =3D (long)(args); + register long _size __asm__ ("x1") =3D (long)(size); + register long arg2 __asm__ ("x2") =3D 0; + register long arg3 __asm__ ("x3") =3D 0; + register long arg4 __asm__ ("x4") =3D 0; + + __asm__ volatile ( + "svc #0\n" + : "=3Dr"(_args) + : "r"(_args), "r"(_size), + "r"(_num), "r"(arg2), + "r"(arg3), "r"(arg4) + : "memory", "cc" + ); + + if ((int)_args < 0) { + errno =3D -((int)_args); + return -1; + } + + return _args; +} +#else static pid_t sys_clone3(struct __clone_args *args, size_t size) { return syscall(__NR_clone3, args, size); } +#endif =20 static inline void test_clone3_supported(void) { --=20 2.39.2