x86/shstk: don't leak a shadow stack when clone TLS fails
Subsystems (3)
cover x86/shstk: don't leak a shadow stack when clone TLS fails
copy_thread() maps a shadow stack and set_new_tls() can still fail. That error goes to bad_fork_cleanup_io and skips exit_thread(). bad_fork_cleanup_thread calls exit_thread() only after copy_thread() has returned 0.
commit 748c90c69336 ("x86/shstk: Remove useless clone error handling") removed the shstk_free() on the set_new_tls() failure. Restoring it only there puts back the single free. The allocation-error return still aliases the parent, and the exit_thread() path is left alone, so the double unmap that commit avoided does not come back.
The selftest assumes /proc is mounted. A clone3() with a valid TLS must succeed before EPERM counts as the TLS failure. No CPUID support, no kernel support, or a locked feature skips the test. Any other ARCH_SHSTK_ENABLE error fails it.
Sixteen clone3() calls with CLONE_VM|CLONE_SETTLS and an out-of-range TLS leaked 16 shadow-stack VMAs without the fix. With it, the count stayed unchanged and call/return on the parent still used the original shadow stack. Tested with user shadow stack on the host and in a one-vCPU KVM guest (-cpu host), same configuration with and without the fix.
The diagnosis and the selftest were drafted with Grok 4.7.
Iván Ezequiel Rodriguez (2):
x86/shstk: free shadow stack when copy_thread() fails selftests/x86: shadow stack must survive a bad TLS
arch/x86/kernel/process.c | 12 +- tools/testing/selftests/x86/Makefile | 3 +- tools/testing/selftests/x86/shstk_clone_tls.c | 364 ++++++++++++++++++ 3 files changed, 376 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/x86/shstk_clone_tls.c
Signature · 1 line
2.43.0
PATCH 1/2 x86/shstk: free shadow stack when copy_thread() fails
copy_thread() maps a shadow stack for a CLONE_VM child and can still fail in set_new_tls(). That error returns through bad_fork_cleanup_io, which does not call exit_thread(). bad_fork_cleanup_thread does, but only after copy_thread() has returned 0.
commit 748c90c69336 ("x86/shstk: Remove useless clone error handling") removed the shstk_free() on this path, assuming exit_thread() would run. It does not. Each rejected clone leaves one shadow-stack VMA in the parent's mm until the mm dies: one map_count slot, pages not populated. The size follows the clone stack, or RLIMIT_STACK when that size is zero.
Free only after set_new_tls() fails. The allocation error returns earlier, while the child's base still aliases the parent. shstk_free() ignores a cleared base and a different mm, so CLONE_VFORK and !CLONE_VM stay no-ops. The exit_thread() path is left alone, so the double unmap that commit avoided does not come back. The io bitmap is still shared only when TLS setup succeeds.
Tested with user shadow stack enabled, on one config with and without this change. Sixteen clone3() calls with CLONE_VM|CLONE_SETTLS and an out-of-range TLS leaked 16 shadow-stack VMAs without the fix. With it, the VMA count was unchanged and call/return on the parent still used the original shadow stack.
--- arch/x86/kernel/process.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kernel/process.c +10 -2
@@ -265,11 +265,19 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)/* Set a new TLS for the child thread? */ if (clone_flags &CLONE_SETTLS ) ret =set_new_tls (p, tls); +if (ret) { +/* +* copy_process() skips exit_thread() when copy_thread() +* fails. Free the shadow stack mapped before this error. +*/ +shstk_free (p); +return ret; + } -if (!ret &&unlikely (test_tsk_thread_flag (current,TIF_IO_BITMAP ))) +if (unlikely (test_tsk_thread_flag (current,TIF_IO_BITMAP )))io_bitmap_share (p); -return ret; +return 0 ; }static void pkru_flush_thread (void )
Signature · 1 line
2.43.0
Re: [PATCH 1/2] x86/shstk: free shadow stack when copy_thread() fails
By inspection the bug looks real to me.
arch/x86/kernel/process.c int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) · line 268
Quoted 9 lines · Iván Ezequiel Rodriguez
arch/x86/kernel/process.c +1
-0@@ -265,11 +265,19 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) /* Set a new TLS for the child thread? */ if (clone_flags & CLONE_SETTLS) ret = set_new_tls(p, tls); +if (ret) {
Having the error check happen even if !CLONE_SETTLS makes it not clear that this condition could only happen that way. I'd maybe nest the check inside, like is done in several cases in in copy_process(). Like (untested):
arch/x86/kernel/process.c +0 -0
@@ -176,7 +176,7 @@ int copy_thread(struct task_struct *p, const struct
kernel_clone_args *args)
struct fork_frame *fork_frame;
struct pt_regs *childregs;
unsigned long new_ssp;- int ret = 0;
+ int ret;
childregs = task_pt_regs(p);
fork_frame = container_of(childregs, struct fork_frame, regs);
@@ -263,13 +263,19 @@ int copy_thread(struct task_struct *p, const struct
kernel_clone_args *args)
}
/* Set a new TLS for the child thread? */
- if (clone_flags & CLONE_SETTLS)
+ if (clone_flags & CLONE_SETTLS) {
ret = set_new_tls(p, tls);+ if (ret)
+ goto shstk_free;
+ }
- if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP)))
+ if (unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP)))
io_bitmap_share(p);
return ret;+shstk_free:
+ shstk_free(p);
+ return ret;
}
static void pkru_flush_thread(void)
arch/x86/kernel/process.c int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) · line 272
Quoted 4 lines
arch/x86/kernel/process.c +4
-0+/* +* copy_process() skips exit_thread() when copy_thread() +* fails. Free the shadow stack mapped before this error. +*/
I'm not in favor of this comment. It is describing behavior far away. And the idea that you cleanup what you allocated in on a later error condition is a pretty common pattern. I tried to write something and nothing seems non-obvious. Maybe just drop it.
Quoted 11 lines
arch/x86/kernel/process.c +3
-0+shstk_free (p); +return ret; + }arch/x86/kernel/process.c +1
-1-if (!ret &&unlikely (test_tsk_thread_flag (current,TIF_IO_BITMAP ))) +if (unlikely (test_tsk_thread_flag (current,TIF_IO_BITMAP )))
io_bitmap_share(p);arch/x86/kernel/process.c +1
-1-return ret; +return 0 ;}
PATCH 2/2 selftests/x86: shadow stack must survive a bad TLS
In reply to Iván Ezequiel Rodriguezclone3(CLONE_VM|CLONE_SETTLS) with an out-of-range TLS fails in set_new_tls() after copy_thread() has mapped a shadow stack. copy_process() does not call exit_thread() on that failure, so the VMA stays in the parent's mm.
Enable user shadow stack, reject 16 such clones, and require the shadow-stack VMA count to stay unchanged. The original mapping must remain, and a call/return on that stack must succeed, so the cleanup cannot be unmapping the parent's shadow stack.
A clone3 with the same flags and a valid TLS has to succeed first. EPERM from the rejected calls is not evidence on its own: a policy can reject every clone3(). The child exits with a raw syscall because it resumes on an empty shadow stack.
Skip when CPUID has no shadow stack, when the kernel does not provide the feature, or when it is locked. Any other ARCH_SHSTK_ENABLE error fails the test. The test expects /proc already mounted, as in a normal kselftest run.
The recursive call goes through an indirect call. A visible recursive body is optimized into a closed form and does not execute ret.
--- tools/testing/selftests/x86/Makefile | 3 +- tools/testing/selftests/x86/shstk_clone_tls.c | 364 ++++++++++++++++++ 2 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/shstk_clone_tls.c
tools/testing/selftests/x86/Makefile +2 -1
@@ -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 \ + shstk_clone_tls # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
tools/testing/selftests/x86/shstk_clone_tls.c new file +364 -0
364 lines not on this page · raw message
Signature · 1 line
2.43.0
v2 cover x86/shstk: don't leak a shadow stack when clone TLS fails
In reply to Iván Ezequiel Rodriguezcopy_thread() maps a shadow stack and set_new_tls() can still fail. That error goes to bad_fork_cleanup_io and skips exit_thread(). bad_fork_cleanup_thread calls exit_thread() only after copy_thread() has returned 0.
commit 748c90c69336 ("x86/shstk: Remove useless clone error handling") removed the shstk_free() on the set_new_tls() failure. Restoring it only there puts back the single free. The allocation-error return still aliases the parent, and the exit_thread() path is left alone, so the double unmap that commit avoided does not come back.
The selftest uses kselftest.h / KTAP. Feature and clone3 availability skips run before ksft_set_plan(). Missing /proc/self/smaps is SKIP. Read errors, truncated smaps lines, and a VMA range without VmFlags are FAIL. A valid-TLS clone3 must succeed before EPERM on a bad TLS counts as the leak path; ENOSYS/EPERM/EACCES SKIP that control.
Regression (CONFIG_X86_USER_SHADOW_STACK=y, host and one-vCPU KVM -cpu host,migratable=off):
Scenario Without fix With fix 16 bad-TLS clone3 +16 ss VMAs +0 ss VMAs Parent shadow stack kept + call/ret OK kept + call/ret OK KTAP 2/3 (test 3 fail) 3/3 pass
Changes in v2:
- Nest the TLS error check under CLONE_SETTLS, as suggested by Rick Edgecombe.
- Use a dedicated shadow-stack cleanup label.
- Return zero explicitly on the successful path.
- Remove the redundant cleanup comment.
- Selftest: rewrite on kselftest.h / KTAP (plan 3). Fix RDSSPQ comment; align get_ssp() with test_shadow_stack; SKIP only ENOSYS/EPERM/EACCES on the valid-TLS control; FAIL on truncated or incomplete smaps records that would under-count ss VMAs.
Iván Ezequiel Rodriguez (2):
x86/shstk: free shadow stack when copy_thread() fails selftests/x86: shadow stack must survive a bad TLS
arch/x86/kernel/process.c | 13 +- tools/testing/selftests/x86/Makefile | 3 +- tools/testing/selftests/x86/shstk_clone_tls.c | 334 ++++++++++++++++++ 3 files changed, 346 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/x86/shstk_clone_tls.c
Signature · 1 line
2.43.0
PATCH v2 1/2 x86/shstk: free shadow stack when copy_thread() fails
copy_thread() maps a shadow stack for a CLONE_VM child and can still fail in set_new_tls(). That error returns through bad_fork_cleanup_io, which does not call exit_thread(). bad_fork_cleanup_thread does, but only after copy_thread() has returned 0.
commit 748c90c69336 ("x86/shstk: Remove useless clone error handling") removed the shstk_free() on this path, assuming exit_thread() would run. It does not. Each rejected clone leaves one shadow-stack VMA in the parent's mm until the mm dies: one map_count slot, pages not populated. The size follows the clone stack, or RLIMIT_STACK when that size is zero.
Free only after set_new_tls() fails, via a dedicated cleanup label. Nest the TLS error check under CLONE_SETTLS so the successful path returns 0 explicitly. The allocation error returns earlier, while the child's base still aliases the parent. shstk_free() ignores a cleared base and a different mm, so CLONE_VFORK and !CLONE_VM stay no-ops. The exit_thread() path is left alone, so the double unmap that commit avoided does not come back. The io bitmap is still shared only when TLS setup succeeds.
Tested with user shadow stack enabled, on one config with and without this change. Sixteen clone3() calls with CLONE_VM|CLONE_SETTLS and an out-of-range TLS leaked 16 shadow-stack VMAs without the fix. With it, the VMA count was unchanged and call/return on the parent still used the original shadow stack.
--- arch/x86/kernel/process.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +10 -3
@@ -176,7 +176,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)struct fork_frame *fork_frame;struct pt_regs *childregs;unsigned long new_ssp; -int ret =0 ; +int ret; childregs =task_pt_regs (p); fork_frame = container_of(childregs, struct fork_frame, regs);
@@ -263,12 +263,19 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) }/* Set a new TLS for the child thread? */ -if (clone_flags &CLONE_SETTLS ) +if (clone_flags &CLONE_SETTLS ) { ret =set_new_tls (p, tls); +if (ret) + goto shstk_free; + } -if (!ret &&unlikely (test_tsk_thread_flag (current,TIF_IO_BITMAP ))) +if (unlikely (test_tsk_thread_flag (current,TIF_IO_BITMAP )))io_bitmap_share (p); +return 0 ; + +shstk_free: +shstk_free (p);return ret; }
Signature · 1 line
2.43.0
PATCH v2 2/2 selftests/x86: shadow stack must survive a bad TLS
In reply to Iván Ezequiel Rodriguezclone3(CLONE_VM|CLONE_SETTLS) with an out-of-range TLS fails in set_new_tls() after copy_thread() has mapped a shadow stack. copy_process() does not call exit_thread() on that failure, so the VMA stays in the parent's mm.
Use kselftest.h / KTAP. Skip paths that cannot run the regression run before ksft_set_plan(). Then confirm a valid clone3, that sixteen bad-TLS clones return -EPERM, and that the shadow-stack VMA count is unchanged while the parent's stack still supports call/return.
SKIP the valid-TLS control only for ENOSYS, EPERM, or EACCES. Missing /proc/self/smaps (ENOENT) is SKIP. Read errors, truncated lines, and VMA records without VmFlags are FAIL so under-counting cannot hide a leak. Skip when CPUID has no shadow stack or when user SHSTK cannot be enabled. The child exits with a raw syscall because it resumes on an empty shadow stack.
The recursive call goes through an indirect call. A visible recursive body is optimized into a closed form and does not execute ret.
--- tools/testing/selftests/x86/Makefile | 3 +- tools/testing/selftests/x86/shstk_clone_tls.c | 334 ++++++++++++++++++ 2 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/shstk_clone_tls.c
tools/testing/selftests/x86/Makefile +2 -1
@@ -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 \ + shstk_clone_tls # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
tools/testing/selftests/x86/shstk_clone_tls.c new file +334 -0
334 lines not on this page · raw message
Signature · 1 line
2.43.0