Skip to contentSkip to conversation

x86: Support shstk via prctl

PATCH New Bill Roberts 2 participants 11 messages Nov 9

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover x86: Support shstk via prctl

This is a continuation of:
https://lore.kernel.org/lkml/[email protected]/

FWIW, x86 maintainers and Rick, I structured this series so that the initial bug fixes and additional test (patches 1-3) can be taken independently of the prctl() enablement. If there are concerns with the remaining patches that require further work, this allows the fixes and additional test to be merged without waiting for the rest of the series.

History:
- v1: Initial patches
- v2: NAKed by author
- Refactor the test suite
- v3:
- Fix an existing fork bug pointed out by Sashiko
- Fix a state rollback issue
- Fix Makefile dependencies
- Add a test for shadow stack state locking
- Refactor the arch_prctl and prctl tests into separate entry points using a shared test implementation
- v4:
- Fix rollback state logic
- v5:
- Expand commit messages to explain behavior and implementation rationale
- Move unsigned long status to the beginning of the function
- Shorten the comment in arch_set_shadow_stack_status()
- Rewrite the WRSS comment in imperative mood
- Remove an unrelated whitespace change in test_shadow_stack.c

Bill Roberts (6):
selftests/x86: fix Makefile dependencies selftests/x86: fix fork bug
selftests/x86: add shadow stack lock test x86/shstk: support via prctl selftests/x86: support header files in extra-files selftests/x86: add generic prctl shadow stack test

 arch/x86/kernel/shstk.c                       |   80 +
 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 | 1167 +--------------
 .../selftests/x86/test_shadow_stack_prctl.c   |   12 +
 6 files changed, 1406 insertions(+), 1166 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 v5 1/6 selftests/x86: fix Makefile dependencies

The amx_64, avx_64, and apx_64 tests compile xstate.c as an additional source file. However, their Makefile rules add xstate.c directly to EXTRA_FILES without registering it as a prerequisite.

As a result, modifying xstate.c does not trigger a rebuild of the affected test binaries, potentially leaving stale test executables.

Use the existing extra-files macro to register xstate.c as both an additional compilation input and a build dependency.

---
 tools/testing/selftests/x86/Makefile | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/x86/Makefile +3 -3
index 7565d2cf6a3c..407f52ac2628 100644
@@ -125,6 +125,9 @@ $(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,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
@@ -136,8 +139,5 @@ $(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
 
 $(OUTPUT)/sigframe_fpu_portability_64: CFLAGS += -mno-avx -mno-avx512f
Signature · 1 line
2.55.0
#2 lore raw

PATCH v5 2/6 selftests/x86: fix fork bug

In reply to Bill Roberts

The shadow stack ptrace test does not check whether fork() fails before proceeding with the parent-side ptrace operations.

If fork() fails, it returns -1, which is subsequently passed to kill() on the test's error path. Calling kill(-1, SIGKILL) sends SIGKILL to every process the caller has permission to signal, potentially terminating unrelated processes.

Check for fork() failure immediately and return an error before entering the ptrace test's parent-side logic.

Shashiko pointed out this fork bug, and the relevant snippets from the report are below:

[ ... ]

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
#3 lore raw

Re: [PATCH v5 2/6] selftests/x86: fix fork bug

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/x86: missing Fixes trailer and redundant report text in changelog --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/x86: missing Fixes trailer and redundant report text in changelog
--

commit 63564f14db64d744c41e3438f86b4c7188ec7e2a
Author: Bill Roberts <[email protected]>

Quoted 39 lines

selftests/x86: fix fork bug

The shadow stack ptrace test does not check whether fork() fails before proceeding with the parent-side ptrace operations.

If fork() fails, it returns -1, which is subsequently passed to kill() on the test's error path. Calling kill(-1, SIGKILL) sends SIGKILL to every process the caller has permission to signal, potentially terminating unrelated processes.

Check for fork() failure immediately and return an error before entering the ptrace test's parent-side logic.

Shashiko pointed out this fork bug, and the relevant snippets from the report are below:

[ ... ]

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:

Quoted 8 lines
        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.

[Severity: Low]
This fixes a bug in test_ptrace() introduced with the shadow stack test. Should the commit message include:

Fixes 81f30337ef4f ("selftests/x86: Add shadow stack test")

Also, this isn't a bug, but the subject does not mention shadow_stack or test_ptrace(), and the pasted review snippet in the second half of the changelog repeats the explanation already given in the first three paragraphs.

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

PATCH v5 3/6 selftests/x86: add shadow stack lock test

In reply to Bill Roberts

The shadow stack selftests do not currently verify that locking a feature prevents subsequent changes to its enabled state.

Add a test that locks the WRSS feature while enabled and verifies that attempting to disable it fails with -EPERM.

Despite WRSS being locked, this allows the existing code in main() to disable shadow stack before returning to prevent a SIGSEGV from a shadow stack underflow. This is why shadow stack enable is not locked.

---
 .../testing/selftests/x86/test_shadow_stack.c | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
tools/testing/selftests/x86/test_shadow_stack.c +38 -0
index b52c5420c137..e047f1ceae0a 100644
@@ -1060,9 +1060,28 @@ 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;
+	unsigned long status = 0;
 
 	if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK)) {
 		printf("[SKIP]\tCould not enable Shadow stack\n");
@@ -1079,12 +1098,31 @@ 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;
 	}
 
