Skip to contentSkip to conversation

[PATCH v4 0/6]

PATCH Superseded Bill Roberts 2 participants 8 messages Sep 15

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover [PATCH v4 0/6]

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

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.

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
- v4:
- fix rollback state logic

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                       |   83 ++
 tools/testing/selftests/x86/Makefile          |   13 +-
 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, 1411 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 v4 1/6 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 | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/x86/Makefile +3 -3
index 434065215d12..75315f1190d6 100644
@@ -124,6 +124,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
@@ -135,6 +138,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

PATCH v4 2/6 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
#3 lore raw

PATCH v4 3/6 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
#4 lore raw

PATCH v4 4/6 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 | 83 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
arch/x86/kernel/shstk.c +83 -0
index 0ca64900192f..249c4998732a 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,85 @@ 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);
+
+	/*
+	 * One must explicitly perform enable/disable calls for the whole feature set so
+	 * that the locked bits are checked. It is also important to capture entry state
+	 * for roll-back if needed. However, we must NOT try to enable or disable a feature
+	 * unless there is a change in feature status from old to new state. This is
+	 * because shstk_prctl(), correctly, checks locked bits before feature enabled/disabled
+	 * short circuit.
+	 */
+	switch (status) {
+	case 0:
+		/*
+		 * We do NOT explicitly disable WRSS here, disabling shadow stack will disable WRSS,
+		 * and we don't want a locked WRSS to prevent disable.
+		 */
+		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
#5 lore raw

Re: [PATCH v4 4/6] x86/shstk: support via prctl

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] x86/shstk: arch_set_shadow_stack_status() evaluates current instead of target task t - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] x86/shstk: arch_set_shadow_stack_status() evaluates current instead of target task t
- [Low] x86/shstk: unreachable rollback logic in arch_set_shadow_stack_status() --

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

x86/shstk: support via prctl

This commit wires up the generic prctl handlers for shadow stack on x86, mapping PR_SET_SHADOW_STACK_STATUS and related operations to the internal CET helper routines. This allows userspace to manage shadow stacks via a cross-architecture interface while maintaining compatibility with arch_prctl.

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

[ ... ]

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

Quoted 10 lines
arch/x86/kernel/shstk.c +6 -0
@@ -630,3 +631,85 @@ 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);

[Severity: Medium]
Does this code incorrectly evaluate the shadow stack status of current instead of the target task t?

The features_enabled() helper strictly evaluates the features of the current task via current->thread.features. If arch_set_shadow_stack_status() is ever called to configure a remote task's shadow stack where t != current, it will evaluate the caller's state rather than the target's state. This could potentially short-circuit and silently fail to configure the target task.

Quoted 10 lines
arch/x86/kernel/shstk.c +10 -0
+
+	/*
+	 * One must explicitly perform enable/disable calls for the whole feature set so
+	 * that the locked bits are checked. It is also important to capture entry state
+	 * for roll-back if needed. However, we must NOT try to enable or disable a feature
+	 * unless there is a change in feature status from old to new state. This is
+	 * because shstk_prctl(), correctly, checks locked bits before feature enabled/disabled
+	 * short circuit.
+	 */
+	switch (status) {

[ ... ]

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

Quoted 8 lines
arch/x86/kernel/shstk.c +8 -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)) {

[Severity: Medium]
Will this again erroneously evaluate the WRSS feature state of current instead of the target task t?

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

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

[Severity: Low]
Is this rollback for ARCH_SHSTK_SHSTK unreachable?

ARCH_SHSTK_WRSS can only be enabled if ARCH_SHSTK_SHSTK is already enabled. Because of this, if features_enabled(ARCH_SHSTK_WRSS) evaluates to true allowing entry into this block, shstk_was_enabled must also be true. Therefore, !shstk_was_enabled is always false in this context, making this rollback structurally dead code.

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

Quoted 13 lines
arch/x86/kernel/shstk.c +13 -0
+			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)) {

[Severity: Medium]
Will this also erroneously evaluate the WRSS feature state of current instead of the target task t?

Quoted 8 lines
arch/x86/kernel/shstk.c +8 -0
+			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;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

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

In reply to Bill Roberts

The Makefile dependencies do not include header files, and adding header files to extra-files calls will cause the header to be passed to the compiler to compile, which is not what we want. So filter out .h files from EXTRA_FILES but keep them for the target's prerequisites.

---
 tools/testing/selftests/x86/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/x86/Makefile +1 -1
index 75315f1190d6..ac181b065f37 100644
@@ -114,7 +114,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
#7 lore raw

PATCH v4 6/6 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 ac181b065f37..64d9057297ef 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
 
@@ -127,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 +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