Skip to contentSkip to conversation

x86: Support shstk via prctl

PATCH Superseded Bill Roberts 2 participants 11 messages Aug 20

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover x86: Support shstk via prctl

This is a continuation of adding prctl support for x86:
- https://patchew.org/linux/[email protected]/

History:
- v1: Initial Patches
- v2: Nacked by author
- Refactors the test suite
- v3:
- Fixes an existing fork bug pointed out by Shashiko
- Fixes a state rollback issue in the patch itself
- Fixes Makefile dependencies
- Add's a test for shadow stack state locking
- Refactors the arch_prctl and prctl tests into two different files using a shared testing suite file

FWIW X86 Maintaners and Rick, I structured this so you can just pull the bug fixes only (up to but not including commit "x86/shstk: support via prctl") if you have concerns on the other patches for me to address, this will allow you to take the bug fixes and additional test now if desired.

Bill Roberts (5):
selftests/x86: fix Makefile dependencies selftests/x86: fix fork bug
selftests/x86: add shadow stack lock test x86/shstk: support via prctl selftests/x86: add generic prctl shadow stack test

 arch/x86/kernel/shstk.c                       |   56 +
 tools/testing/selftests/x86/Makefile          |   12 +-
 tools/testing/selftests/x86/shadow_stack.c    | 1287 +++++++++++++++++
 tools/testing/selftests/x86/shadow_stack.h    |   14 +
 .../testing/selftests/x86/test_shadow_stack.c | 1168 +--------------
 .../selftests/x86/test_shadow_stack_prctl.c   |   12 +
 6 files changed, 1384 insertions(+), 1165 deletions(-)
 create mode 100644 tools/testing/selftests/x86/shadow_stack.c
 create mode 100644 tools/testing/selftests/x86/shadow_stack.h
 create mode 100644 tools/testing/selftests/x86/test_shadow_stack_prctl.c
Signature · 1 line
2.55.0
#1 lore raw

PATCH v3 1/5 selftests/x86: fix Makefile dependencies

The Makefile has a macro called "extra-files" that sets up the dependency on the target. Right now, for some targets that need EXTRA_FILES added to the compilation, the dependency is left untracked. Thus, to fix this, use the aforementioned macro.

---
 tools/testing/selftests/x86/Makefile | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/x86/Makefile +4 -3
index 434065215d12..a9a229f83b77 100644
@@ -124,6 +124,10 @@ $(eval $(call extra-files,test_syscall_vdso_32,thunks_32.S))
 $(eval $(call extra-files,fsgsbase_restore_64,clang_helpers_64.S))
 $(eval $(call extra-files,fsgsbase_restore_32,clang_helpers_32.S))
 $(eval $(call extra-files,sysret_rip_64,clang_helpers_64.S))
+$(eval $(call extra-files,amx_64,xstate.c))
+$(eval $(call extra-files,avx_64,xstate.c))
+$(eval $(call extra-files,avx_64,xstate.c))
+$(eval $(call extra-files,apx_64,xstate.c))
 
 # check_initial_reg_state is special: it needs a custom entry, and it
 # needs to be static so that its interpreter doesn't destroy its initial