+	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 v5 3/6] selftests/x86: add shadow stack lock test

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/x86: main() comment references nonexistent test_shadow_stack_lock() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/x86: main() comment references nonexistent test_shadow_stack_lock()
--

commit c2aa8d04f3f8897cd4991d5c5c2ee94b8e83b82a
Author: Bill Roberts <[email protected]>

selftests/x86: add shadow stack lock test

This commit adds a selftest that locks the WRSS shadow stack feature and verifies that subsequent attempts to disable it fail with -EPERM.

Quoted 9 lines
tools/testing/selftests/x86/test_shadow_stack.c +1 -0
index b52c5420c1374..e047f1ceae0a7 100644
@@ -1060,9 +1060,28 @@ int test_ptrace(void)
 	return 1;
 }
 
+static int test_locking(void)

[ ... ]

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

Quoted 6 lines
tools/testing/selftests/x86/test_shadow_stack.c +1 -0
@@ -1079,12 +1098,31 @@ 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)) {

[Severity: Low]
This comment refers to test_shadow_stack_lock(), whereas the helper added and called below is named test_locking().

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

PATCH v5 4/6 x86/shstk: support via prctl

In reply to Bill Roberts

On x86, userspace shadow stack state is managed through the architecture-specific arch_prctl() interface. Other architectures, including arm64 and RISC-V, use the generic shadow stack prctl() interface instead.

This difference requires userspace runtimes, toolchains, and libc implementations to support architecture-specific interfaces for managing shadow stack state. It also complicates efforts to provide consistent LSM mediation of control-flow integrity state changes.

Implement the generic PR_SET_SHADOW_STACK_STATUS, PR_GET_SHADOW_STACK_STATUS, and PR_LOCK_SHADOW_STACK_STATUS operations on x86 using the existing shadow stack helpers. Retain the arch_prctl() interface for compatibility with existing userspace.

Unlike arch_prctl(), which enables or disables individual features, PR_SET_SHADOW_STACK_STATUS specifies the desired state of the supported feature set. Translate this state into the corresponding shadow stack and WRSS operations, changing only features whose state differs from the requested state.

Avoid unnecessary operations because the underlying shadow stack helpers check locked bits before checking whether the requested feature state is already satisfied. If enabling or disabling WRSS fails after enabling shadow stack, restore the original shadow stack state.

Disabling shadow stack also disables WRSS, so do not disable WRSS separately when clearing the entire feature set. This avoids allowing a locked WRSS feature to prevent shadow stack disablement.

Reject unsupported feature combinations and use the existing shadow stack helpers to enforce feature locking.

---
 arch/x86/kernel/shstk.c | 80 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
arch/x86/kernel/shstk.c +80 -0
index eb690ba90180..6dfe310634b0 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,82 @@ bool shstk_is_enabled(void)
 {
 	return features_enabled(ARCH_SHSTK_SHSTK);
 }
+
+/* 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;
+	bool shstk_was_enabled = features_enabled(ARCH_SHSTK_SHSTK);
+
+	/*
+	 * Enable/disable the full feature set and preserve the original state for rollback.
+	 * Only act on changed features, since shstk_prctl() checks locked bits before checking
+	 * feature status.
+	 */
+	switch (status) {
+	case 0:
+		/*
+		 * Do not explicitly disable WRSS. Disabling shadow stack also
+		 * disables WRSS, and a locked WRSS must not prevent this.
+		 */
+		return shstk_was_enabled ?
+			shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK) : 0;
+
+	case PR_SHADOW_STACK_ENABLE:
+		if (!shstk_was_enabled) {
+			rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK);
+			if (rc)
+				return rc;
+		}
+
+		if (features_enabled(ARCH_SHSTK_WRSS)) {
+			rc = shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_WRSS);
+			if (rc && !shstk_was_enabled)
+				shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
+			return rc;
+		}
+
+		return 0;
+
+	case PR_SHADOW_STACK_ENABLE|PR_SHADOW_STACK_WRITE:
+		if (!shstk_was_enabled) {
+			rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK);
+			if (rc)
+				return rc;
+		}
+
+		if (!features_enabled(ARCH_SHSTK_WRSS)) {
+			rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_WRSS);
+			if (rc && !shstk_was_enabled)
+				shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
+
+			return rc;
+		}
+
+		return 0;
+
+	/*
+	 * Unknown bit mapping, unsupported PR_SHADOW_STACK_PUSH or *only*
+	 * PR_SHADOW_STACK_WRITE.
+	 */
+	default:
+		return -EINVAL;
+	}
+}
+
+/* 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 v5 4/6] x86/shstk: support via prctl

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] x86/shstk: missing 32-bit check in prctl status helpers corrupts user stack - [Medium] x86/shstk…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] x86/shstk: missing 32-bit check in prctl status helpers corrupts user stack
- [Medium] x86/shstk: missing X86_FEATURE_USER_SHSTK check in shadow stack prctl helpers
- [Low] x86/shstk: unreachable rollback path in arch_set_shadow_stack_status()
--

commit 9a3ff7cedb6830c75ef0c0aa6dd108c66cfde589
Author: Bill Roberts <[email protected]>

x86/shstk: support via prctl

This commit implements the generic PR_SET_SHADOW_STACK_STATUS, PR_GET_SHADOW_STACK_STATUS, and PR_LOCK_SHADOW_STACK_STATUS prctl() operations on x86 using the existing shadow stack helpers.

Quoted 4 lines
arch/x86/kernel/shstk.c +0 -0
index eb690ba90180c..6dfe310634b02 100644

[ ... ]

Quoted 14 lines
arch/x86/kernel/shstk.c +10 -0
@@ -630,3 +631,82 @@ bool shstk_is_enabled(void)
 {
 	return features_enabled(ARCH_SHSTK_SHSTK);
 }
+
+/* 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;
+	bool shstk_was_enabled = features_enabled(ARCH_SHSTK_SHSTK);
+
+	/*
+	 * Enable/disable the full feature set and preserve the original state for rollback.
+	 * Only act on changed features, since shstk_prctl() checks locked bits before checking

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

Quoted 10 lines
arch/x86/kernel/shstk.c +10 -0
+	 * feature status.
+	 */
+	switch (status) {
+	case 0:
+		/*
+		 * Do not explicitly disable WRSS. Disabling shadow stack also
+		 * disables WRSS, and a locked WRSS must not prevent this.
+		 */
+		return shstk_was_enabled ?
+			shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK) : 0;

