arm64, riscv: release shadow stacks on failed fork
Subsystems (4)
v3 cover arm64, riscv: release shadow stacks on failed fork
clone3(CLONE_VM|CLONE_PIDFD) can fail in put_user() after copy_thread() has mapped a shadow stack into the shared mm. copy_process() then calls exit_thread(). arm64 and riscv did not implement it, so the VMA leaked.
x86 already frees via exit_thread() -> shstk_free(). Its separate TLS leak is when copy_thread() fails after the alloc and skips exit_thread().
Changes since v2:
- Move gcs_reset_thread() into process.c (Mark Brown).
- Spell out the x86 vs arm64/riscv difference above.
v2: https://lore.kernel.org/all/[email protected]/
Iván Ezequiel Rodriguez (4):
arm64/gcs: release GCS on failed fork
riscv/shstk: release shadow stack on failed fork selftests/arm64: GCS must not leak on a failed clone3 selftests/riscv: shadow stack must not leak on a failed clone3
arch/arm64/Kconfig | 1 + arch/arm64/kernel/process.c | 16 ++ arch/riscv/Kconfig | 1 + arch/riscv/kernel/process.c | 9 + tools/testing/selftests/arm64/gcs/Makefile | 3 +- .../selftests/arm64/gcs/gcs-clone-pidfd.c | 168 +++++++++++++++++ .../testing/selftests/arm64/gcs/gcs-locking.c | 20 -- tools/testing/selftests/arm64/gcs/gcs-util.h | 24 +++ tools/testing/selftests/arm64/gcs/libc-gcs.c | 20 -- tools/testing/selftests/riscv/cfi/Makefile | 5 +- .../selftests/riscv/cfi/shstk-clone-pidfd.c | 173 ++++++++++++++++++ 11 files changed, 398 insertions(+), 42 deletions(-) create mode 100644 tools/testing/selftests/arm64/gcs/gcs-clone-pidfd.c create mode 100644 tools/testing/selftests/riscv/cfi/shstk-clone-pidfd.c
Signature · 1 line
2.43.0
PATCH v3 1/4 arm64/gcs: release GCS on failed fork
copy_thread() can map a CLONE_VM GCS and still return 0. A later copy_process() failure calls exit_thread(), but arm64 did not implement it, so the VMA stayed in the parent's mm. Wire HAVE_EXIT_THREAD to gcs_free().
x86 already does this via exit_thread() -> shstk_free(). The separate x86 TLS leak is different: copy_thread() itself fails after the alloc, so copy_process() skips exit_thread().
args->fn tasks inherit GCS tracking without allocating; clear it so a failed CLONE_VM copy_process() cannot gcs_free() the caller's stack. Keep that helper in process.c with the other fork helpers.
--- arch/arm64/Kconfig | 1 + arch/arm64/kernel/process.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -177,6 +177,7 @@ config ARM64 select HAVE_ARM_SMCCC select HAVE_ASM_MODVERSIONS select HAVE_EBPF_JIT + select HAVE_EXIT_THREAD select HAVE_C_RECORDMCOUNT select HAVE_CMPXCHG_DOUBLE select HAVE_CMPXCHG_LOCAL
arch/arm64/kernel/process.c +16 -0
@@ -320,6 +320,13 @@ static int copy_thread_gcs(struct task_struct *p, return 0; } +static void gcs_reset_thread (struct task_struct *task) +{ + task->thread.gcs_base =0 ; + task->thread.gcs_size =0 ; + task->thread.gcspr_el0 =0 ; +} +#else static void flush_gcs (void ) { }
@@ -328,6 +335,7 @@ static int copy_thread_gcs(struct task_struct *p, {return 0 ; } +static void gcs_reset_thread (struct task_struct *task) { }#endif
@@ -341,6 +349,12 @@ void flush_thread(void)flush_gcs (); } +void exit_thread (struct task_struct *tsk) +{ +/* Failed fork after copy_thread(); deactivate_mm() does not run. */ +gcs_free (tsk); +} +void arch_release_task_struct (struct task_struct *tsk) {fpsimd_release_task (tsk);
@@ -503,6 +517,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.cpu_context.x19 = (unsigned long )args->fn; p->thread.cpu_context.x20 = (unsigned long )args->fn_arg; +/* No user GCS alloc here; drop inherited tracking for CLONE_VM. */ +gcs_reset_thread (p);if (system_supports_poe ()) p->thread.por_el0 =POR_EL0_INIT ;
Signature · 1 line
2.43.0
Re: [PATCH v3 1/4] arm64/gcs: release GCS on failed fork
Quoted 4 lines · Iván Ezequiel Rodriguez
copy_thread()can map aCLONE_VMGCS and still return 0. A latercopy_process()failure callsexit_thread(), but arm64 did not implement it, so the VMA stayed in the parent's mm. WireHAVE_EXIT_THREADtogcs_free().
One extremely minor nit below, I verified that this fails with the current kernel and that the kernel change causes it to pass so with or without dealing with that:
arch/arm64/kernel/process.c static int copy_thread_gcs(struct task_struct *p, · line 339
Quoted 6 lines
arch/arm64/kernel/process.c +1
-0@@ -328,6 +335,7 @@ static int copy_thread_gcs(struct task_struct *p, {return 0 ; } +static void gcs_reset_thread (struct task_struct *task) { }
This looks like there's a missing blank line but it fits in with the missing blank before the stub for copy_thread_gcs(). Possibly slightly neater to put this before there next to the similar flush_gcs() stub but meh.
Catalin Marinas
maintainerRe: [PATCH v3 1/4] arm64/gcs: release GCS on failed fork
In reply to Iván Ezequiel RodriguezQuoted 8 lines · Iván Ezequiel Rodriguez
copy_thread()can map aCLONE_VMGCS and still return 0. A latercopy_process()failure callsexit_thread(), but arm64 did not implement it, so the VMA stayed in the parent's mm. WireHAVE_EXIT_THREADtogcs_free().x86 already does this via
exit_thread()->shstk_free(). The separate x86 TLS leak is different:copy_thread()itself fails after the alloc, socopy_process()skipsexit_thread().
Is the x86 TLS leak relevant to arm64?
[...]
arch/arm64/kernel/process.c int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) · line 521
Quoted 6 lines
arch/arm64/kernel/process.c +2
-0@@ -503,6 +517,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.cpu_context.x19 = (unsigned long )args->fn; p->thread.cpu_context.x20 = (unsigned long )args->fn_arg; +/* No user GCS alloc here; drop inherited tracking for CLONE_VM. */ +gcs_reset_thread (p);
My LLM run noticed that this fixes another problem, unrelated to the failed fork: io_uring and vhost workers are created with args->fn and CLONE_VM and they inherit gcs_base/gcs_size from the creating task through arch_dup_task_struct(). Without this reset, a worker exiting normally unmaps the parent task's GCS.
I was wondering whether to have a separate one-line commit for the worker thread case but I think I'll just add a comment in the commit log, keep things simple.
Signature · 1 line
Catalin
Re: [PATCH v3 1/4] arm64/gcs: release GCS on failed fork
Quoted 7 lines · Catalin Marinas
Quoted 3 lines · Iván Ezequiel Rodriguez
x86 already does this via
exit_thread()->shstk_free(). The separate x86 TLS leak is different:copy_thread()itself fails after the alloc, socopy_process()skipsexit_thread().Is the x86 TLS leak relevant to arm64?
That was in response to me querying if they were affected on prior versions I think.
PATCH v3 2/4 riscv/shstk: release shadow stack on failed fork
In reply to Iván Ezequiel RodriguezSame hole as arm64: copy_thread() maps a CLONE_VM shadow stack, copy_process() fails later, exit_thread() ran but was never implemented. Select HAVE_EXIT_THREAD and call shstk_release().
shstk_release() already documented this path; connect it. Clear inherited tracking on the args->fn path for the same CLONE_VM reason as arm64.
--- arch/riscv/Kconfig | 1 + arch/riscv/kernel/process.c | 9 +++++++++ 2 files changed, 10 insertions(+)
arch/riscv/Kconfig +1 -0
@@ -168,6 +168,7 @@ config RISCV select HAVE_FUNCTION_GRAPH_FREGS select HAVE_FUNCTION_TRACER if HAVE_DYNAMIC_FTRACE select HAVE_EBPF_JIT if MMU + select HAVE_EXIT_THREAD select HAVE_GENERIC_TIF_BITS select HAVE_GUP_FAST if MMU select HAVE_FUNCTION_ARG_ACCESS_API
arch/riscv/kernel/process.c +9 -0
@@ -12,6 +12,7 @@#include <linux/kernel.h> #include <linux/sched.h> #include <linux/sched/debug.h> +#include <linux/sched/task.h> #include <linux/sched/task_stack.h> #include <linux/tick.h> #include <linux/ptrace.h>
@@ -213,6 +214,12 @@ void arch_release_task_struct(struct task_struct *tsk)riscv_v_thread_free (tsk); } +void exit_thread (struct task_struct *tsk) +{ +/* Failed fork after copy_thread(); deactivate_mm() does not run. */ +shstk_release (tsk); +} +int arch_dup_task_struct (struct task_struct *dst,struct task_struct *src) {fstate_save (src,task_pt_regs (src));
@@ -261,6 +268,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.s[0 ] = (unsigned long )args->fn; p->thread.s[1 ] = (unsigned long )args->fn_arg; p->thread.ra = (unsigned long )ret_from_fork_kernel_asm; +/* No user shstk alloc here; drop inherited tracking for CLONE_VM. */ +set_shstk_base (p,0 ,0 ); }else {/* allocate new shadow stack if needed. In case of CLONE_VM we have to */ ssp =shstk_alloc_thread_stack (p, args);
Signature · 1 line
2.43.0
PATCH v3 3/4 selftests/arm64: GCS must not leak on a failed clone3
In reply to Iván Ezequiel RodriguezKTAP coverage for clone3(CLONE_VM|CLONE_PIDFD) with a bad pidfd
pointer: sixteen failures must not grow the parent's GCS VMA count.
--- tools/testing/selftests/arm64/gcs/Makefile | 3 +- .../selftests/arm64/gcs/gcs-clone-pidfd.c | 168 ++++++++++++++++++ .../testing/selftests/arm64/gcs/gcs-locking.c | 20 --- tools/testing/selftests/arm64/gcs/gcs-util.h | 24 +++ tools/testing/selftests/arm64/gcs/libc-gcs.c | 20 --- 5 files changed, 194 insertions(+), 41 deletions(-) create mode 100644 tools/testing/selftests/arm64/gcs/gcs-clone-pidfd.c
tools/testing/selftests/arm64/gcs/Makefile +2 -1
@@ -6,7 +6,8 @@ # nolibc. # -TEST_GEN_PROGS := basic-gcs libc-gcs gcs-locking gcs-stress gcspushm gcsstr +TEST_GEN_PROGS := basic-gcs libc-gcs gcs-locking gcs-stress gcspushm gcsstr \ + gcs-clone-pidfd TEST_GEN_PROGS_EXTENDED := gcs-stress-thread LDLIBS+=-lpthread
tools/testing/selftests/arm64/gcs/gcs-clone-pidfd.c new file +168 -0
168 lines not on this page · raw message
tools/testing/selftests/arm64/gcs/gcs-locking.c +0 -20
@@ -18,26 +18,6 @@#include "gcs-util.h" -#define my_syscall2 (num, arg1, arg2) \ -({ \ - register long _num __asm__ ("x8") = (num); \ - register long _arg1 __asm__ ("x0") = (long)(arg1); \ - register long _arg2 __asm__ ("x1") = (long)(arg2); \ - register long _arg3 __asm__ ("x2") = 0; \ - register long _arg4 __asm__ ("x3") = 0; \ - register long _arg5 __asm__ ("x4") = 0; \ - \ - __asm__ volatile ( \ - "svc #0\n" \ - : "=r"(_arg1) \ - : "r"(_arg1), "r"(_arg2), \ - "r"(_arg3), "r"(_arg4), \ - "r"(_arg5), "r"(_num) \ - : "memory", "cc" \ - ); \ - _arg1; \ -}) -/* No mode bits are rejected for locking */ TEST (lock_all_modes) {
tools/testing/selftests/arm64/gcs/gcs-util.h +24 -0
@@ -43,6 +43,30 @@#define GCS_CAP (x) (((unsigned long)(x) & GCS_CAP_ADDR_MASK) | \ GCS_CAP_VALID_TOKEN) +/* +* prctl(..., arg1, arg2) with unused registers forced to zero. The +* kernel rejects non-zero unused args; libc may leave garbage there. +*/ +#define my_syscall2 (num, arg1, arg2) \ +({ \ + register long _num __asm__ ("x8") = (num); \ + register long _arg1 __asm__ ("x0") = (long)(arg1); \ + register long _arg2 __asm__ ("x1") = (long)(arg2); \ + register long _arg3 __asm__ ("x2") = 0; \ + register long _arg4 __asm__ ("x3") = 0; \ + register long _arg5 __asm__ ("x4") = 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=r"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) +static inline unsigned long *get_gcspr (void ) {unsigned long *gcspr;
tools/testing/selftests/arm64/gcs/libc-gcs.c +0 -20
@@ -24,26 +24,6 @@#include "gcs-util.h" -#define my_syscall2 (num, arg1, arg2) \ -({ \ - register long _num __asm__ ("x8") = (num); \ - register long _arg1 __asm__ ("x0") = (long)(arg1); \ - register long _arg2 __asm__ ("x1") = (long)(arg2); \ - register long _arg3 __asm__ ("x2") = 0; \ - register long _arg4 __asm__ ("x3") = 0; \ - register long _arg5 __asm__ ("x4") = 0; \ - \ - __asm__ volatile ( \ - "svc #0\n" \ - : "=r"(_arg1) \ - : "r"(_arg1), "r"(_arg2), \ - "r"(_arg3), "r"(_arg4), \ - "r"(_arg5), "r"(_num) \ - : "memory", "cc" \ - ); \ - _arg1; \ -}) - static noinline void gcs_recurse(int depth) {if (depth)
Signature · 1 line
2.43.0
Re: [PATCH v3 3/4] selftests/arm64: GCS must not leak on a failed clone3
Quoted 2 lines · Iván Ezequiel Rodriguez
KTAP coverage for
clone3(CLONE_VM|CLONE_PIDFD)with a bad pidfd
pointer: sixteen failures must not grow the parent's GCS VMA count.
Thanks, this is a lot cleaner and easier to understand than the original version. It'd probably be nicer to do the refactoring of the existing code in a separate commit but that's more nice to have than essential.
tools/testing/selftests/arm64/gcs/Makefile line 10
Quoted 2 lines
tools/testing/selftests/arm64/gcs/Makefile +2
-0+TEST_GEN_PROGS := basic-gcs libc-gcs gcs-locking gcs-stress gcspushm gcsstr \ + gcs-clone-pidfd
We should update .gitignore as well (or merge the test into libc-gcs but that's a bunch more work and definitely into bikeshed territory).
I verified that this fails with the current kernel and that the kernel change causes it to pass so with the above:
PATCH v3 4/4 selftests/riscv: shadow stack must not leak on a failed clone3
In reply to Iván Ezequiel RodriguezSame clone3 bad-pidfd check as the arm64 GCS selftest.
--- tools/testing/selftests/riscv/cfi/Makefile | 5 +- .../selftests/riscv/cfi/shstk-clone-pidfd.c | 173 ++++++++++++++++++ 2 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/cfi/shstk-clone-pidfd.c
tools/testing/selftests/riscv/cfi/Makefile +4 -1
@@ -13,10 +13,13 @@ endif ifeq ($(shell $(CC) $(CFLAGS) -nostdlib -xc /dev/null -o /dev/null > /dev/null 2>&1; echo $$?),0) -TEST_GEN_PROGS := cfitests +TEST_GEN_PROGS := cfitests shstk-clone-pidfd $(OUTPUT)/cfitests: cfitests.c shadowstack.c $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $^ + +$(OUTPUT)/shstk-clone-pidfd: shstk-clone-pidfd.c + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $^ else $(shell echo "Toolchain doesn't support CFI, skipping CFI kselftest." >&2)
tools/testing/selftests/riscv/cfi/shstk-clone-pidfd.c new file +173 -0
173 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: (subset) [PATCH v3 0/4] arm64, riscv: release shadow stacks on failed fork
In reply to Iván Ezequiel RodriguezQuoted 8 lines · Iván Ezequiel Rodriguez
clone3(CLONE_VM|CLONE_PIDFD)can fail input_user()aftercopy_thread()has mapped a shadow stack into the shared mm.copy_process()then callsexit_thread(). arm64 and riscv did not implement it, so the VMA leaked.x86 already frees via
exit_thread()->shstk_free(). Its separate TLS leak is whencopy_thread()fails after the alloc and skipsexit_thread().[...]
Applied to arm64 (for-next/core), thanks!
Only the arm64 changes, riscv can go through the riscv tree. I changed the commit log a bit on the first patch and addressed some nits Mark raised. As a separate patch, it might be good to add a kselftest for the io_uring worker thread and GCS.
[1/4] arm64/gcs: release GCS on failed fork
https://git.kernel.org/arm64/c/078e122f3bf0 [3/4] selftests/arm64: GCS must not leak on a failed clone3 https://git.kernel.org/arm64/c/f46be4da2cbb
cover selftests/arm64: GCS must survive io_uring SQPOLL worker exit
As suggested, a separate kselftest for the io_uring/SQPOLL worker path that gcs_reset_thread() also covers.
SQPOLL is create_io_thread() (args->fn + CLONE_VM). Without the reset, worker exit unmaps the parent's GCS. The test waits for iou-sqp-* to leave /proc/self/task after close (teardown is async), then checks the parent ss VMA count.
QEMU virt/max: pass with the reset; SIGSEGV on an unpatched kernel.
Iván Ezequiel Rodriguez (1):
selftests/arm64: GCS must survive io_uring SQPOLL worker exit
tools/testing/selftests/arm64/gcs/Makefile | 2 +- .../selftests/arm64/gcs/gcs-uring-worker.c | 254 ++++++++++++++++++ 2 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/gcs/gcs-uring-worker.c
Signature · 1 line
2.43.0
PATCH 1/1 selftests/arm64: GCS must survive io_uring SQPOLL worker exit
SQPOLL threads are create_io_thread() workers (args->fn + CLONE_VM). They inherit GCS tracking via arch_dup_task_struct(); without gcs_reset_thread() their exit runs gcs_free() on the parent's stack.
Create a SQPOLL ring, wait for iou-sqp-* to appear and leave /proc/self/task (close only queues async teardown), then check the parent ss VMA count is unchanged. QEMU: pass with the reset, SIGSEGV without it.
--- tools/testing/selftests/arm64/gcs/Makefile | 2 +- .../selftests/arm64/gcs/gcs-uring-worker.c | 254 ++++++++++++++++++ 2 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/gcs/gcs-uring-worker.c
tools/testing/selftests/arm64/gcs/Makefile +1 -1
@@ -7,7 +7,7 @@ # TEST_GEN_PROGS := basic-gcs libc-gcs gcs-locking gcs-stress gcspushm gcsstr \ - gcs-clone-pidfd + gcs-clone-pidfd gcs-uring-worker TEST_GEN_PROGS_EXTENDED := gcs-stress-thread LDLIBS+=-lpthread
tools/testing/selftests/arm64/gcs/gcs-uring-worker.c new file +254 -0
254 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH 1/1] selftests/arm64: GCS must survive io_uring SQPOLL worker exit
Quoted 8 lines · Iván Ezequiel Rodriguez
SQPOLL threads are
create_io_thread()workers (args->fn+CLONE_VM). They inherit GCS tracking viaarch_dup_task_struct(); withoutgcs_reset_thread()their exit runsgcs_free()on the parent's stack.Create a SQPOLL ring, wait for iou-sqp-* to appear and leave
/proc/self/task(close only queues async teardown), then check the parent ss VMA count is unchanged. QEMU: pass with the reset, SIGSEGV without it.