@@ -135,6 +139,3 @@ $(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack
 $(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack
 
 $(OUTPUT)/avx_64: CFLAGS += -mno-avx -mno-avx512f
-$(OUTPUT)/amx_64: EXTRA_FILES += xstate.c
-$(OUTPUT)/avx_64: EXTRA_FILES += xstate.c
-$(OUTPUT)/apx_64: EXTRA_FILES += xstate.c
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v3 1/5] selftests/x86: fix Makefile dependencies

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The target `avx_64` has its extra files dependency declared twice consecutively. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The target avx_64 has its extra files dependency declared twice consecutively. --

--- Patch [1]: [PATCH v3 1/5] selftests/x86: fix Makefile dependencies --- commit d009708fbed1c85db2f909f2fe20cdcff1a21a01
Author: Bill Roberts <[email protected]>

selftests/x86: fix Makefile dependencies

This commit updates the x86 selftests Makefile to track dependencies for compilation targets that use EXTRA_FILES. It uses the extra-files macro to ensure these dependencies are correctly evaluated during the build.

tools/testing/selftests/x86/Makefile $(eval $(call extra-files,test_syscall_vdso_32,thunks_32.S)) · line 129

Quoted 11 lines
tools/testing/selftests/x86/Makefile +3 -0
index 434065215d127..a9a229f83b771 100644
@@ -124,6 +124,10 @@ $(eval $(call extra-files,test_syscall_vdso_32,thunks_32.S))
 $(eval $(call extra-files,fsgsbase_restore_64,clang_helpers_64.S))
 $(eval $(call extra-files,fsgsbase_restore_32,clang_helpers_32.S))
 $(eval $(call extra-files,sysret_rip_64,clang_helpers_64.S))
+$(eval $(call extra-files,amx_64,xstate.c))
+$(eval $(call extra-files,avx_64,xstate.c))
+$(eval $(call extra-files,avx_64,xstate.c))

[Severity: Low]
This isn't a bug, but is this duplicate declaration for avx_64 intentional?

Quoted 3 lines
tools/testing/selftests/x86/Makefile +1 -0
+$(eval $(call extra-files,apx_64,xstate.c))
 
 # check_initial_reg_state is special: it needs a custom entry, and it

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v3 2/5 selftests/x86: fix fork bug

In reply to Bill Roberts

Shashiko correctly pointed out this fork bug, the report is below. This patch fixes it by checking for the child and error conditions explicitly.

[ ... ]

Review

Quoted 13 lines
(quoted diff) +13 -0
+int test_ptrace(void)
+{
+     unsigned long saved_ssp, ssp = 0;
+     struct sigaction sa = {};
+     struct iovec iov;
+     int status;
+     int pid;
+
+     iov.iov_base = &ssp;
+     iov.iov_len = sizeof(ssp);
+
+     pid = fork();
+     if (!pid) {

[Severity: High]
This is a pre-existing issue, but could this code broadcast SIGKILL to all user processes if fork() fails?

If fork() returns -1 due to resource limits, execution will proceed with pid = -1. When ptrace() subsequently fails, the error path is triggered:

out_kill:

        kill(pid, SIGKILL);
        return 1;

Since pid is -1, POSIX specifies that this will kill all processes the current user has permission to terminate, potentially crashing the host environment.

---
 tools/testing/selftests/x86/test_shadow_stack.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/x86/test_shadow_stack.c +5 -0
index 3d6ca33edba4..b52c5420c137 100644
@@ -971,6 +971,11 @@ int test_ptrace(void)
 	iov.iov_len = sizeof(ssp);
 
 	pid = fork();
+	if (pid < 0) {
+		printf("[FAIL]\tFork failed for %s\n", __func__);
+		return 1;
+	}
+
 	if (!pid) {
 		ssp = get_ssp();
 
Signature · 1 line
2.55.0
#4 lore raw

PATCH v3 3/5 selftests/x86: add shadow stack lock test

In reply to Bill Roberts

Add a test that locks the shadow stack write bit and then attempts to disable write. The disable should fail with EPERM since it's in a locked state. This also preserves the write bit being set for the whole test suite as well as preserving the ability to unlock at the end.

---
 .../testing/selftests/x86/test_shadow_stack.c | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
tools/testing/selftests/x86/test_shadow_stack.c +39 -0
index b52c5420c137..72e7329d89d7 100644
@@ -1060,6 +1060,24 @@ int test_ptrace(void)
 	return 1;
 }
 
+static int test_locking(void)
+{
+
+	if (ARCH_PRCTL(ARCH_SHSTK_LOCK, ARCH_SHSTK_WRSS)) {
+		printf("[FAIL]\tCould not lock Shadow stack write\n");
+		return 1;
+	}
+
+	if (ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_WRSS) != -EPERM) {
+		printf("[FAIL]\tCould change Shadow stack write after lock\n");
+		return 1;
+	}
+
+	printf("[OK]\tShadow stack locking\n");
+
+	return 0;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret = 0;
@@ -1079,12 +1097,33 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
+	/* Note: test_shadow_stack_lock() needs write enabled */
 	if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_WRSS)) {
 		printf("[SKIP]\tCould not enable WRSS\n");
 		ret = 1;
 		goto out;
 	}
 
+	unsigned long status = 0;
+
+	if (ARCH_PRCTL(ARCH_SHSTK_STATUS, &status)) {
+		printf("[FAIL]\tCould not get Shadow stack status\n");
+		ret = 1;
+		goto out;
+	}
+
+	if (status != (ARCH_SHSTK_WRSS|ARCH_SHSTK_SHSTK)) {
+		printf("[FAIL]\tStatus not as expected, got 0x%lx status, wanted: 0x%llx\n",
+			status, (ARCH_SHSTK_WRSS|ARCH_SHSTK_SHSTK));
+		ret = 1;
+		goto out;
+	}
+
+	if (test_locking()) {
+		ret = 1;
+		goto out;
+	}
+
 	/* Should have succeeded if here, but this is a test, so double check. */
 	if (!get_ssp()) {
 		printf("[FAIL]\tShadow stack disabled\n");
Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH v3 3/5] selftests/x86: add shadow stack lock test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The comment references a function name that does not exist in the code. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The comment references a function name that does not exist in the code. --

--- Patch [3]: [PATCH v3 3/5] selftests/x86: add shadow stack lock test --- commit ff0ac7d8a7598f33a03169f21e6b92ce89cdf8a4
Author: Bill Roberts <[email protected]>

selftests/x86: add shadow stack lock test

This commit adds a test to lock the shadow stack write bit and verifies that attempting to disable write fails with EPERM. It also ensures the write bit remains set for the whole test suite and preserves the ability to unlock it.

Quoted 4 lines
tools/testing/selftests/x86/test_shadow_stack.c +0 -0
index b52c5420c1374..72e7329d89d70 100644

[ ... ]

tools/testing/selftests/x86/test_shadow_stack.c int main(int argc, char *argv[]) · line 1100

Quoted 5 lines
tools/testing/selftests/x86/test_shadow_stack.c +1 -0
@@ -1079,12 +1097,33 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
+	/* Note: test_shadow_stack_lock() needs write enabled */

[Severity: Low]
This isn't a bug, but the comment refers to test_shadow_stack_lock() which does not exist. Should this comment instead reference the test_locking() function that is implemented and called in this patch?

Quoted 5 lines
tools/testing/selftests/x86/test_shadow_stack.c +2 -0
+	if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_WRSS)) {
+		printf("[SKIP]\tCould not enable WRSS\n");
 		ret = 1;
 		goto out;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#6 lore raw

PATCH v3 4/5 x86/shstk: support via prctl

In reply to Bill Roberts

Historically, managing the user-space shadow stack state on x86 has been handled exclusively through the arch_prctl() interface via the ARCH_SHSTK_* operations. However, other architectures (such as arm64 and riscv) do not implement arch_prctl() and instead utilize the newer, arch-agnostic, prctl() interface (i.e. PR_GET_SHADOW_STACK_STATUS and PR_SET_SHADOW_STACK_STATUS).

To provide language runtimes, toolchains, and libc implementations with a consistent, cross-architecture interface for managing control-flow integrity, wire up the generic shadow stack prctl handlers for x86.

Map the generic PR_SHADOW_STACK_ENABLE, PR_SHADOW_STACK_DISABLE, and PR_SHADOW_STACK_LOCK operations onto the underlying x86 internal CET helper routines. This allows portable userspace applications to toggle or query shadow stack states without relying on architecture-specific system calls, while maintaining backward compatibility with existing arch_prctl() calls.

---
 arch/x86/kernel/shstk.c | 56 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
arch/x86/kernel/shstk.c +56 -0
index 0ca64900192f..2c2a96b2d936 100644
@@ -18,6 +18,7 @@
 #include <linux/sizes.h>
 #include <linux/user.h>
 #include <linux/syscalls.h>
+#include <linux/prctl.h>
 #include <asm/msr.h>
 #include <asm/fpu/xstate.h>
 #include <asm/fpu/types.h>
@@ -630,3 +631,58 @@ bool shstk_is_enabled(void)
 {
 	return features_enabled(ARCH_SHSTK_SHSTK);
 }
+
+#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK \
+		(PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE)
+
+/* Handles the generic prctl interface for PR_SET_SHADOW_STACK_STATUS and its feature bits */
+int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status)
+{
+	int rc;
+	int tmp_rc;
+
+	if (status & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK)
+		return -EINVAL;
+
+	/*
+	 * x86 arch_prctl is single bit at a time, so handle these one at time
+	 * If anything fails, rollback state.
+	 */
+	if (!(status & PR_SHADOW_STACK_ENABLE))
+		return shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
+
+	rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK);
+	if (rc)
+		return rc;
+
+	if (status & PR_SHADOW_STACK_WRITE) {
+		rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_WRSS);
+		/* rollback, best attempt, if we actually enabled shadow stack in feature bits */
+		if (rc && (status & PR_SHADOW_STACK_ENABLE)) {
+			tmp_rc = shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
+			WARN(tmp_rc, "Could not rollback shadow stack enabled: %d\n", tmp_rc);
+		}
+
+		return rc;
+
+	}
+
+	return shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_WRSS);
+}
+
+/* Handles the generic prctl interface for PR_LOCK_SHADOW_STACK_STATUS and its feature bits */
+int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status)
+{
+	return shstk_prctl(t, ARCH_SHSTK_LOCK, status);
+}
+
+/*
+ * We assume the prctl() feature bits line up with the arch_prctl() specific ones. If not,
+ * the flags returned via arch_get_shadow_stack_status will be mapped wrong.
+ */
+static_assert(PR_SHADOW_STACK_ENABLE == ARCH_SHSTK_SHSTK);
+static_assert(PR_SHADOW_STACK_WRITE  == ARCH_SHSTK_WRSS);
+int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status)
+{
+	return shstk_prctl(t, ARCH_SHSTK_STATUS, (unsigned long)status);
+}
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v3 4/5] x86/shstk: support via prctl

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Flawed rollback logic destroys an already-enabled shadow stack if enabling WRSS fails. - [High]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Flawed rollback logic destroys an already-enabled shadow stack if enabling WRSS fails.
- [High] Unconditional toggling of WRSS causes spurious failures and partial state application when features are locked.
- [Medium] Setting PR_SHADOW_STACK_WRITE without PR_SHADOW_STACK_ENABLE silently disables the shadow stack. --

