]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
openrisc: signal: do not restore privileged SR bits on sigreturn
authorAli Ahmet Memis <ali@iusegentoo.com>
Fri, 7 Aug 2026 23:42:30 +0000 (23:42 +0000)
committerStafford Horne <shorne@gmail.com>
Sat, 15 Aug 2026 06:14:55 +0000 (07:14 +0100)
restore_sigcontext() copies the whole supervision register (SR) from the
signal frame and only clears SPR_SR_SM before the value is reloaded into
the hardware SR (through ESR and l.rfe) on the return to user space.  All
other SR bits are left under user control.

An unprivileged task can thus return from a signal handler through a
crafted sigframe that clears SPR_SR_DME.  With the data MMU disabled the
CPU performs no translation or protection on data accesses, so the task
gains read and write access to arbitrary physical memory, a local
privilege escalation.  SPR_SR_IME, SPR_SR_SUMRA, SPR_SR_LEE, SPR_SR_EPH
and the cache-enable bits are exposed the same way.  The ptrace GPR regset
already refuses any change to SR for exactly this reason.

Restore only the arithmetic flag bits (F, CY, OV) from the signal frame
and take every privileged control bit from the SR the kernel saved on
signal entry.

Verified with qemu-system-or1k -M or1k-sim: before this change an
unprivileged PoC clears SPR_SR_DME in rt_sigreturn and writes a marker to
physical address 0x03000000 (beyond the kernel's mem=32M); afterwards the
same PoC receives SIGSEGV and physical memory is unchanged.

Fixes: ac689eb7f9d4 ("OpenRISC: Signal handling")
Cc: stable@vger.kernel.org
Signed-off-by: Ali Ahmet Memis <ali@iusegentoo.com>
Signed-off-by: Stafford Horne <shorne@gmail.com>
arch/openrisc/include/asm/processor.h
arch/openrisc/kernel/signal.c

index 3ff893a67c13b53f9f8fb03e2da057074dc3284e..ae43fe79b2b5c0aa8f9aa8fae199218ba1423170 100644 (file)
@@ -26,6 +26,8 @@
                   | SPR_SR_DCE | SPR_SR_SM)
 #define USER_SR   (SPR_SR_DME | SPR_SR_IME | SPR_SR_ICE \
                   | SPR_SR_DCE | SPR_SR_IEE | SPR_SR_TEE)
+/* SR bits user space may change via sigreturn, the rest stay kernel owned */
+#define SPR_SR_USER_MASK  (SPR_SR_F | SPR_SR_CY | SPR_SR_OV)
 
 /*
  * User space process size. This is hardcoded into a few places,
index f70a13ee059365679e505a0dc5f871bad6f7007c..2be5af3b983236466236709c5115009bdf30da57 100644 (file)
@@ -74,6 +74,7 @@ static long save_fp_state(struct sigcontext __user *sc)
 static int restore_sigcontext(struct pt_regs *regs,
                              struct sigcontext __user *sc)
 {
+       unsigned long old_sr = regs->sr;
        int err = 0;
 
        /* Always make any pending restarted system calls return -EINTR */
@@ -89,8 +90,8 @@ static int restore_sigcontext(struct pt_regs *regs,
        err |= __copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long));
        err |= restore_fp_state(sc);
 
-       /* make sure the SM-bit is cleared so user-mode cannot fool us */
-       regs->sr &= ~SPR_SR_SM;
+       /* keep the privileged SR bits kernel owned, restore only user flags */
+       regs->sr = (old_sr & ~SPR_SR_USER_MASK) | (regs->sr & SPR_SR_USER_MASK);
 
        regs->orig_gpr11 = -1;  /* Avoid syscall restart checks */