[Severity: Medium]
Does returning 0 here when shstk_was_enabled is false, and delegating arch_lock_shadow_stack_status() and arch_get_shadow_stack_status() to shstk_prctl() without checking cpu_feature_enabled(X86_FEATURE_USER_SHSTK), cause these prctl() calls to succeed on CPUs without shadow stack support?

shstk_disable(), shstk_setup(), and wrss_control() all check !cpu_feature_enabled(X86_FEATURE_USER_SHSTK) and return -EOPNOTSUPP, but ARCH_SHSTK_STATUS and ARCH_SHSTK_LOCK in shstk_prctl() do not, and case 0 here bypasses shstk_disable() when shadow stack is not enabled. On systems without X86_FEATURE_USER_SHSTK (or booted with nousershstk), PR_GET_SHADOW_STACK_STATUS, PR_LOCK_SHADOW_STACK_STATUS, and PR_SET_SHADOW_STACK_STATUS(0) return 0 instead of an error, misleading userspace probing for shadow stack support.

Quoted 4 lines
arch/x86/kernel/shstk.c +4 -0
+
+	case PR_SHADOW_STACK_ENABLE:
+		if (!shstk_was_enabled) {
+			rc = shstk_prctl(t, ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK);

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

Quoted 10 lines
arch/x86/kernel/shstk.c +10 -0
+			if (rc)
+				return rc;
+		}
+
+		if (features_enabled(ARCH_SHSTK_WRSS)) {
+			rc = shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_WRSS);
+			if (rc && !shstk_was_enabled)
+				shstk_prctl(t, ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
+			return rc;
+		}

