[PATCH] riscv: ptrace: Initialize cregs before copyin in compat_riscv_gpr_set()

Guo Ren guoren at kernel.org
Sat Nov 21 01:34:12 PST 2026


On Wed, Nov 18, 2026 at 1:39 PM Hyunjung Ko <hj351016 at gmail.com> wrote:
>
> user_regset_copyin() only copies `count` bytes, so a PTRACE_SETREGSET
> request with a short (or zero) iov_len leaves the rest of the on-stack
> cregs uninitialized. cregs_to_regs() then copies all of it into the
> tracee's pt_regs, where the tracer can read it back with
> PTRACE_GETREGSET.
>
> Fill cregs from the current register state first, so that a partial
> write only updates the registers it covers, as riscv_gpr_set() does.
>
> Fixes: 4608c159594f ("riscv: compat: ptrace: Add compat_arch_ptrace implement")
> Cc: stable at vger.kernel.org
> Signed-off-by: Hyunjung Ko <hj351016 at gmail.com>
> ---
> diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c
> index a005574..32227b7 100644
> --- a/arch/riscv/kernel/ptrace.c
> +++ b/arch/riscv/kernel/ptrace.c
> @@ -584,6 +584,8 @@ static int compat_riscv_gpr_set(struct task_struct *target,
>         int ret;
>         struct compat_user_regs_struct cregs;
>
> +       regs_to_cregs(&cregs, task_pt_regs(target));
> +
>         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
>         if (!ret)
>                 cregs_to_regs(&cregs, task_pt_regs(target));
Yes, we need to initialize cregs from the target's current register
state so that partial writes preserve the untouched registers.

Reviewed-by: GUO Ren (XuanTie) <guoren at kernel.org>

-- 
Best Regards
 GUO Ren



More information about the linux-riscv mailing list