--- Patch [4]: [PATCH v3 4/5] x86/shstk: support via prctl --- commit b3742dadd5cde35b115774fa09769c5a44a81e76
Author: Bill Roberts <[email protected]>
Subject: x86/shstk: support via prctl

This commit wires up the generic prctl handlers for shadow stack on x86, mapping PR_SHADOW_STACK_ENABLE, PR_SHADOW_STACK_DISABLE, and PR_SHADOW_STACK_LOCK to the internal CET helper routines. This provides a consistent cross-architecture interface for user-space applications to manage control-flow integrity.

Quoted 4 lines
arch/x86/kernel/shstk.c +0 -0
index 0ca64900192f8..2c2a96b2d9367 100644

[ ... ]

Quoted 13 lines
arch/x86/kernel/shstk.c +9 -0
@@ -630,3 +631,58 @@ bool shstk_is_enabled(void)
 {
 	return features_enabled(ARCH_SHSTK_SHSTK);
 }
+
+#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK \
+		(PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE)
+
+/* Handles the generic prctl interface for PR_SET_SHADOW_STACK_STATUS and its feature bits */
+int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status)
+{
+	int rc;
+	int tmp_rc;

arch/x86/kernel/shstk.c bool shstk_is_enabled(void) · line 652

Quoted 10 lines
arch/x86/kernel/shstk.c +10 -0
+
+	if (status & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK)
+		return -EINVAL;
+
+	/*
+	 * x86 arch_prctl is single bit at a time, so handle these one at time
+	 * If anything fails, rollback state.
+	 */
+	if (!(status & PR_SHADOW_STACK_ENABLE))
+		return shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);

