Skip to contentSkip to conversation

riscv: cfi: Bounds check restore token before swap

PATCH In review Samuel Holland 2 participants 4 messages Oct 7 – Oct 8

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

PATCH 1/2 riscv: cfi: Bounds check restore token before swap

Currently the user-provided shadow stack restore token is not checked before being used as the address in ssamoswap.d. While an invalid restore token will be caught and raise SIGSEGV, this happens after writing 4 bytes of zeroes anywhere in the address space. Check that the restore token is at least a valid user address before the swap to ensure we do not scribble over kernel memory.

Fixes 66c9c713de59 ("riscv/signal: save and restore the shadow stack on a signal")

---

 arch/riscv/kernel/usercfi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/riscv/kernel/usercfi.c +4 -1
index dec0ba5eff5ea..23c4d6d9d8cbc 100644
@@ -206,10 +206,13 @@ int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr)
  */
 int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr)
 {
+	unsigned long __user *token_ptr = (unsigned long __user *)shstk_ptr;
 	unsigned long token = 0;
 
-	token = amo_user_shstk((unsigned long __user *)shstk_ptr, 0);
+	if (!access_ok(token_ptr, sizeof(token)))
+		return -EFAULT;
 
+	token = amo_user_shstk(token_ptr, 0);
 	if (token == -1)
 		return -EFAULT;
 
Signature · 4 lines
2.52.0

base-commit: a90ee4305c4a5df72c11b31dacfdc76e00fcf78a
branch: up/cfi-fixes
#1 lore raw

PATCH 2/2 riscv: cfi: Only write envcfg CSR when task is current

set_shstk_status() and set_indir_lp_status() take a task argument, which lets them operate on tasks other than current, but they always write the envcfg CSR for the current task. Fix this with the envcfg_update_bits() helper, which only writes the envcfg CSR when operating on current.

Fixes 61a0200211d31e ("riscv: Implement arch-agnostic shadow stack prctls")
Fixes 8a9e22d2ca5855 ("riscv: Implement indirect branch tracking prctls")

---

 arch/riscv/kernel/usercfi.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
arch/riscv/kernel/usercfi.c +3 -12
index 23c4d6d9d8cbc..86141e5bab822 100644
@@ -15,6 +15,7 @@
 #include <linux/syscalls.h>
 #include <linux/prctl.h>
 #include <asm/csr.h>
+#include <asm/switch_to.h>
 #include <asm/usercfi.h>
 
 unsigned long riscv_nousercfi __read_mostly;
@@ -66,12 +67,7 @@ void set_shstk_status(struct task_struct *task, bool enable)
 
 	task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0;
 
-	if (enable)
-		task->thread.envcfg |= ENVCFG_SSE;
-	else
-		task->thread.envcfg &= ~ENVCFG_SSE;
-
-	csr_write(CSR_ENVCFG, task->thread.envcfg);
+	envcfg_update_bits(task, ENVCFG_SSE, enable ? ENVCFG_SSE : 0);
 }
 
 void set_shstk_lock(struct task_struct *task, bool lock)
@@ -96,12 +92,7 @@ void set_indir_lp_status(struct task_struct *task, bool enable)
 
 	task->thread_info.user_cfi_state.ufcfi_en = enable ? 1 : 0;
 
-	if (enable)
-		task->thread.envcfg |= ENVCFG_LPE;
-	else
-		task->thread.envcfg &= ~ENVCFG_LPE;
-
-	csr_write(CSR_ENVCFG, task->thread.envcfg);
+	envcfg_update_bits(task, ENVCFG_LPE, enable ? ENVCFG_LPE : 0);
 }
 
 void set_indir_lp_lock(struct task_struct *task, bool lock)
Signature · 4 lines
2.52.0

base-commit: a90ee4305c4a5df72c11b31dacfdc76e00fcf78a
branch: up/cfi-fixes
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] riscv: cfi: Only write envcfg CSR when task is current

Quoted 44 lines · Samuel Holland

