copy_thread() maps a shadow stack for a CLONE_VM child and can still
return 0. A later copy_process() failure, including clone3()
CLONE_PIDFD when put_user() fails, goes through
bad_fork_cleanup_thread and exit_thread(). riscv did not select
HAVE_EXIT_THREAD, so that free was a no-op. shstk_release() already
documents this call; deactivate_mm() is the only path that actually
reached it.

Select HAVE_EXIT_THREAD and call shstk_release() from
exit_thread(). The helper already refuses a different mm and a
cleared base, so CLONE_VFORK, !CLONE_VM, and a second call after
deactivate_mm() stay no-ops.

Do not free when shstk_alloc_thread_stack() fails. That error
returns from copy_thread() with the inherited parent base still in
place, and copy_process() skips exit_thread() on that path.

A kthread copies the caller's tracking via arch_dup_task_struct().
kernel_thread() uses CLONE_VM, so during a failed copy_process()
the child still shares the caller's mm and shstk_release()'s mm
check does not save us. Clear the inherited tracking as part of
kthread setup.

Fixes: fd44a4a85516 ("riscv/shstk: If needed allocate a new shadow stack on 
clone")
Cc: [email protected]
Cc: Deepak Gupta <[email protected]>
Assisted-by: LLM
Signed-off-by: Iván Ezequiel Rodriguez <[email protected]>
---
 arch/riscv/Kconfig          |  1 +
 arch/riscv/kernel/process.c | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 5874ca30f4f6..7635f8cb0397 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -169,6 +169,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
diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c
index 7cc5a6a5c020..def1f520d9ef 100644
--- a/arch/riscv/kernel/process.c
+++ b/arch/riscv/kernel/process.c
@@ -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,16 @@ void arch_release_task_struct(struct task_struct *tsk)
                riscv_v_thread_free(tsk);
 }
 
+void exit_thread(struct task_struct *tsk)
+{
+       /*
+        * shstk_release() documents this call for a failed CLONE_VM
+        * clone. copy_process() reaches it only after copy_thread()
+        * returns 0; deactivate_mm() does not run on that path.
+        */
+       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 +272,13 @@ 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;
+               /*
+                * kernel_thread() uses CLONE_VM, so a failed
+                * copy_process() still shares the caller's mm.
+                * Inherited tracking would then look like a child
+                * mapping to shstk_release().
+                */
+               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);
-- 
2.43.0


Reply via email to