[Severity: Medium]
Does this silently ignore a request for PR_SHADOW_STACK_WRITE without PR_SHADOW_STACK_ENABLE?

If an application calls prctl(PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_WRITE), the condition (!(status & PR_SHADOW_STACK_ENABLE)) evaluates to true.

It appears this will call shstk_prctl() to disable the shadow stack and return 0 for success, instead of returning an error like -EINVAL for the unsupported configuration.

arch/x86/kernel/shstk.c bool shstk_is_enabled(void) · line 664

Quoted 12 lines
arch/x86/kernel/shstk.c +12 -0
+
+	rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK);
+	if (rc)
+		return rc;
+
+	if (status & PR_SHADOW_STACK_WRITE) {
+		rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_WRSS);
+		/* rollback, best attempt, if we actually enabled shadow stack in feature bits */
+		if (rc && (status & PR_SHADOW_STACK_ENABLE)) {
+			tmp_rc = shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
+			WARN(tmp_rc, "Could not rollback shadow stack enabled: %d\n", tmp_rc);
+		}

[Severity: High]
Can this rollback logic unintentionally destroy an already-enabled shadow stack?

If an application already has its shadow stack enabled, but WRSS is locked to disabled, a call to prctl(PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE) will process the SHSTK enable as a no-op (returning 0).

