Skip to contentSkip to conversation

riscv: ptrace: Initialize cregs before copyin in compat_riscv_gpr_set()

PATCH In review Hyunjung Ko 2 participants 2 messages Nov 18 – Nov 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: ptrace: Initialize cregs before copyin in compat_riscv_gpr_set()

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

---

arch/riscv/kernel/ptrace.c +2 -0
index a005574..32227b7 100644
@@ -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));
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Hyunjung Ko

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

---

arch/riscv/kernel/ptrace.c +0 -0
index a005574..32227b7 100644

arch/riscv/kernel/ptrace.c static int compat_riscv_gpr_set(struct task_struct *target, · line 591

Quoted 8 lines
arch/riscv/kernel/ptrace.c +2 -0
@@ -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.

Signature · 2 lines
Best Regards
 GUO Ren