From nobody Sat Oct 4 06:33:17 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 3FC3D24EA90; Tue, 19 Aug 2025 16:27:15 +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=1755620836; cv=none; b=BsFaL76YywVvC9+hhyIor+AQRb8nkbljD28WFcmhiQS9orzJJsdLUKFm4CAvzXzAK85aL3vMhupCBsNJsLbJC/yKlqiegPIglY/byimfJwJIkInSoBuEClQA4kpDMIEq/+MnIu77mYUUA1vBfb3jwhi9k4pJC5fECgWd0M+3K6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620836; 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=LV4M2AUz/t9EIfhuzg18/3HfQ6Wu5zkNwIw7tJGPA/ynl80Uabflz9+FVdISYWqwCfva9EEBG27EYWjy6GXGnUb8gHT5DJIfYBWGOhBO2OFQ63SeiEKDBUmX620h/gw1XBYIW4eBPMo3TXexrCcSWghU83xAdzNhmP5zbWLMYwA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mG5Nht3f; 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="mG5Nht3f" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C676C113D0; Tue, 19 Aug 2025 16:27:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620835; bh=sCkuhZhQLgVA+X2B9E/24D8lf8QLxbBNxj1NjjW+v04=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mG5Nht3f84Q5VTewVMfPte9wo/B1JeUugMm5OYacumVcsiulYF5HT4UmAcDoWABJv lT8FKLtjnanElC9gidxE8r1TDfIP/AovpEKOF9CuzxJ55PhnUxPOxL4aGED2vCxE/G +orm7ozyn32Q3/7dWavQfCvqs3prJck0bGmHTpuYZ5KA8fS8QAC3UYVtegS6rucHLo wD+OShym5ukDRMNBTRmJMSaweFu6UIwIRvpC+AWI0gD4d8VkXpUWtyjineC/ccQk/A vc7Gyn7kto4PNHl6XopPSvyZAhEBHxvlSo0sBKK9YO9krfa7AOS8MN8YSOhpM/7533 F4K1vqaBBn5lg== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:47 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-1-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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/pANAwAKASTWi3JdVIfQAcsmYgBopKXPN3yAT+iEP6l5t5KQ9WtZj/u1xSCDpyuiR HCGAXK471OJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSlzwAKCRAk1otyXVSH 0MB8B/9+XrLDMVZdjgtEDztlLW4AykJOQ6+Hqd0rWPG76jByPwN6NYQsdgMOvQ6AoI1YlGNZ35C VBDwqR0iRuq2FbHUrFsLd82f+HU4IS9wZcz65b3zgppFABbUGMW5MKv4JLy0wilhGe4s7EChG95 rcwLBoH+Qg38/VTbki2wi5q2tYvNFEy6Hm/8xrdAfcPdlj79GIi7c6nmRBDwl3reoqgu0SlkUKA 6XgXjsdtSTSUcCLxPaoNdjVV7c+2aZf25zO4j+QLHFx7ufsIofO6kQQwsSaoYQsCECtTxrql9UL iciW9ARdLDJFQxeQulvsuqFrMkX4yVb0AJ1XFGRJIFG2gZQc 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 Sat Oct 4 06:33:17 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 D40F2256C91; Tue, 19 Aug 2025 16:27:22 +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=1755620842; cv=none; b=fVAjHwgRM2obsRbIUdte/PwUlVqYHNI3GtAY08tssftSi9MGAEXKzkm+mz8PT7ndHX0qPAMivfFU9bww9gZaxt4XltzVh/ZZ64195vh+J9kglTtdJblYNy108f6Sd5+2jAMiBalj+RD13b2g6PBtlaE9K/wYZM5er5i2xodqimQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620842; c=relaxed/simple; bh=hVaIM8V8kRd9AQDq914Wwe26mHFpW+Fx9glpdzPR7+8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hNrcngXjLVNOTDgz1qaPWaG/JAyrT2bGQO2ViCmYr+i1uMiitr7nL1907iPzhsVYn7GcOV+qzaGQVX12ySjplsaH7CS2ZHpSZ2E/3x29uW2yi7yvlitYCUAl4BkhvANxl0us+4G9s7wvSaY7xR8uIwfEx2+dOnRwhwGFkg/L4pQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DyPLfa5T; 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="DyPLfa5T" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32DE8C4CEF1; Tue, 19 Aug 2025 16:27:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620842; bh=hVaIM8V8kRd9AQDq914Wwe26mHFpW+Fx9glpdzPR7+8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DyPLfa5TK2OpgwmCPBJImANFYCq6autqHJOl9oj5Jxw6GQFMyCV3UQgZju5aY74kj UFf19PLU9ejKGdziB/1W66uYaCxAIMqGk+LWv5PwoHOMAOPHT528Y5D29rY3r8Tm0g bxRm1u7v59lueFmlP85aut4623DemO85Ua1Ve15RSGhiIyHN7nOCp+fLzCJT00pQ3Q hK9UcXPEgxEDvCs/W0/SNqlLKrwwKVfLH5WPosxHpKYh9yCGrOAjUbi3Zr2cdt9LU/ fakGNIWhf9n73AK+H4dLnymI68NgoLpUCEI6YVcJB2z7krir59P55AdXvZ+s7SbRrV Pw/psMcw9ZYPQ== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:48 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-2-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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=3291; i=broonie@kernel.org; h=from:subject:message-id; bh=hVaIM8V8kRd9AQDq914Wwe26mHFpW+Fx9glpdzPR7+8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBopKXQTdY3ugJkYbJS5pLU75D0MW2jy457UDv26 3WzN4ar6iiJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSl0AAKCRAk1otyXVSH 0NRaB/wJySdY1ayPL1KkBWJiBZ2rO3++CePDThRvIUqVv8MSOS5JV6ExNNLfaCcL+n4mdOIRwBP k+Vp23qjXejxL1E49NBonyonPKK5kc0/PpZBHW3q+BPEVqBXxYsK7yz4itNzxdkCdQNFQvNLHrF W1cOZaoHItMs9p5NPZmQ4bXiFwPoe72IIAEl29WnhU10pFNZlUqcTqBQyWFiPVrt/hxCbnJcKV2 eaUmQkuxUdXTLop38o/lP6W2OI/x9VR/tpDCgbCh2JCwBJV45Hgw7SZKv5cQhn12g2Z2iLasK+O inCZguORVZZHRYpPPwpQDMiae+5Slcnuf25s2zmPwnPLeoPC 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..65c665496624 --- /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 +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() + - 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 Sat Oct 4 06:33:17 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 A3F01258ECF; Tue, 19 Aug 2025 16:27:32 +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=1755620852; cv=none; b=mJHIFhEUBj91dAmfkZD4R2ha+IYOHI6px1ehj6SeuKrvBiXurloCy44LEajzoMZwpzcqzwsQxfumJyd+MFQcEL5InNQeUJJFVKY7bdLiOdLR/Y7mhX8TpFurhp0PbtIa7qANBb+CHk0RjiLndNf7VnbEdMxO2AeEgnJuGxxW6eg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620852; c=relaxed/simple; bh=CpY1ufLEWWNH3kGc/B+gDx5FfDuMHKHnHbWU0IDFJ7U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=glhWvAa5/tu9FBk2jtuwsfmFg0LcoED2gLukqWntFONkeJXwzMEbz58j/IaYVPCGYPRBQXqUkG4pPZzSfRsHeomEzQEimgqOYyAl2mqkzJTpcucF+xm7Zf1iTPzWDGlmm3or0+dzZTTCq5TI8UQTG0bQJTgjMM2YRTZrCpn+I9g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dUzPpd5C; 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="dUzPpd5C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2DF12C4CEF1; Tue, 19 Aug 2025 16:27:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620852; bh=CpY1ufLEWWNH3kGc/B+gDx5FfDuMHKHnHbWU0IDFJ7U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dUzPpd5Ch1gAwPQ4RMaF/mxwKUbtKJ1PKn8ki8qZo9Te/0N4A4Rn9LvOzDASrBIXu /htVaV+/Kox56Sik0AGYaruBOeOdsUtRERWyYcSncLHHAizu1hHWged6nDb0QlAFL0 hLwYqIU5zgeBRFMdALCVDCchNK3HrLdMk7jlre/sxqyHTZ3AoT1MnQFTNozJ3nmpbi oWHRnkb5cdVkSmr+JNRlCmqFMooM4iHHi85+GSOd+v+02jKdt6esSuYDqY/zby0fR2 y6oMoQzfXyXPAsksJO8G3qjaFfEeuJ0dq3Hqcfn9tk8biU0XxUCWy/eV2KXXivOlEk t7guF3sK5vEag== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:49 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-3-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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/pANAwAKASTWi3JdVIfQAcsmYgBopKXQ+7orl49b6QFaoHfJhPRBM3cnmhhrps0Co fsFp62Q9+KJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSl0AAKCRAk1otyXVSH 0DcOB/sHQiTiHO3xkP1GksYe+otBd9UQ0/3RoFjDWhCyiDlr2V1d8tpeMVYv4zI/IcdQB0kOc8l oNvq5WuTLvMJ5nV53b9ORY/8qb1g0X8pmQ8E1dL5936i7LjW395X9Qf28NUZ6kVRumD88VR2JU4 iI7O5Df5YDFk8O4dFgm5nIP0X4fU8UgBeL9GsJqWD4lMQqlKLkeUg6RuG7vC1n+K9b9ZDW5zXWR nf1EYzdLdMwbV0DzHVZGXj2Agt0dxyrbF+XTagF9k+qIVllLiriMBY/heTswJ1wRgkKg5xuUpX3 A4RvKR6Uo1hp+kv4Xkhm1lMM0ovkw/LgZjJpdLx9kDb+aSUe 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 Sat Oct 4 06:33:17 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 EC20B274FDB; Tue, 19 Aug 2025 16:27:38 +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=1755620859; cv=none; b=mpKjE2VfkB7rLw+6a59R4GTaGtCCquzUR6CkFzaJOY/mbK2dSaR10VZVJ/ph18NZF0kXjLU6ujpefstfXh8TxXKbGEmjeOh4h9TUf+GCQjHxiT5/lFCy98PSz6Shnx8/hGRnnWDZk7JKujhxTE6+SJ3FvvYHBorY7Eq/ul5gR0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620859; c=relaxed/simple; bh=Cjn/a+o4KuvIqWC0eDpwQ/gOgEDn7+78ZNHfX1o8OsY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=g3CSyurdyOaOe+KUkt8GM5jMEnOQeJeujG/F8sQzUMQ+NcFoUOR+YCSFZUusPCHbhFAxCn49zNMaBdp+vgfkZDhtDhSDV7PzAbt2kEK2R1o0p7N4rAbDZXkqy44qrzCW0tMP66u4m73CI9+yjxaKYwB/Gcr8pQ+AKJ31FKcmA7w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GDzx/2EL; 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="GDzx/2EL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBC20C113D0; Tue, 19 Aug 2025 16:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620858; bh=Cjn/a+o4KuvIqWC0eDpwQ/gOgEDn7+78ZNHfX1o8OsY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GDzx/2ELt6CBjvXx263xyF9fXwjrziX5xvKHhFv62aMBm0EHryfTbYyNYIEKPOG6l kOS+I4RgpMkB3gW+IMMHNMqVngXXZR5WlbhJRDADsTwLl9LsR3Y2jMruwH6UGcnf92 bxf3MM3LjZot0kQ2q/FFQr56A9pM/w5WZW4fqfVxb5EJKmk/4jtlWaRcoImabVUI4J 2cyvUThECPd6zDxNN/ciJ120yFiKNPCXYcqLsSiR0T6CATqpjuZ/16cE5K2BlzjqOx xn6gs13dAMEy+hMFVpsbRx3BZCMocPIb2HGB3k7ODQTEGbClkcUjuiwQxwL9dMwBA6 kXMx0elNCl9Ig== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:50 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-4-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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=16315; i=broonie@kernel.org; h=from:subject:message-id; bh=Cjn/a+o4KuvIqWC0eDpwQ/gOgEDn7+78ZNHfX1o8OsY=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBopKXRSVAm9vlQ9rj4GuAVfZsMR4zQNeY9HasYV Xmc3kj4k3OJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSl0QAKCRAk1otyXVSH 0MYGB/9vjD7fp/hgPG2P12HuZI+zMVigQEv0Kkg4RNaJX2Pw+RTvlEKufS+ivGsuqFAtC+Vy2ys W6uY/w/Qcfij65DSx/SavJAVfBDUGxeYGMup91embycyhFzro72b8bH5BFBCk+6HzFs9PvUH298 74ZLrSJ1sNi5VWPAuNPgYysqzmeeQlnhODbKj/DEMh9ntxUEYZtY+4oVQG6XdMI9mFK0aQOoTgo 731u7dKcIS8gtiRRu8Xi3TcPDjE580TESJy4YgRpCGkrciW49uwumkBRXRqYT1MI4UkZnx6MDIi AHTcPC4nP1VHGffwM5rgfJyrjJwf6ecWdiukLwuWxFU9RgOA 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 Signed-off-by: Mark Brown --- 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..88ca9eaebc96 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) +{ + /* + * SSP is aligned, so reserved bits and mode bit are a zero, just mark + * the token 64-bit. + */ + void *maddr =3D page_address(page); + unsigned long token; + int offset; + u64 expected; + + 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 Sat Oct 4 06:33:17 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 6A29127AC32; Tue, 19 Aug 2025 16:27:44 +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=1755620864; cv=none; b=QSW+8lksp4YLEVoXdJzp2rhsPjWrBDbAC0UiI/IbKAFi3eI3n9L08EWwiySX8YuW7PIWDDQmd0Fg51L8G3B1K0u5MEgBTh0scFl5ddlumHB/fs0jPsfok55iNNUoVG+1apiRNbokPIAiJJS/0Y3GfnD8xXSZ0aSZkrd4I5vlskk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620864; c=relaxed/simple; bh=E5jTrDbxtCpIeDLiMUwge4Mqphic3I5ve2LQ4cJCm7o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OG8to3NZ4MaS8WlJNgzvfI/HsD/RYWfDiSiLZfuVyM4vJRug5SDT4iayknufC8MghvVdhLoJ61xIEzMFFrwbkSFc7G6c3NMjBGSNpSVbCSBd6wYdp3LhB8OXBJCJs6xlUUpAEYhnjLbAavVl3hWRzkjxWp3bGcfpXP/XnagIVLo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KS++6vJd; 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="KS++6vJd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF34CC4CEF1; Tue, 19 Aug 2025 16:27:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620864; bh=E5jTrDbxtCpIeDLiMUwge4Mqphic3I5ve2LQ4cJCm7o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KS++6vJdhA02yEzbb0qk9D8K14YkIzxiwIyt0M1N6hXDs8Bc16T6lWkN8I5u5o82z 4+vMSpmueGiiebZsh5OpE45M57b/nKqdO6kOKjJPwKrskzDOO2gudXLyx7B030bgwn 4ddcT9xyNvjhDELohwns6pFQwhV/E2rC8DxGkiw6TTc5Tg9qweSyHKyXLqp0ZXcgop Zsei1Ca0AKyskEOyMJBRWm18ULVxQQlkiKjyhteRhpY+LJ4Dpvxvla3LLpBEEYKak9 CjSM36xXbKDo5P5/hkY7nPYUGshrfLenTNyRxajXpG/Ky7Zsr7pFVJoMhhCcKbOPGR 1iiSW+HT5gbog== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:51 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-5-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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/pANAwAKASTWi3JdVIfQAcsmYgBopKXSx20XuLhIePiF8ibu/ryFRcdQSedJ/vEaJ 1c7WVKs80+JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSl0gAKCRAk1otyXVSH 0PRTB/9tD6cOeMNKk5lyZVv7/lLxOBFVoeYnGGizqdEV+J58ggFgrYlRBOE5EaBiNCLh/nTbUaP HCxLGVAn/02v69fcbQXiwkhJllHc3stS4q5cLePgtwnSJFv01HKWNSr4Bv0euH2hr0/uSK8/wKC q31xj02i+fwJ9zF2hHyHUBwcJAduuW42xj0cu1bMi/cqjMnLIkfavdJSHGbIRdPuoTD/fl1Yb0s CcjPKFydm+iQT1iEXm6zk8sMLBxlcx3YB4mNm4fY5bSdIITy1F5l1/V8oOlHW/ecPh0Nyd83xu2 vV/GXxSVAp7AXb2hUrkisQDgDWMRYxWnBs92hJ4ykmKkK7tG 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 Sat Oct 4 06:33:17 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 A3F15214A6A; Tue, 19 Aug 2025 16:27:50 +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=1755620870; cv=none; b=BlkAVVH4bCk68j5EYPW8Qb1449xo5Osa5yVY36jBcBrea45PczsSSTu90WzJdsuZbHYNlHScU7cItbsChtVqitUsbVAdTXFumbS3DBJcaZ+VgpqYV/qLY/EtydbhvSCwbvwQyx3ld9wA42Dnr7Fao2SbQooyZYt25paNNwYEvHU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620870; c=relaxed/simple; bh=XMiGAtjdArL2gbxrFhicE4t6Y0M2WueXT37wxBI+sMo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WUPSYU9GW9n9isBShxbk0hoicK7d52P+JM630AgsUmbhybGI9fREwvCuEEvsi9yqwN1kS72rq7CR3GWto0lpadEX4gpQ3rCGOijgFh6nVmNs3g7k9lyN9jVJQ/BF1js4A3Rbz42gfCoAvooNrPQExW3yGtLN7ByHsodbBJwN1zc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d+JxJLh0; 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="d+JxJLh0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4548C4CEF1; Tue, 19 Aug 2025 16:27:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620870; bh=XMiGAtjdArL2gbxrFhicE4t6Y0M2WueXT37wxBI+sMo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=d+JxJLh0SJwTYUA/93AwSF8/tSdgXyMP64Ee2L9v21GpDJfx6w7p6UhNbvZ8c3PlI U0UqU95hhUUXMyVj2PatjtyX52YZK4V/I2f3ZXp0nUAh1jdj7ND1/xPzbCU8Z6IRRB 4dQBbY+wrU2+ONNJyLFf/vkic+aEul8gY+f+5XhTsdUZLc95O87D/Z4+jB3YtSrG46 /+d17fgrPiBibwGCNduRlOZsC96KtL03FdVR8LXmcIDyC9J4qts9HT3A5/QExxu/7t MjuCXNQZUQtQJB56ar/27kNiUD8s2sC5vamPySyD2tAvmuWU2W/IJH5de3xfs8ADOr gUsybBVi/CFQg== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:52 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-6-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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/pANAwAKASTWi3JdVIfQAcsmYgBopKXTExHE/eEr4zmk/VVQse1Hz6TRf2NQFW+Cr 5OlXUREwr6JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSl0wAKCRAk1otyXVSH 0GpaB/4v8PwFrs7AOMi66cA6Tsi/qBjflH3KQrj3At4pq7TFPyO91m0lAQh1WXFylFC5JAF6uCf uzV/uQXIFyaT9pTlUFea5OkxOf7zYs2cOzeuq+8hL3yahyQsVSjrr9UAOthoUVSoZsb8PR82pu+ LvUDRKEdyokDt0SBOBPh0kFSNFHBWJH+Gl1EWFqq0MAj1SFHP46UzniKrKTUfurI6kE+zk/uBlk 159yhkqFqXTAuKGm2f8SmDwT1lFBJ/jD2hb7GxW60oUhJbuQvfsx3k8svlQYDbhb6sDw1qdUR4e 1pw1JDqj4I9u8mtH+62X19zkTVQBrW6bgbOIxiYJPwP9vaEh 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 Sat Oct 4 06:33:17 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 97D11214A6A; Tue, 19 Aug 2025 16:27:56 +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=1755620876; cv=none; b=Dc0oRtKRqTeBKxpML3L0qFDmNbDBz+FxZpfbuCTEobFnxAY29nRblsjmpSI4ZChNOPckf2UzU5rHIXvMt6c61l2EmFrbQ0Ve4E3PvyGmn8H8Y/CqakcPeFPmGSv/RrsHdCHXqxI6YXB9/DDMyADZM1MzBZu1bBl4LxsItBVhHv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620876; c=relaxed/simple; bh=8IZxQR2EJWcC8CtTN5ZvDL0ZGuJwJUzmD8bnJ/92bkQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EAVfxW3Oe7tTjmpPel7WYS90XfknGjhFoDenNtoMM3SoysIPTDVjjeTN+hMpglkfYW3kMsEagtTHnM6ZdK7x7e86JkCQveRR8BOcBRLAYLsBdVu0/Vp5cfmfwWPRZEqpxxUT7W8Q1xPbvxx5KEzNO+OI0PS3hVVOYbb2Jwlu7p8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=G8r6ilfH; 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="G8r6ilfH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9669CC19421; Tue, 19 Aug 2025 16:27:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620876; bh=8IZxQR2EJWcC8CtTN5ZvDL0ZGuJwJUzmD8bnJ/92bkQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=G8r6ilfHHGQ7GjSE9WFdweYjEjMxoBVX50S+NDSRWCjj6h9zLbpnGBvovKZyeO7nb YFCqWONmAe9IaZeDnn10oPTAURZFbjMgoupkqlmQrq59Oi2bitCMF1RrZ44KABzNXR PW3oAly13FJmRrwKB1j/Kac+SepTjn4fgGmZhyaRNDeiu05TDZ2mBsPVfBPHgd2wYU 7M7AUWBKj8p9vCPd8cfdrmJdLIWH1HNx5z91Lo81SBg4XTXH48vkqlkIzWnifKx3TM 0rnHRFxpnbzV8JdS2+aDLN53s2Z/cDYFHvoy3gYA44m3wzc6rjV8vXqMelaW6pc1wp xXUxtCbInXzvQ== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:53 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-7-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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/pANAwAKASTWi3JdVIfQAcsmYgBopKXTiaTNYXEL7eVrB66m1hpHLBTUk8zXThuHz MmL7+wlfoWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSl0wAKCRAk1otyXVSH 0GgAB/4+ZjjtJ/ZQej1kUcB7oSFe6zl3cTPBDsV3yDkrvMfk+xEYVV5VFZUfCmpqEfqfKTef8/e IA6ILUKJq0ge0leTKm74xP/hLJfmluH41auWlEIWns4fJTjMDVzrhlxs5OBaQeUSObd3kmHbAil kubyh06T9FXrJAj0DxKdatAB42hPjz9Ba7a3JmYpcKg0Kgn00R8TgAgPikh+xzZVvEch4zSaM5d CKFe7lgZXAy/6nAv+c5E9zPlaxzUMGUmGAV1NWwT3yySBdrJEZ4ZlpIhvtDPbdR1zz91GmKhp6X 26RfbRfI5Sarxb6gMXMGcNdE7mQu/urvDmJ76kFgBfuye4ov 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 Sat Oct 4 06:33:17 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 8E3E8257851; Tue, 19 Aug 2025 16:28:02 +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=1755620882; cv=none; b=RYSZwHLXAo9ltUg8wMCTKsrCMU9bTr2shd/UfzceHXV+RAfjW1+R5kpUl6rGvXP5t66+Myc5jhFvlA/ysvjkRXnlZFko+oFWMPcTyD0nKtIr3C4hYyvFC9RJwbu14+FgRe9qyZxBVacpvUFS63iibRPKsTLjD3LwzD9+iZTfe8c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755620882; c=relaxed/simple; bh=BpkR4AzIyL5Wv4KvQJJXzhwkykopqHSofdHP/8A50zc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sjdunkYl2fd8+3U+Sa5PnynHCZz9S7FUUiA1T2QpSoKtS4f13dU1Km4Yrz/WMI5/t7Lk9tLs0ruF4KN5rmYCZ6M7RV5jAGIAW9tMeYc7LtH4yUpBoo+UauenzxF5CK/SGnMP2bEvXCg6d6iv608r5gwktx+eZiP+ai4jpCUqnrk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fNj/6zDT; 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="fNj/6zDT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7CB7BC116B1; Tue, 19 Aug 2025 16:27:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755620882; bh=BpkR4AzIyL5Wv4KvQJJXzhwkykopqHSofdHP/8A50zc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fNj/6zDT/xPhlRPMiaWlWjZmROk3dhDesMsFSQbYKAaiOdePWOAMWHBvf6/GHkaLe t0Rz4iKt6XmCYdRiyUxh6eqr76rPrCjn6YMOsMqIqK+yiIgOTee72kbbfn75lGmHNX r0tUDXyp5vlYqiucHDZ5Tzt4J9o5lPVnurrQFngvgXF4F/LHv34gthO9m2oCjyD13A 7WyXnvj4hZqdN2TJJjAyaUqOaBBFzEmpabEICojNI4iO5XVjyYrYOtEgHDF/gOxdl4 gZ95T33B87hgrtKWajHpCD7n+ee+W9mJWiwNnc0gIB7Vqf3nMwLfpQeIz6vDWTto3N ivTewNeJhRXSQ== From: Mark Brown Date: Tue, 19 Aug 2025 17:21:54 +0100 Subject: [PATCH v19 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: <20250819-clone3-shadow-stack-v19-8-bc957075479b@kernel.org> References: <20250819-clone3-shadow-stack-v19-0-bc957075479b@kernel.org> In-Reply-To: <20250819-clone3-shadow-stack-v19-0-bc957075479b@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/pANAwAKASTWi3JdVIfQAcsmYgBopKXUUSvpijPUSHSnwGZIi7jjvIPT63XY9DEWe 4f3GCh/7LuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCaKSl1AAKCRAk1otyXVSH 0I6KB/9VFtOoMzfQzyeHKN4wrLB8W1uDEpeRDWnMuR9X0Hkyu4DBmZp7IftAN7yeSYYuhEIhxTC 7asLReRl0HWwoyZPq3tMAv3chOkaqJtVETpJKZoTj+77sx7103mJ4O8Gy8fggty8hRZCQklEvgC ypXCZxbvsaryoxJ/b1pKbf+cWVQVVp4MyDgIYba0AOacHhzS89QP6kxhUbFUpg1HuemaJUCPUpi bkWhGF7bm1eSQ4jlsMTIARN6eYvZAYfZbfDra2VL+gcuuY0/TlxXFoV9TmUwLXcbMrpMQf/e3eI CEHHnY18ZaPYpiApfDVotZnUomDjoUJrXe2Ok1oAlBsXpl28 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