When it attempts to enable WRSS, shstk_prctl() will return -EPERM due to the lock. The rollback condition checks (status & PR_SHADOW_STACK_ENABLE) from the user request rather than checking if SHSTK was actually toggled during this specific call. Could this cause the pre-existing shadow stack to be disabled?

Additionally, a concurrent thread locking the shadow stack during the rollback window could trigger a user-controllable kernel WARN, potentially leading to a denial of service if panic_on_warn is set.

arch/x86/kernel/shstk.c bool shstk_is_enabled(void) · line 671

Quoted 7 lines
arch/x86/kernel/shstk.c +7 -0
+
+		return rc;
+
+	}
+
+	return shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_WRSS);
+}

[Severity: High]
Will this unconditional disablement return spurious errors when features are locked?

If an application locks WRSS to disabled, and later calls prctl(PR_SET_SHADOW_STACK_STATUS, PR_SHADOW_STACK_ENABLE) without the WRITE flag, the shadow stack is successfully enabled.

However, the code falls through to unconditionally call shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_WRSS). Because WRSS is locked, shstk_prctl() will fail with -EPERM, even though the target state matches the current disabled state.

Could this return an error to user space after partially applying the state, potentially leaking the allocation if the application aborts based on the error?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#8 lore raw

PATCH v3 5/5 selftests/x86: add generic prctl shadow stack test

In reply to Bill Roberts

Run the same test suite for arch_prctl, against prctl, to ensure consistency and correctness between the interfaces.

---
 tools/testing/selftests/x86/Makefile          |    5 +-
 tools/testing/selftests/x86/shadow_stack.c    | 1287 +++++++++++++++++
 tools/testing/selftests/x86/shadow_stack.h    |   14 +
 .../testing/selftests/x86/test_shadow_stack.c | 1212 +---------------
 .../selftests/x86/test_shadow_stack_prctl.c   |   12 +
 5 files changed, 1324 insertions(+), 1206 deletions(-)
 create mode 100644 tools/testing/selftests/x86/shadow_stack.c
 create mode 100644 tools/testing/selftests/x86/shadow_stack.h
 create mode 100644 tools/testing/selftests/x86/test_shadow_stack_prctl.c
tools/testing/selftests/x86/Makefile +4 -1
index a9a229f83b77..110b1b241bb7 100644
@@ -19,7 +19,8 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			test_FCMOV test_FCOMI test_FISTTP \
 			vdso_restorer
 TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
-			corrupt_xstate_header amx lam test_shadow_stack avx apx
+			corrupt_xstate_header amx lam test_shadow_stack avx apx \
+			test_shadow_stack_prctl
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
@@ -128,6 +129,8 @@ $(eval $(call extra-files,amx_64,xstate.c))
 $(eval $(call extra-files,avx_64,xstate.c))
 $(eval $(call extra-files,avx_64,xstate.c))
 $(eval $(call extra-files,apx_64,xstate.c))
+$(eval $(call extra-files,test_shadow_stack_64,shadow_stack.c))
+$(eval $(call extra-files,test_shadow_stack_prctl_64,shadow_stack.c))
 
 # check_initial_reg_state is special: it needs a custom entry, and it
 # needs to be static so that its interpreter doesn't destroy its initial
tools/testing/selftests/x86/shadow_stack.c new file +1287 -0

1,287 lines not on this page · raw message

