Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Neuling <mikey@neuling.org>
To: pjw@kernel.org
Cc: ajones@ventanamicro.com, akpm@linux-foundation.org,
	aleksa.paunovic@htecgroup.com, alex@ghiti.fr,
	aou@eecs.berkeley.edu, arikalo@gmail.com, arnd@arndb.de,
	bjorn@rivosinc.com, david@redhat.com,
	djordje.todorovic@htecgroup.com, guoren@kernel.org,
	junhui.liu@pigmoral.tech, kevin.brodsky@arm.com,
	linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org,
	ljs@kernel.org, mikey@neuling.org, namcao@linutronix.de,
	oleg@redhat.com, osalvador@suse.de, palmer@dabbelt.com,
	panqinglin2020@iscas.ac.cn, rppt@kernel.org,
	rvishwanathan@mips.com, vishal.moola@gmail.com
Subject: [PATCH v2] riscv: Fix register corruption from uninitialized cregs on error
Date: Fri,  1 May 2026 06:23:20 +0000	[thread overview]
Message-ID: <20260501062320.2339562-1-mikey@neuling.org> (raw)
In-Reply-To: <78b4e931-9ec7-14b6-1487-906652a65ce8@kernel.org>

compat_riscv_gpr_set() calls cregs_to_regs() unconditionally, even when
user_regset_copyin() fails. Since cregs is an uninitialized stack
variable, a copyin failure causes uninitialized stack data to be written
into the target task's pt_regs, corrupting its register state and
potentially leaking kernel stack contents.

compat_restore_sigcontext() has the same issue: it calls cregs_to_regs()
even when __copy_from_user() fails, leading to the same corruption of
the signal-returning task's register state on error.

Only call cregs_to_regs() when the user copy succeeds.

Fixes: 4608c159594f ("riscv: compat: ptrace: Add compat_arch_ptrace implement")
Fixes: 7383ee05314b ("riscv: compat: signal: Add rt_frame implementation")
Signed-off-by: Michael Neuling <mikey@neuling.org>
Assisted-by: Cursor:claude-4.6-opus-high-thinking
---
 arch/riscv/kernel/compat_signal.c | 2 ++
 arch/riscv/kernel/ptrace.c        | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/kernel/compat_signal.c b/arch/riscv/kernel/compat_signal.c
index 6ec4e34255..cf3eb33a11 100644
--- a/arch/riscv/kernel/compat_signal.c
+++ b/arch/riscv/kernel/compat_signal.c
@@ -107,6 +107,8 @@ static long compat_restore_sigcontext(struct pt_regs *regs,
 
 	/* sc_regs is structured the same as the start of pt_regs */
 	err = __copy_from_user(&cregs, &sc->sc_regs, sizeof(sc->sc_regs));
+	if (unlikely(err))
+		return err;
 
 	cregs_to_regs(&cregs, regs);
 
diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c
index 93de2e7a30..793bcee461 100644
--- a/arch/riscv/kernel/ptrace.c
+++ b/arch/riscv/kernel/ptrace.c
@@ -577,8 +577,8 @@ static int compat_riscv_gpr_set(struct task_struct *target,
 	struct compat_user_regs_struct cregs;
 
 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
-
-	cregs_to_regs(&cregs, task_pt_regs(target));
+	if (!ret)
+		cregs_to_regs(&cregs, task_pt_regs(target));
 
 	return ret;
 }
-- 
2.43.0


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  parent reply	other threads:[~2026-05-01  6:23 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-09  9:11 [PATCH 0/5] riscv: Assorted bug fixes Michael Neuling
2026-04-09  9:11 ` [PATCH 1/5] riscv: errata: Fix bitwise vs logical AND in MIPS errata patching Michael Neuling
2026-05-01  2:08   ` Paul Walmsley
2026-04-09  9:11 ` [PATCH 2/5] riscv: ptrace: Fix register corruption in compat_riscv_gpr_set on error Michael Neuling
2026-05-01  2:05   ` Paul Walmsley
2026-05-01  6:21     ` Michael Neuling
2026-05-01  6:23     ` Michael Neuling [this message]
2026-05-02  3:14       ` [PATCH v2] riscv: Fix register corruption from uninitialized cregs " Paul Walmsley
2026-04-09  9:11 ` [PATCH 3/5] riscv: mm: Fix NULL pointer dereference in __set_memory Michael Neuling
2026-04-09 12:37   ` David Hildenbrand (Arm)
2026-04-10  6:23     ` Michael Neuling
2026-04-10  7:42       ` David Hildenbrand (Arm)
2026-04-10  7:53         ` Mike Rapoport
2026-04-10  7:59           ` David Hildenbrand (Arm)
2026-04-10  8:55             ` Michael Neuling
2026-04-09  9:11 ` [PATCH 4/5] riscv: mm: Fix NULL dereferences in napot hugetlb functions Michael Neuling
2026-04-09 12:36   ` David Hildenbrand (Arm)
2026-04-09  9:11 ` [PATCH 5/5] riscv: mm: Fix TOCTOU race in remove_pte_mapping Michael Neuling
2026-04-09 12:32   ` David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260501062320.2339562-1-mikey@neuling.org \
    --to=mikey@neuling.org \
    --cc=ajones@ventanamicro.com \
    --cc=akpm@linux-foundation.org \
    --cc=aleksa.paunovic@htecgroup.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=arikalo@gmail.com \
    --cc=arnd@arndb.de \
    --cc=bjorn@rivosinc.com \
    --cc=david@redhat.com \
    --cc=djordje.todorovic@htecgroup.com \
    --cc=guoren@kernel.org \
    --cc=junhui.liu@pigmoral.tech \
    --cc=kevin.brodsky@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=ljs@kernel.org \
    --cc=namcao@linutronix.de \
    --cc=oleg@redhat.com \
    --cc=osalvador@suse.de \
    --cc=palmer@dabbelt.com \
    --cc=panqinglin2020@iscas.ac.cn \
    --cc=pjw@kernel.org \
    --cc=rppt@kernel.org \
    --cc=rvishwanathan@mips.com \
    --cc=vishal.moola@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox