riscv: ptrace: Initialize cregs before copyin in compat_riscv_gpr_set()
Subsystems (3)
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.
---
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));
Quoted 19 lines · Hyunjung Ko
user_regset_copyin()only copiescountbytes, so aPTRACE_SETREGSETrequest with a short (or zero)iov_lenleaves the rest of the on-stack cregs uninitialized.cregs_to_regs()then copies all of it into the tracee'spt_regs, where the tracer can read it back withPTRACE_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")Signed-off-by Hyunjung Ko <[email protected]>---
arch/riscv/kernel/ptrace.c +0
-0
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