tools/testing/selftests/x86/shadow_stack.h new file +14 -0
new file mode 100644
index 000000000000..d36a44df7387
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#ifndef TOOLS_TESTING_SELFTESTS_X86_SHADOW_STACK_H_
+#define TOOLS_TESTING_SELFTESTS_X86_SHADOW_STACK_H_
+
+enum shadow_stack_test {
+	SHADOW_STACK_TEST_ARCH_PRCTL,
+	SHADOW_STACK_TEST_PRCTL,
+	SHADOW_STACK_TEST_COUNT,
+};
+
+int shadow_stack_run_tests(enum shadow_stack_test which_test);
+
+#endif /* TOOLS_TESTING_SELFTESTS_X86_SHADOW_STACK_H_ */
tools/testing/selftests/x86/test_shadow_stack.c +7 -1205

1,225 lines not on this page · raw message

tools/testing/selftests/x86/test_shadow_stack_prctl.c new file +12 -0
new file mode 100644
index 000000000000..4641a3b1ca03
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Same as test_shadow_stack.c but uses the PRCTL interface
+ * instead of ARCH_PRCTL.
+ */
+#include "shadow_stack.h"
+
+int main(int argc, char *argv[])
+{
+	return shadow_stack_run_tests(SHADOW_STACK_TEST_PRCTL);
+}
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH v3 5/5] selftests/x86: add generic prctl shadow stack test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The global `_which_test` variable used by `segv_gp_handler` is only initialized inside…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The global _which_test variable used by segv_gp_handler is only initialized inside test_32bit(), creating an implicit execution dependency. --

--- Patch [5]: [PATCH v3 5/5] selftests/x86: add generic prctl shadow stack test --- commit 4df1cdb98415fac5314af3be03f1bd5e57dfbd7c
Author: Bill Roberts <[email protected]>

selftests/x86: add generic prctl shadow stack test

This commit refactors the existing shadow stack selftests to allow running the suite using both arch_prctl and the generic prctl interfaces. It moves the core logic into a shared file and introduces a new test executable for the generic prctl.

Quoted 4 lines
tools/testing/selftests/x86/shadow_stack.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 13 lines
tools/testing/selftests/x86/shadow_stack.c new file +13 -0
+sigjmp_buf jmp_buffer;
+enum shadow_stack_test _which_test;
+void segv_gp_handler(int signum, siginfo_t *si, void *uc)
+{
+	segv_triggered = true;
+
+	/*
+	 * To work with old glibc, this can't rely on siglongjmp working with
+	 * shadow stack enabled, so disable shadow stack before siglongjmp().
+	 */
+	SHADOW_STACK_DISABLE(_which_test);
+	siglongjmp(jmp_buffer, -1);
+}

[ ... ]

Quoted 6 lines
tools/testing/selftests/x86/shadow_stack.c new file +6 -0
+int test_32bit(enum shadow_stack_test which_test)
+{
+	struct sigaction sa = {};
+	struct sigaction *sa32;
+
+	_which_test = which_test;

[ ... ]

tools/testing/selftests/x86/shadow_stack.c line 1147

Quoted 4 lines
tools/testing/selftests/x86/shadow_stack.c new file +4 -0
+int shadow_stack_run_tests(enum shadow_stack_test which_test)
+{
+	int ret = 0;
+

[Severity: Medium]
Could this create a test execution order dependency?

The global variable _which_test is used by segv_gp_handler to multiplex between the arch_prctl and prctl APIs before calling siglongjmp(). However, _which_test is only explicitly initialized when test_32bit() executes.

Since segv_gp_handler is also used by test_uretprobe() and test_uprobe_call(), if the test execution order changes or if test_32bit() gets skipped, _which_test will remain uninitialized (defaulting to 0, which corresponds to SHADOW_STACK_TEST_ARCH_PRCTL). This could cause the generic prctl test binary to incorrectly use the arch_prctl APIs in its signal handler.

Should _which_test be initialized here at the entry of shadow_stack_run_tests() instead of inside test_32bit() to ensure it is always cleanly set for all tests that rely on this signal handler?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#10 lore raw
In reply to Bill Roberts
Quoted 4 lines

FWIW X86 Maintaners and Rick, I structured this so you can just pull the bug fixes only (up to but not including commit "x86/shstk: support via prctl") if you have concerns on the other patches for me to address, this will allow you to take the bug fixes and additional test now if desired.

Nack'ing again, Shashiko has valid concerns and some minor nits in the commits. Sorry for the spam, just realized I can get this bot running locally I believe via
https://github.com/sashiko-dev/sashiko

Maybe that will help someone as we battle the era of bots.

Thanks