set_shstk_status() and set_indir_lp_status() take a task argument, which lets them operate on tasks other than current, but they always write the envcfg CSR for the current task. Fix this with the envcfg_update_bits() helper, which only writes the envcfg CSR when operating on current.

Fixes 61a0200211d31e ("riscv: Implement arch-agnostic shadow stack prctls")
Fixes 8a9e22d2ca5855 ("riscv: Implement indirect branch tracking prctls")

---

 arch/riscv/kernel/usercfi.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
arch/riscv/kernel/usercfi.c +2 -6
index 23c4d6d9d8cbc..86141e5bab822 100644
@@ -15,6 +15,7 @@
 #include <linux/syscalls.h>
 #include <linux/prctl.h>
 #include <asm/csr.h>
+#include <asm/switch_to.h>
 #include <asm/usercfi.h>
 
 unsigned long riscv_nousercfi __read_mostly;
@@ -66,12 +67,7 @@ void set_shstk_status(struct task_struct *task, bool enable)
 
        task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0;
 
-       if (enable)
-               task->thread.envcfg |= ENVCFG_SSE;
-       else
-               task->thread.envcfg &= ~ENVCFG_SSE;
-
-       csr_write(CSR_ENVCFG, task->thread.envcfg);
+       envcfg_update_bits(task, ENVCFG_SSE, enable ? ENVCFG_SSE : 0);
 }
 
 void set_shstk_lock(struct task_struct *task, bool lock)
@@ -96,12 +92,7 @@ void set_indir_lp_status(struct task_struct *task, bool enable)
 
        task->thread_info.user_cfi_state.ufcfi_en = enable ? 1 : 0;
 

arch/riscv/kernel/usercfi.c void set_indir_lp_status(struct task_struct *task, bool enable) · line 98

Quoted 10 lines
arch/riscv/kernel/usercfi.c +1 -6
-       if (enable)
-               task->thread.envcfg |= ENVCFG_LPE;
-       else
-               task->thread.envcfg &= ~ENVCFG_LPE;
-
-       csr_write(CSR_ENVCFG, task->thread.envcfg);
+       envcfg_update_bits(task, ENVCFG_LPE, enable ? ENVCFG_LPE : 0);
 }
 
 void set_indir_lp_lock(struct task_struct *task, bool lock)

LGTM

Quoted 5 lines

--
2.52.0

base-commit: a90ee4305c4a5df72c11b31dacfdc76e00fcf78a
branch: up/cfi-fixes

Reviewed-by on v1 1/2
#3 lore raw
In reply to Samuel Holland
Quoted 24 lines · Samuel Holland

Currently the user-provided shadow stack restore token is not checked before being used as the address in ssamoswap.d. While an invalid restore token will be caught and raise SIGSEGV, this happens after writing 4 bytes of zeroes anywhere in the address space. Check that the restore token is at least a valid user address before the swap to ensure we do not scribble over kernel memory.

Fixes 66c9c713de59 ("riscv/signal: save and restore the shadow stack on a signal")

---

 arch/riscv/kernel/usercfi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/riscv/kernel/usercfi.c +1 -0
index dec0ba5eff5ea..23c4d6d9d8cbc 100644
@@ -206,10 +206,13 @@ int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr)
  */
 int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr)
 {
+       unsigned long __user *token_ptr = (unsigned long __user *)shstk_ptr;

arch/riscv/kernel/usercfi.c int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr) · line 218

Quoted 10 lines
arch/riscv/kernel/usercfi.c +3 -1
        unsigned long token = 0;
 
-       token = amo_user_shstk((unsigned long __user *)shstk_ptr, 0);
+       if (!access_ok(token_ptr, sizeof(token)))
+               return -EFAULT;
 
+       token = amo_user_shstk(token_ptr, 0);
        if (token == -1)
                return -EFAULT;
 

LGTM.

Quoted 5 lines

--
2.52.0

base-commit: a90ee4305c4a5df72c11b31dacfdc76e00fcf78a
branch: up/cfi-fixes