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

Hyunjung Ko hj351016 at gmail.com
Tue Nov 17 21:39:26 PST 2026


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));



More information about the linux-riscv mailing list