Skip to contentSkip to conversation

arch/x86: handle ARCH_SHSTK_ENABLE explicitly

PATCH Superseded Bill Roberts 2 participants 7 messages Jul 7 – Sep 9

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

PATCH v2 1/2 arch/x86: handle ARCH_SHSTK_ENABLE explicitly

The arch_prctl() handling for shadow stack operations checks that the operation being specified is a specific value, except for the ARCH_SHSTK_ENABLE condition, which is treated like a default case in a switch.

However, the special handling for ARCH_SHSTK_UNLOCK is only performed when task != current. As a result, an ARCH_SHSTK_UNLOCK request for the current task bypasses the unlock check and falls through to the ARCH_SHSTK_ENABLE path, causing the request to be interpreted as an enable operation instead.

To fix this, handle ARCH_SHSTK_ENABLE explicitly rather than relying on it as the default case. This fixes the incorrect dispatch and makes the operation handling more robust by requiring each operation to be matched explicitly.

---
 arch/x86/kernel/shstk.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kernel/shstk.c +7 -5
index 0ca64900192f..65c896d02379 100644
@@ -607,11 +607,13 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long arg2)
 		return -EINVAL;
 	}
 
-	/* Handle ARCH_SHSTK_ENABLE */
-	if (features & ARCH_SHSTK_SHSTK)
-		return shstk_setup();
-	if (features & ARCH_SHSTK_WRSS)
-		return wrss_control(true);
+	if (option == ARCH_SHSTK_ENABLE) {
+		if (features & ARCH_SHSTK_SHSTK)
+			return shstk_setup();
+		if (features & ARCH_SHSTK_WRSS)
+			return wrss_control(true);
+	}
+
 	return -EINVAL;
 }
 
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 2/2 selftest/x86: test ARCH_SHSTK_UNLOCK

Test that ARCH_SHSTK_UNLOCK can't be called from normal (non-ptrace) context.

---
 tools/testing/selftests/x86/test_shadow_stack.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/testing/selftests/x86/test_shadow_stack.c +8 -0
index 3d6ca33edba4..cbe7ca4ebd90 100644
@@ -1059,6 +1059,14 @@ int main(int argc, char *argv[])
 {
 	int ret = 0;
 
+	/* Test that ARCH_SHSTK_UNLOCK can't be called from normal (non-ptrace) context. */
+	if (!ARCH_PRCTL(ARCH_SHSTK_UNLOCK, ARCH_SHSTK_SHSTK)) {
+		printf("[FAIL]\tARCH_SHSTK_UNLOCK should fail in a non-ptrace context\n");
+		goto out;
+	}
+
+	printf("[OK]\tCouldn't enable Shadow stack via UNLOCK\n");
+
 	if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK)) {
 		printf("[SKIP]\tCould not enable Shadow stack\n");
 		return 1;
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH v2 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK

Quoted 7 lines · Rick P
Quoted 5 lines · Bill Roberts

Test that ARCH_SHSTK_UNLOCK can't be called from normal (non-ptrace) context.

---

Rick/Dave, bump, any love on this patch series? Thanks.

Lore links for convenience:

- Re: [PATCH v2 1/2] arch/x86: handle ARCH_SHSTK_ENABLE explicitly
  - https://lore.kernel.org/all/[email protected]/
- Re: [PATCH v2 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK
  - https://lore.kernel.org/all/[email protected]/

#4 lore raw

Re: [PATCH v2 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK

Quoted 1 line · Bill Roberts

Rick

I replied. Is there something else you need?

#5 lore raw

Re: [PATCH v2 2/2] selftest/x86: test ARCH_SHSTK_UNLOCK

In reply to Edgecombe, Rick P
Quoted 7 lines · Rick P
Quoted 5 lines · Bill Roberts

Test that ARCH_SHSTK_UNLOCK can't be called from normal (non-ptrace) context.

---

Bump, I don't see these acked/staged anywhere.

Links in case you need the patches handy:

1. https://lore.kernel.org/all/[email protected]/
2. https://lore.kernel.org/all/[email protected]/

#6 lore raw
In reply to Bill Roberts
Quoted 4 lines · Bill Roberts

The arch_prctl() handling for shadow stack operations checks that the operation being specified is a specific value, except for the ARCH_SHSTK_ENABLE condition, which is treated like a default case in a switch.

Seems a bit unspecific "checks that the operation being specified is a specific value".

Quoted 11 lines

However, the special handling for ARCH_SHSTK_UNLOCK is only performed when task != current. As a result, an ARCH_SHSTK_UNLOCK request for the current task bypasses the unlock check and falls through to the ARCH_SHSTK_ENABLE path, causing the request to be interpreted as an enable operation instead.

To fix this, handle ARCH_SHSTK_ENABLE explicitly rather than relying on it as the default case. This fixes the incorrect dispatch and makes the operation handling more robust by requiring each operation to be matched explicitly.

I don't know if maintainers will be happy with the log. I'd have written it like this. But no warranty:

On x86 user shadow stack is controlled via an arch_prctl() interface, in order to allow ptrace to actuate some of the options, and allow normal syscall execution of arch_prctl() to execute others. Only valid shadow stack options, for both ptrace and normal userspace callers, are directed into the shstk_prctl() handler via checking in do_arch_prctl_64(). Then in shstk_prctl(), it differentiates ptrace callers by checking if task != current.

The trouble starts when the handling of the task == current (normal syscall) case treats the enabling option, ARCH_SHSTK_ENABLE, like a default case. It reasons that shstk_prctl() has handled all other possible shadow stack options, so ARCH_SHSTK_ENABLE can be the only remaining. Or so it thinks. Because the ARCH_SHSTK_UNLOCK option is only handled in the ptrace case. Since it has no handler in the syscall case, ARCH_SHSTK_UNLOCK can get misinterpreted as ARCH_SHSTK_ENABLE by the erroneous default case logic.

The impact is that doing an arch_prtcl(ARCH_SHSTK_UNLOCK) syscall actually behaves like an arch_prtcl(ARCH_SHSTK_ENABLE) syscall. This is a surprising behavior, but does not expose any control to userspace that it doesn't already have.

So drop the buggy default assumptions by checking for ARCH_SHSTK_ENABLE explicitly.

Quoted 3 lines

---

Fix looks good. I think it's correct that this isn't marked for stable.

Quoted 26 lines
 arch/x86/kernel/shstk.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kernel/shstk.c +7 -5
index 0ca64900192f..65c896d02379 100644
@@ -607,11 +607,13 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long arg2)
 		return -EINVAL;
 	}
 
-	/* Handle ARCH_SHSTK_ENABLE */
-	if (features & ARCH_SHSTK_SHSTK)
-		return shstk_setup();
-	if (features & ARCH_SHSTK_WRSS)
-		return wrss_control(true);
+	if (option == ARCH_SHSTK_ENABLE) {
+		if (features & ARCH_SHSTK_SHSTK)
+			return shstk_setup();
+		if (features & ARCH_SHSTK_WRSS)
+			return wrss_control(true);
+	}
+
 	return -EINVAL;
 }