[Severity: Low]
Can the rollback check (if (rc && !shstk_was_enabled)) ever be reached?

wrss_control(true) requires ARCH_SHSTK_SHSTK to be enabled, shstk_disable() clears both ARCH_SHSTK_SHSTK and ARCH_SHSTK_WRSS, and shstk_setup() only enables ARCH_SHSTK_SHSTK. Whenever features_enabled(ARCH_SHSTK_WRSS) is true here, shstk_was_enabled is already true.

[ ... ]

Quoted 6 lines
arch/x86/kernel/shstk.c +6 -0
+/* 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);
+}
+

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

Quoted 10 lines
arch/x86/kernel/shstk.c +10 -0
+/*
+ * 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);
+}

[Severity: High]
Can a 32-bit compat or x32 caller corrupt 4 bytes past its status variable via PR_GET_SHADOW_STACK_STATUS?

Previously, shstk_prctl() was only reachable from sys_arch_prctl(), which rejects in_ia32_syscall() upfront, so shstk_prctl() only checks in_ia32_syscall() inside shstk_setup(). Because sys_prctl() is wired to 32-bit and x32 syscalls, calling prctl(PR_GET_SHADOW_STACK_STATUS, &status, 0, 0, 0) from 32-bit or x32 userspace reaches shstk_prctl(ARCH_SHSTK_STATUS) and executes an 8-byte put_user() into a 4-byte userspace unsigned long:

arch/x86/kernel/shstk.c:shstk_prctl() {
	if (option == ARCH_SHSTK_STATUS) {
		return put_user(task->thread.features, (unsigned long __user *)arg2);
	}
    ...
}

32-bit IA32 callers can also lock features via arch_lock_shadow_stack_status() and call arch_set_shadow_stack_status() to disable shadow stack, toggle WRSS, or get 0 back for status == 0 instead of -EINVAL.

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

PATCH v5 5/6 selftests/x86: support header files in extra-files

In reply to Bill Roberts

The extra-files Makefile macro uses its file list both as additional compiler inputs and as target prerequisites.

This prevents header files from being included in the list, since they would also be passed to the compiler as independent compilation inputs.

Exclude .h files from EXTRA_FILES while retaining them as target prerequisites. This allows tests to track header dependencies and rebuild when those headers change, without attempting to compile them independently.

---
 tools/testing/selftests/x86/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/x86/Makefile +1 -1
index 407f52ac2628..effbfe9b269f 100644
@@ -115,7 +115,7 @@ endif
 # the dependencies ("header3.h"), because clang, unlike gcc, will not accept
 # header files as an input to the compiler invocation.
 define extra-files
-$(OUTPUT)/$(1): EXTRA_FILES := $(2)
+$(OUTPUT)/$(1): EXTRA_FILES := $(filter-out %.h,$(2))
 $(OUTPUT)/$(1): $(2)
 endef
 
Signature · 1 line
2.55.0
#9 lore raw

PATCH v5 6/6 selftests/x86: add generic prctl shadow stack test

In reply to Bill Roberts

The existing x86 shadow stack selftests exercise the architecture-specific arch_prctl() interface but do not cover the generic shadow stack prctl() operations.

Refactor the existing test implementation into a shared source file and provide separate test entry points for arch_prctl() and prctl(). Use common wrappers to translate shadow stack operations into the appropriate syscall interface.

Run the same shadow stack test suite against both interfaces to verify consistent behavior, including feature enablement, disablement, status queries, and locking.

Sharing the test implementation avoids duplicating the test logic and helps detect behavioral differences between the interfaces as they evolve.

---
 tools/testing/selftests/x86/Makefile          |    4 +-
 tools/testing/selftests/x86/shadow_stack.c    | 1287 +++++++++++++++++
 tools/testing/selftests/x86/shadow_stack.h    |   14 +
 .../testing/selftests/x86/test_shadow_stack.c | 1210 +---------------
 .../selftests/x86/test_shadow_stack_prctl.c   |   12 +
 5 files changed, 1322 insertions(+), 1205 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 +3 -1
index effbfe9b269f..48c745909161 100644
@@ -20,7 +20,7 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			vdso_restorer
 TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
 			corrupt_xstate_header amx lam test_shadow_stack avx apx \
-			sigframe_fpu_portability
+			sigframe_fpu_portability test_shadow_stack test_shadow_stack_prctl
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
@@ -128,6 +128,8 @@ $(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,apx_64,xstate.c))
+$(eval $(call extra-files,test_shadow_stack_64,shadow_stack.c shadow_stack.h))
+$(eval $(call extra-files,test_shadow_stack_prctl_64,shadow_stack.c shadow_stack.h))
 
 # 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 +6 -1204

1,219 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
#10 lore raw

Re: [PATCH v5 6/6] selftests/x86: add generic prctl shadow stack test

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/x86: duplicate test_shadow_stack entry in TARGETS_C_64BIT_ONLY --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/x86: duplicate test_shadow_stack entry in TARGETS_C_64BIT_ONLY
--

commit 52e57f66207c4b662bab2432ff697c9236a6020d
Author: Bill Roberts <[email protected]>

selftests/x86: add generic prctl shadow stack test

This commit refactors the x86 shadow stack selftest into a shared implementation and adds a test_shadow_stack_prctl entry point to exercise the generic prctl() shadow stack operations alongside arch_prctl().

tools/testing/selftests/x86/Makefile TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ · line 23

Quoted 10 lines
tools/testing/selftests/x86/Makefile +1 -1
index effbfe9b269f3..48c7459091614 100644
@@ -20,7 +20,7 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			vdso_restorer
 TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
 			corrupt_xstate_header amx lam test_shadow_stack avx apx \
-			sigframe_fpu_portability
+			sigframe_fpu_portability test_shadow_stack test_shadow_stack_prctl

[Severity: Low]
Does adding test_shadow_stack here duplicate the existing entry on the previous line of TARGETS_C_64BIT_ONLY? Because BINARIES_64 and TEST_PROGS expand TARGETS_C_64BIT_ONLY without deduplication, make warns about duplicate target recipes for test_shadow_stack_64 and kselftest runs the binary twice.

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