From nobody Fri Oct 3 10:10:16 2025 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 7D1242F3C1D; Tue, 2 Sep 2025 10:22:29 +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=1756808550; cv=none; b=kitUFtfJaOeBE6pYOn8kmrUZlnxxFw0jGGpJWDu5cQvdLEtevAz9FMHsSnzhIRBa1A/9+wAYVChjWjqOuaepBO/VxpqQnuaoGJhBifq60xrVAwFTFwqrzUud9FDB24643CdBVGS9vPuU50KmAqZDKaWtqLX735GyjO8DzQtlL7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808550; c=relaxed/simple; bh=sCkuhZhQLgVA+X2B9E/24D8lf8QLxbBNxj1NjjW+v04=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BYBl/nPWjLhakEyacowuIUxB9XOWrlHMygbAAGg8ppVDCdQpcsClPqf7BiR3wiWcA6V8uGYwY4KoW7BJ92mtyHrwj3I/F9SHj3Y0AC1EFGaOcZ6EFFN207xxIgi1Shp7CH5k+Hd5qMSvGTd+kXab/4jdGV9PkQul7ScoiTJKdbI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aRyd9SBM; 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="aRyd9SBM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5EEFC4CEF5; Tue, 2 Sep 2025 10:22:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808549; bh=sCkuhZhQLgVA+X2B9E/24D8lf8QLxbBNxj1NjjW+v04=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=aRyd9SBMcjdV4og1ltHdfkn4DvlbCPrVqh/uF73gIB2HTJBwdumz0/cvg9M+cgeQU Sg3VBET4Px11382wau/TU1802KVoNRk8fJ9Vce6Umyt34VB2L1O7UH0mX106oUlkEH 6ZYfZZbbnrIhNcBvwU2z7XqHW9/58gCnS+2qlFCs0cYPW/6mc39lkSE9AdMWthTEJs EBsDBrCTXDNb8Ox6aejU9FoFV0aTOxMQa1hlEWUvkr+QR/kC5m7LUB3x3wzK1Ue99j LdWkasRA6FRxglQE7qaevZI7u1Kw/iVlbWcucXNWPLNJpFjtxNsl4qYLCuSfm/v+V1 NDi1I8pyQ3+JQ== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:45 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-1-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook X-Mailer: b4 0.15-dev-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=3726; i=broonie@kernel.org; h=from:subject:message-id; bh=sCkuhZhQLgVA+X2B9E/24D8lf8QLxbBNxj1NjjW+v04=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVTvFrPQ/44ijTMy4nQPjMRqOSQw1Ozc9WMy spgEEVX9TaJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFUwAKCRAk1otyXVSH 0MisB/93EeWirSq+14A3iPUXfZq1WyF3Btt+T07QHDPQofqiG3RFcIdqnv/ZfqiHAj/WHUb09YH kYNfRoD5dE549tpy1I0AAz3gMWa90j94kzf42Cxa+NfBg/rY1PzWRQEaQNct/fWmaygvmNfrGpB 3s4I2w8HZ/uTsSE1bAeb2bn3yEifufSGujnAdcK3DPBGQNDpSojsnKfZJj/ZxzYEbObngqlHWa8 YdjGdBcYtZ8NPqMPKMjjWFL4VNz16v0AR5UYU8VHvujGyGphDDE7dRjF/ycKMCcAMxkNUVgnmkz K+XrHO3ICgAA0WO2/DqV7RgTdAFfMwGq3V9TaWGP3GLXJLNd 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. Acked-by: Deepak Gupta Reviewed-by: Catalin Marinas Signed-off-by: Mark Brown --- 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 5bc432234d3a..b4bbec9382a1 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 96482a1412c6..f0b1bea9c873 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -299,7 +299,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; @@ -310,9 +310,9 @@ static int copy_thread_gcs(struct task_struct *p, p->thread.gcs_el0_mode =3D current->thread.gcs_el0_mode; p->thread.gcs_el0_locked =3D current->thread.gcs_el0_locked; =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 return 0; } diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 6e93f78de79b..3abcbf9adb5c 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.5 From nobody Fri Oct 3 10:10:16 2025 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 60FCA2836A3; Tue, 2 Sep 2025 10:22:35 +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=1756808555; cv=none; b=tdC5OinueBvIGXVz9FbchB2PHolNFHB85dqXLGRWo7JIkqMLPdvRFGfnuy85Nm46CZBDQiNdF51Z/2w5RABUthHK71cAr+HUt21SIGZdEBHh7al7womMXEoPK0ks3NvXIe4PYbpykqvD5sgGwwKUtmRbHQiireSkCWVWKx+pJ3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808555; c=relaxed/simple; bh=ezL+4DeZdmcwM1aOt0IjT9Heuymi4ERrDFr1xBHVoaw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uiWYr0Ib7ZxyuuJM7YHeGDVZhJ9w00400xMr7g7CBS7EVB413Me736/uim6tEZa4qT+3ZMOiwiZehgAwj6jn7UmbPHVurFRZZ5iMKgTKnqJBktXHohugfb3hF/JCV2BFpWZAfuDcOweGhhyN6tB74i9LZwXv0tI8lDCFyMsbBdY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jVuWTeEh; 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="jVuWTeEh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8655AC4CEED; Tue, 2 Sep 2025 10:22:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808555; bh=ezL+4DeZdmcwM1aOt0IjT9Heuymi4ERrDFr1xBHVoaw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jVuWTeEheko+2ailkmfcNMumzyUjZfUqujJxFDwB2W7+wGnuea3X4Th+s7ExK4E5t J0XZMRF7gvXZFRBsBnNouaZIhR1UM70HikMpEBckz8sVNrzequewLUiPzWV61PfwLM RNOTugcMYWPOqZIB/c+Kq7i+28zf9KQsd4q9MYSENeehowbyQwvpVTjtzqJYSZR/Lr o7BsYcwUW8lewELDJUhnX+WXFSrRuviwLVQhUyz+dRpcUjek4H2i9ULMwqFNmUatU9 YEn012M5AkztAHvIeY87IeYzoGOd+8xbjJC5nnY901YmUh3VZiaaAdTvV46cLg88vm /nbLGAXS2Ab6Q== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:46 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-2-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , 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-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=3290; i=broonie@kernel.org; h=from:subject:message-id; bh=ezL+4DeZdmcwM1aOt0IjT9Heuymi4ERrDFr1xBHVoaw=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVUHWgG1vwknbFT86yhj+SPeFJI9f0+BAWbt ZD2fRGsGfeJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFVAAKCRAk1otyXVSH 0ERAB/9/57aYZcpXltdrAkJBQ4IPy11apdLVyGggNwrw1ZE9zwyIQSBTqG7XQ7WsLwNe1KbKZkQ ZI3QG5OnA/M/e0kN3gTnjtlmO6xsWw2d8t5JQIQjCmAka3RKBNDn+WqCEPJWNh8AIwE7vW34R+q IsqBuCPes0CkoYCaoWdDoQdNi+KF/J7oduQJZhTylUG59CiXnXUVsK1W5fZpsCmkJ9Np6Z6vzEa 1H8Ml3lfZ+yjBvOhw3l8yqELobMUenIYjBxIoyQGuxI3japf/x4zNX+/WwgeaZChn1Y2KE6hkbv cOz+vwLVLdl4cuv5AH1ZhVWMbCwLB7AOUTMaVFi2lJlyk0R0 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 Acked-by: Yury Khrustalev Reviewed-by: Deepak Gupta Tested-by: Rick Edgecombe Signed-off-by: Mark Brown --- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/shadow_stack.rst | 44 ++++++++++++++++++++++++= ++++ 2 files changed, 45 insertions(+) diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspac= e-api/index.rst index b8c73be4fb11..0167e59b541e 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -62,6 +62,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 000000000000..42617d0470ba --- /dev/null +++ b/Documentation/userspace-api/shadow_stack.rst @@ -0,0 +1,44 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=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 +writable 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() + - For other architectures the PR_SET_SHADOW_STACK_ENABLE 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.5 From nobody Fri Oct 3 10:10:16 2025 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 29762283C8E; Tue, 2 Sep 2025 10:22:40 +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=1756808561; cv=none; b=AfZfx4YVtcW7a748102FSNN+jsgfHRNOREQXLGPIgagzs77sSMj4nni49L0QbakFKqK+kdsam7v1Bu1NVxd68oDj56KAopVKWip5MFn5no0RGIW4w8mMi15st2T56QmCSsZOf0YgY47pK8/mARcK/sOHAV+DkPqiQovaYH7PYCA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808561; c=relaxed/simple; bh=CpY1ufLEWWNH3kGc/B+gDx5FfDuMHKHnHbWU0IDFJ7U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QK7EnSsnReKi4KF2sV0R++JisV7UHgUmtun0LKmSE9EE9QxIHQ7OX13EN59gdJVEt/vhd5x5YaQeWUqqzS/v8IM38mCtl3rbjCYqBQ/YCxzSO1Nu1vGtBf9cmf3f7aDAbtebvxinLg6VT8/DK2UOBjUNRm/bhh2KpLT56aqHu14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mu7jvmED; 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="mu7jvmED" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58F01C4CEF7; Tue, 2 Sep 2025 10:22:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808560; bh=CpY1ufLEWWNH3kGc/B+gDx5FfDuMHKHnHbWU0IDFJ7U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mu7jvmEDAqUVNraGKOGKFf24OR0gvQtSe0fGuRX5j9Ey4ODF6FbTp9GmMC9Y2kNDM RlVpLbDXUE37X83PtUTlGcShzegQsLo4b/fvSSTsBOPSaNqP91srzOAbT/yA2cXVq+ gG1JR7j7i3hGa2HrmPCkasU68mtasoaSVAWj2pZICT9n8+9ahabSgp9OIGTu4HvdHk C+gluTv9fgr0fGvegiQsFzPZvHF6jWxphIYCZudcPPSVXim+zSckcLtAx6K2CFA/O1 DssX9vRM7OoEINgVWMW5tLYSU7M5oD5Fa7OIPBeGez4C1MJmBSzvIFjnuZGRdODQrb AHiFChs8wOFng== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:47 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-3-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , 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-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=4438; i=broonie@kernel.org; h=from:subject:message-id; bh=CpY1ufLEWWNH3kGc/B+gDx5FfDuMHKHnHbWU0IDFJ7U=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVVZT2WD/y3IfCbmqY92OOrwgvqXCu6iEUJq zOJeqgYzPWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFVQAKCRAk1otyXVSH 0ABlB/9zV9xiQUCdYYNkXE8DKdutIO1IkB1Wbm3nE+o6FG1JATl4bIwD30ZyYIP5K0XNL1+Kv7O AeqOYPBFeVJZYkEGsQnOp3DcVIdvbbahb47tum/yGDwgvLlinFpnSUOnN9hlayFohi/PaWSLBR4 M288uXKFt9e2wzyAoXQk0Cysu4r6fNI+pDfvoTNWSqHX+zApOLkC6TnGSG3Qn9mtdRi+mEvgT7o xXW9f0GejdfdForjYppkT72VJOVgjRNZhmcCZTcIQn6x9wZNCFPv7UsB2w4J+4NjT8osalm5nrv Ku7IspQFG12iU8YWoZx8A9TZdprKAqsnH2Q06o1WOTGzWdEw 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 Acked-by: Catalin Marinas Tested-by: Rick Edgecombe 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 000000000000..fecf91218ea5 --- /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 __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 __always_inline void enable_shadow_stack(void) +{ + 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.5 From nobody Fri Oct 3 10:10:16 2025 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 E8FEA283C8E; Tue, 2 Sep 2025 10:22: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=1756808567; cv=none; b=R1fuvuCg82PjUXWLwYNydqFXa/7OjBV8ytsO5esPyvfj3+75IkjuE8EOGF44R5Cb46izqm/KfvleejE7R3s1yzH1AswKB/9zvOcQQWI1ouNUOqLhSmF2Bo4rTavnJJJgNs42lD+uUKQIHHjmW5d+p86ki2D341kGFEhN1OSQkeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808567; c=relaxed/simple; bh=1ZjkJedUp1dnDv4q2GOrK/PZdRej0osCSaTnBILMRaY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hSji5VesqSF96efgTC9emb8I352xtRoLAoH/2f6dQxSQFE9Frpw31CiZmDDHL4ByOdXIlwPQ8HHSIk3Vd6Uqdr0RhAjju5cVDL4IgmOyppyiOldoAp/SSR++uKP9wOLLPwPJgtwJiEZEKQHhXEUTMbyHvWTVYsj30TuH0529o5w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I240SRYT; 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="I240SRYT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2812EC4CEED; Tue, 2 Sep 2025 10:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808566; bh=1ZjkJedUp1dnDv4q2GOrK/PZdRej0osCSaTnBILMRaY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=I240SRYTCHLHCJLWjEvaYBfNHQBmlp5n+IovDnP0clrmm39WGJO5Z0S6WGhFMe7XD o/b1WWVlgSIpKIONUvpGPacjreZrlCl4+9IjxzshwB+h68OGTUBiViHo9bS6Vkklck VI1z26A2ZA87nTT5b0jalMe4oA1M2j9QQ8ej3gXD93hd/6ZXH8GwGkEYaznWFGhpsv bXXEFQdHfJrslagKRIZ93i30YlsUCGPubJbBjrxQgtvbw2DzSiW7Kg+oH75gkHubjn kfDkMUIyBoC8CJ2N/DB2NtlZoSuaVc/aXA9H+gLJfCb21JCHO6XhFIheQSjzerqNgP UapaNCQvmxm5Q== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:48 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-4-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , Yury Khrustalev , Wilco Dijkstra , linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Mark Brown , Kees Cook X-Mailer: b4 0.15-dev-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=16361; i=broonie@kernel.org; h=from:subject:message-id; bh=1ZjkJedUp1dnDv4q2GOrK/PZdRej0osCSaTnBILMRaY=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVVgtVGSP7mq0Cb/8XTH2LlIIBL8RFM+gvUl L+mztJyNgeJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFVQAKCRAk1otyXVSH 0C7IB/4gB5YzaAqFBzL+kyYOGTmUCfGURfy6k8Tox7N9izSgKmQ1xLR+7cDuJIsYe2X/MsK5Cso RC7+kKQrNgFsyqyHURMlIZeby4GUgwMDdKAX6ajH4pGWLGBxEOqyQvtbwRi20sSin7sf9bZhBEt Ry5Et8zWaN7bT64d7CuiTqrYH7cdzYzZf+dHpO2kZsy5LS5ZnjOZjvDe005yzMUljbjavsns02Z 8ACIXBWWqhR207AY6inhaLyy3BFAgBd81TAfZoY07+a1Uhqu0U8TcNLs4DGceMcBK/n/+V4BhKc 7UI6NP77Qq+8p0uV7HWe4Oen4yIypXbbdMhVSS9iZNnN5b9i X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Unlike with the normal stack there is no API for configuring 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 parameter to clone3() specifying a 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, the shadow stack is specified by pointing to this token. 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. On architectures with support for userspace pivoting of shadow stacks it is expected that the same format and placement of tokens will be used, this is the case for arm64 and x86. 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. Acked-by: Yury Khrustalev Tested-by: Yury Khrustalev Tested-by: Rick Edgecombe Reviewed-by: Rick Edgecombe Signed-off-by: Mark Brown Reviewed-by: Catalin Marinas --- arch/arm64/mm/gcs.c | 47 +++++++++++++++++++- arch/x86/include/asm/shstk.h | 11 +++-- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 53 ++++++++++++++++++++--- include/asm-generic/cacheflush.h | 11 +++++ include/linux/sched/task.h | 17 ++++++++ include/uapi/linux/sched.h | 9 ++-- kernel/fork.c | 93 ++++++++++++++++++++++++++++++++++--= ---- 8 files changed, 217 insertions(+), 26 deletions(-) diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 3abcbf9adb5c..249ff05bca45 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -43,8 +43,23 @@ 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_token) + 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_token) { + tsk->thread.gcs_base =3D 0; + tsk->thread.gcs_size =3D 0; + return 0; + } =20 if (!task_gcs_el0_enabled(tsk)) return 0; @@ -68,6 +83,36 @@ 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; + + gcspr_el0 =3D args->shadow_stack_token; + if (!gcs_consume_token(vma, page, gcspr_el0)) + return -EINVAL; + + tsk->thread.gcspr_el0 =3D gcspr_el0 + sizeof(u64); + + 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 ba6f2fe43848..827e983430aa 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 1b7960cf6eb0..0a54af6c60df 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -209,7 +209,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 2ddf23387c7e..9926d58e5d41 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -191,18 +191,61 @@ 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) +{ + void *maddr =3D page_address(page); + unsigned long token; + int offset; + u64 expected; + + /* + * kernel_clone_args() verification assures token address is 8 + * byte aligned. + */ + token =3D args->shadow_stack_token; + expected =3D (token + SS_FRAME_SIZE) | BIT(0); + offset =3D offset_in_page(token); + + if (!cmpxchg_to_user_page(vma, page, token, (unsigned long *)(maddr + off= set), + 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_token) + 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_token) { + shstk->base =3D 0; + shstk->size =3D 0; + return args->shadow_stack_token + 8; + } =20 /* * For CLONE_VFORK the child will share the parents shadow stack. @@ -222,7 +265,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 7ee8a179d103..96cc0c7a5c90 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 ea41795a352b..b501f752fc9a 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 @@ -44,6 +45,7 @@ struct kernel_clone_args { struct cgroup *cgrp; struct css_set *cset; unsigned int kill_seq; + unsigned long shadow_stack_token; }; =20 /* @@ -226,4 +228,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 359a14cc76a4..9cf5c419e109 100644 --- a/include/uapi/linux/sched.h +++ b/include/uapi/linux/sched.h @@ -84,6 +84,7 @@ * 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_token: Pointer to shadow stack token at top of stack. * * The structure is versioned by size and thus extensible. * New struct members must go at the end of the struct and @@ -101,12 +102,14 @@ struct clone_args { __aligned_u64 set_tid; __aligned_u64 set_tid_size; __aligned_u64 cgroup; + __aligned_u64 shadow_stack_token; }; #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 af673856499d..d484ebeded33 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1907,6 +1907,51 @@ static bool need_futex_hash_allocate_default(u64 clo= ne_flags) return true; } =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_token) + 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_token); + 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. @@ -2182,6 +2227,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 @@ -2763,7 +2811,9 @@ static noinline 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_token) !=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; @@ -2796,16 +2846,17 @@ static noinline 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_token =3D args.shadow_stack_token, }; =20 if (args.set_tid && @@ -2846,6 +2897,24 @@ 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_token) + return true; + + if (!IS_ALIGNED(kargs->shadow_stack_token, sizeof(void *))) + return false; + + /* Fail if the kernel wasn't built with shadow stacks */ + 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. */ @@ -2868,7 +2937,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.5 From nobody Fri Oct 3 10:10:16 2025 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 99A1428466A; Tue, 2 Sep 2025 10:22: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=1756808573; cv=none; b=WrmRQupwkjatC6p+ZwjrpYgG6C09UIE5i6/SQjV88kffQpBWW9z6alcxCbbS6eu0vS9V6j4uImxwrnw/ExxNXvqGVVIk2GTnAKihkUhzaLWvO0+3n6Xyjo77i5CkIfWm6w8rCqik6PtP0FVzap3HgJe+M7GwaJYQ3jtLQM726zM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808573; c=relaxed/simple; bh=E5jTrDbxtCpIeDLiMUwge4Mqphic3I5ve2LQ4cJCm7o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dRQaKuDxpc8AC1Sv797UUOIBT8VqajeNwfXZ84yr8Zpk6hNRBkAwq+EuESVtJ5eEF0gjYI6jQE2MCffNRVX0r+uj98LF+Wc9fdLSWH1w0whU3g7nBubBnwF3K6e264kdE4W+5Qy/3JKMaOD9134e1ezDioiOpDuHkY6mNP1lpA0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oCGaZZyP; 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="oCGaZZyP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C54EAC4CEF9; Tue, 2 Sep 2025 10:22:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808572; bh=E5jTrDbxtCpIeDLiMUwge4Mqphic3I5ve2LQ4cJCm7o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oCGaZZyPLqQXCDVtkGHccs4sMYQytwbOeuBabRhu/cAhlP9JDw8Eof6/6VzYuimeC dSelB5Ih0O5lHbFktR87vshlfptsSei6iMLCH5UqHQ9JaAzn/toF3TnVy3dgGZqkwH KLdZxT9lmNYo+1Jo14ybykyx4d1qkixpzu7xII/E6FJCallqabs3WKO6FmccxrAUmr gMwRxcKZVTLyH+FWwrmPu53ecTLCNW28+tZtuATXrzFRDBBnqDsuep4hQ7OzXscR0x M4b1EC6LbLEYpT0eO7ZR5cUgNhJDVDN7L/6E+QW4MU60cVINnFvHFSaOO4Nj+4rhas 1qpq+4YOrphAA== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:49 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-5-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , 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-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=1170; i=broonie@kernel.org; h=from:subject:message-id; bh=E5jTrDbxtCpIeDLiMUwge4Mqphic3I5ve2LQ4cJCm7o=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVWRP3iBqfaGDsEwUZV7WCoebNlAg+31iLJ2 i/73Lv8TMuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFVgAKCRAk1otyXVSH 0C+/B/0ee2BGEMfoL62ZEkbha4zuCtctfokJ9gIIqKGXk3MYnYkFaXzcr43bF6hUFv1U3sv6SGQ 91xkxsjK8iLhYPPjsELjVEMXxoeszQI4hvkFITI5C6s93zfiFqk8mJJtTLevHg9DutODKD9s10R Tbhrt5Hjyv/Q36FtveHV+i8I2hegC/SfY+M1e6tdGtShfswinF1TN4g69kCRyAxky+OrHM9M0rA iKa7Q5tS0GBosV3JuoC22mV5w2AqpuShJvYuEsuwy7ShB0kubl2KSmBEajOEOcVYW41DTcByEpr htWu/QzkLiPEHBsX3El1L2SZLXglBD0LbjFpBANbzI8YcYDZ 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 Reviewed-by: Catalin Marinas Tested-by: Rick Edgecombe 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 eeca8005723f..939b26c86d42 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.5 From nobody Fri Oct 3 10:10:16 2025 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 138BC2F6566; Tue, 2 Sep 2025 10:22: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=1756808578; cv=none; b=HGJd8UimezKJM+/+RxuhiY4mzJn8NM3gkcMdNd2VOLiHOHosW5JxNhO9qo+lrBmkum+gnMuU+q9xY1/d6u09tYvNtRkMX0j00XuhpIHldvMqio5ZXiWKW7FiO1PujFHCbi+iRDic9k/I1HG0KftODxzT8C4gpCf+WdL4FArJdJ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808578; c=relaxed/simple; bh=XMiGAtjdArL2gbxrFhicE4t6Y0M2WueXT37wxBI+sMo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VGDY2TGc+TmQ9PKtj4B5NtGtJuRuJvlk7lhlbL3HCLkINB+icoLbuCvW4ClVtGwTkvmneBj/+sxZ7BbIlddswn/0Dv8Mf+NtyQjMFOFSKIEn8K8Z+OM/z3mB8vq2HblMIZEJh73HNZRiTq8AaaHfcTWQNT5h3TJFMnyVyQILW8Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jcJ4ZZVR; 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="jcJ4ZZVR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E347C4CEF5; Tue, 2 Sep 2025 10:22:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808577; bh=XMiGAtjdArL2gbxrFhicE4t6Y0M2WueXT37wxBI+sMo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jcJ4ZZVRj2d5gP9vQgZ43cllsOe47I4PJWXPNF0FfxMO/t5XsmkaGkEAvuN785Jsf RX71rTcI/p/qjt5CG9VE/YjUcNH642b+EojIQxgpFDbnkYJ3gmBU2pqwh4rjNPEe8h BOEut+kG8d2orOr1AfpCUuA+i844W2IsNz5CZov2IBp2rItXpMTl6DzQUaT1DO2Ygz gKg1n7sa4kCUAsXTakJdMIKsGK+VWzR3JKC417Ghg9GRkSpHo5KNJ5hJVgyMuEKCYA ZFIhl0hmhgchSQFKw6i9pfTPbK2R12abrJK2ZwBPJXSunsoyZg8D2NVaW3az2TybIf ySEQpfPh2gHuA== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:50 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-6-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , 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-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=3987; i=broonie@kernel.org; h=from:subject:message-id; bh=XMiGAtjdArL2gbxrFhicE4t6Y0M2WueXT37wxBI+sMo=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVXgGoEf6Nti5rmUyeNKtA+J1F32QsBFy0sr kTaa541022JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFVwAKCRAk1otyXVSH 0JcVB/wJOlhl3F2rxHCjs1vAOo9AWHUNXOlygJqYzoXRc8JvFZxpC8nlsYZSRwkxP4oo+YwH44d Fs/BXTmQdxd9Ut17+OzLymZnxZ/jhyTI+xRC+wsY9nw8+r76360Hk2Kazabga90ng1gYSKKCrPC ekYdmivALKN6YY3EVVQqT1e3IwCrZxBAX2k6fAX9dc6rFJw3WrMWqoifnq3w6GWJUk9mKwtwmQz d2d9uHSZPWC5oyHse9dMQnnrXcBB+7tFaQWTPX/ul5846JH8gga3MLLSyHE/8PE7tYqxr0yhF06 56zLw/4PTdpz4vNjEM2nKGzOhlSJBvtMpf/KBYTSAgK35VjA 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 Reviewed-by: Catalin Marinas Tested-by: Rick Edgecombe 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 e61f07973ce5..e066b201fa64 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.5 From nobody Fri Oct 3 10:10:16 2025 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 3082E2EE280; Tue, 2 Sep 2025 10:23:03 +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=1756808584; cv=none; b=ALy4gbs2CCxFw50m2Ky6kaEXuaiMr+3KyvYqg0G2kp+a1g1XmuirbZJa6KUg/ChqLpF1r+Oo7NwDLBBKCWTYZjLiuKb6oGdgzhayCm1BTpwOCS5WV2EFdbj/6t34ntSISGGG5tnGEVpjuL54AlVfxhsJeqwVvzQDuOOzyeGf4ow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808584; c=relaxed/simple; bh=8IZxQR2EJWcC8CtTN5ZvDL0ZGuJwJUzmD8bnJ/92bkQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EdIUjLA8+OfVVZuP5PTF+Q8sb4zGGPbm9g7jtdH9456j/bvJZsSVcXJV+z9vBC5KYHZcQhNrInAkqFVIpRNXm10ZQdHg1ZBNN2MadXOs+kigcfpmN8jhIGo/cf5EiuXPZ855oBhSoomQFsYyebTNoU1U54bd5fz5klB3F6H3IDo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T1zc8cLF; 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="T1zc8cLF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59CC3C4CEF5; Tue, 2 Sep 2025 10:22:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808583; bh=8IZxQR2EJWcC8CtTN5ZvDL0ZGuJwJUzmD8bnJ/92bkQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=T1zc8cLFD7TMoNSKmJX/9bPl8/N6/Ky5R/1lGAQqkedMIDAKpy+EYlDX24AMLFNuh 6OgOxcv5hSeH1kHF6vL1LqonEikuATullWM6oWh6Y9h0yjoEQ4RwAK41dG5gZBzVDu WbFRr/Z1N6UD47j9lVvP9ze/C8uqrS/zMG9MFzxknOKTGgeE14NqyAjMJWjhGtZh7+ rPkZt96fc2Y2yqmRcQn+K20eH/y7YbfViYxAUaocxVCsklzyIWyDuLwWUKDPiiaiqZ nUhsDRnwxYqJnxIEKWqERAtKQowVwtxxo56nWua5p4CY8Z2sR31TxJR5tAAF1ZbY1m RrH7eWaBTktLw== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:51 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-7-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , 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-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=2131; i=broonie@kernel.org; h=from:subject:message-id; bh=8IZxQR2EJWcC8CtTN5ZvDL0ZGuJwJUzmD8bnJ/92bkQ=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVY6i8bkwneplIfw7BxTTwMdnW3uIvGEPRdu Mk+6G52SPOJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFWAAKCRAk1otyXVSH 0FdIB/42KBpWiDdkKoEaqQqzrK5DxKBxcaFyg5Ngid9FKvSlvM8icwGMvW64vooiLPDipC6Zps2 /+1ISfi2dHUl1B4ceL6h1Zq2R2mzO22/4TukwQpnYHK3IRQyeqOQcv6Lq+VxKz9vaEFinqTfPXq iKuDZwgZcbAPQTRG2BrX+Sk7UrDBVcjvKOsI2KNnl5JJ8gLDGubJXrnXpqK0o2WBx4Ruj3ei199 efh2ZFW4tmb6EbrxIj52fTiDxh8yWR+yjsGOziIWjGpxXQLfSWlp4cv3D/wUTDE++93V7Pmf3Rl 33IRXttZNCwYPYVboGIZMaacNyG4Ewa++Jj5lHSxkidL43AE 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 Reviewed-by: Catalin Marinas Tested-by: Rick Edgecombe 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 e066b201fa64..5b8b7d640e70 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.5 From nobody Fri Oct 3 10:10:16 2025 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 E8037283FC4; Tue, 2 Sep 2025 10:23:09 +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=1756808590; cv=none; b=U+1tS3UcI+cHmmRSpy6cU0hId/4o5fYJpEbe3JwQt1z/uE2LhvAlYLOrGz6f7Rev7HI/JhesOgpCOhaMilOXQw6i7Y3cTecCzMqy0dv8RKuOFHhAvXl67zljJ67ytfYAKkK+WS3TVNpBBYMaN+zwyZJS0WzK3awzp7kozYgJfwg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756808590; c=relaxed/simple; bh=BpkR4AzIyL5Wv4KvQJJXzhwkykopqHSofdHP/8A50zc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cGrTi4PPBZagehagdjhFisEYwm4QVLlroGsIZCiccZO0znNwm1X8lKSA13BgASejVg8vy8YK3vxSJJ+QBbvRvNNQeD8zSluRCk6yRoOqLnMYspJ92FfE+fSDX5OWCVKJkEteItkF0Cncb4jBysTRxJdeyiN8aITi/gIAV8LJ45I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qWYGeqxD; 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="qWYGeqxD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26325C4CEED; Tue, 2 Sep 2025 10:23:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756808589; bh=BpkR4AzIyL5Wv4KvQJJXzhwkykopqHSofdHP/8A50zc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qWYGeqxDI+I5qUvgPYWbt5pAjUp3vjYLoRrrdtuEIY1ZQpMzza9WarnR5MXT9NEsT g3k7ahBIq7s/KpU3p8Gz2CW3w4KBVvww6PMg5Om8ts14Jbm2LRAu+kbbCKB1k1Bgfg M18NfQadBxpWvoDK42mcafoNQfvZBv/3dJD0IJtz0L/VDW9IQ9tgRVuXwxfV0G74Lx 81+Fgc4LyUQa3UX4BMYx0agbBrMajwRLrv++pIw4gIKvOdy+OXr8pd5cL86vRRb9jb DUwjUyToBC24APe3sZvCEyShUEuX7rpMl50myeQ86mhPo4UvOWhn8v2g4zxWo9t/Gy BgyIfRgP5CthQ== From: Mark Brown Date: Tue, 02 Sep 2025 11:21:52 +0100 Subject: [PATCH v20 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: <20250902-clone3-shadow-stack-v20-8-4d9fff1c53e7@kernel.org> References: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@kernel.org> In-Reply-To: <20250902-clone3-shadow-stack-v20-0-4d9fff1c53e7@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, Andrew Morton , 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-cff91 X-Developer-Signature: v=1; a=openpgp-sha256; l=10244; i=broonie@kernel.org; h=from:subject:message-id; bh=BpkR4AzIyL5Wv4KvQJJXzhwkykopqHSofdHP/8A50zc=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBotsVYK55JOPJmLu3dzlgwhu7x9/187EYf0Mx8n i2ljcOM2taJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaLbFWAAKCRAk1otyXVSH 0IQEB/4kvuBt/AdPuLZx90k69H205Hlldj/GiQmhryNY6S+hHeSjlJbXMvoRz5NqR0WtDRGhLJ0 Wx4k0w8KRdOOOOPp0s6/ni0eIeRj+MC7B3IFcHJOckD0GIUgmpvk360yUF64Fa5yCDc7ICZvcP7 gHTlZObgqCIxwvoudhB2T5Cv9w0urO+BdVX4QxSHe10KJWYDpOgcHzhcGlApzzac3+FM/1jS28k kMjg8fu2TE8MG5TtHtk9/dFo9OEB6nVndyMHMmRNzdwojSJykx4tre7yobY9oWQF99FMEDlnvVL 2vjK1Tb5LeKqFId2ERqz0oGis5nuRP2kEVSqOYR5X6rA9InC 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 Tested-by: Rick Edgecombe 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 5b8b7d640e70..6fd2b3238e2c 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_token =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_token =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_token =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_token =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 -EFAULT, + .e2big_valid =3D true, + .test_mode =3D CLONE3_ARGS_SHADOW_STACK_NORMAL_MEMORY, + .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 939b26c86d42..8151c4fc971a 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_token; +#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 __always_inline pid_t sys_clone3(struct __clone_args *args, size_t = size) +{ + register long _num __asm__ ("rax") =3D __NR_clone3; + register long _args __asm__ ("rdi") =3D (long)(args); + register long _size __asm__ ("rsi") =3D (long)(size); + long ret; + + __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 __always_inline pid_t 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.5