[v1,RFC,Zisslpcfi,16/20] riscv: Implements sslp prctls

Message ID 20230213045351.3945824-17-debug@rivosinc.com
State New
Headers
Series riscv control-flow integrity for U mode |

Commit Message

Deepak Gupta Feb. 13, 2023, 4:53 a.m. UTC
  New prctls are PR_GET_SHADOW_STACK_STATUS/PR_SET_SHADOW_STACK_STATUS and
PR_GET_INDIRECT_BR_LP_STATUS/PR_SET_INDIRECT_BR_LP_STATUS are implemented
on riscv in this patch.

Signed-off-by: Deepak Gupta <debug@rivosinc.com>
---
 arch/riscv/include/asm/processor.h |  4 +-
 arch/riscv/kernel/process.c        | 88 +++++++++++++++++++++++++++++-
 2 files changed, 90 insertions(+), 2 deletions(-)
  

Patch

diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h
index 39c36f739ebb..c088584580b4 100644
--- a/arch/riscv/include/asm/processor.h
+++ b/arch/riscv/include/asm/processor.h
@@ -46,7 +46,9 @@  struct cfi_status {
 	unsigned int ufcfi_en : 1; /* Enable for forward cfi. Note that ELP goes in sstatus */
 	unsigned int ubcfi_en : 1; /* Enable for backward cfi. */
 	unsigned int audit_mode : 1;
-	unsigned int rsvd1 : 29;
+	unsigned int ufcfi_locked : 1;
+	unsigned int ubcfi_locked : 1;
+	unsigned int rsvd1 : 27;
 	unsigned int lp_label; /* saved label value (25bit) */
 	long user_shdw_stk; /* Current user shadow stack pointer */
 	long shdw_stk_base; /* Base address of shadow stack */
diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c
index bfd8511914d9..1218ed4fd29f 100644
--- a/arch/riscv/kernel/process.c
+++ b/arch/riscv/kernel/process.c
@@ -257,4 +257,90 @@  int arch_elf_setup_cfi_state(const struct arch_elf_state *state)
 
 	return ret;
 }
-#endif
\ No newline at end of file
+#endif
+
+#ifdef CONFIG_USER_SHADOW_STACK
+int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status)
+{
+	unsigned long bcfi_status = 0;
+	struct thread_info *info = NULL;
+
+	if (!arch_supports_shadow_stack())
+		return -EINVAL;
+
+	info = current_thread_info();
+	bcfi_status |= info->user_cfi_state.ubcfi_locked ? (1UL << 0) : 0;
+	bcfi_status |= info->user_cfi_state.ubcfi_en ? ((1UL << 1) |
+		       (info->user_cfi_state.user_shdw_stk)) : 0;
+
+	return copy_to_user(status, &bcfi_status, sizeof(bcfi_status)) ? -EFAULT : 0;
+}
+
+int arch_set_shadow_stack_status(struct task_struct *t, unsigned long __user *status)
+{
+	unsigned long bcfi_status = 0;
+	struct thread_info *info = NULL;
+	unsigned long shdw_stk = 0;
+
+	if (!arch_supports_shadow_stack())
+		return -EINVAL;
+
+	info = current_thread_info();
+	/* bcfi status is locked and further can't be modified by user */
+	if (info->user_cfi_state.ubcfi_locked)
+		return -EINVAL;
+
+	if (copy_from_user(&bcfi_status, status, sizeof(bcfi_status)))
+		return -EFAULT;
+	/* clear two least significant bits. Always assume min 4 byte alignment */
+	shdw_stk = (long) (bcfi_status & (~3));
+
+	if (shdw_stk >= TASK_SIZE)
+		return -EINVAL;
+
+	info->user_cfi_state.ubcfi_en = (bcfi_status & (1UL << 1)) ? 1 : 0;
+	info->user_cfi_state.ubcfi_locked = (bcfi_status & (1UL << 0)) ? 1 : 0;
+	info->user_cfi_state.user_shdw_stk = (long) shdw_stk;
+
+	return 0;
+}
+#endif
+
+#ifdef CONFIG_USER_INDIRECT_BR_LP
+int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status)
+{
+	unsigned long fcfi_status = 0;
+	struct thread_info *info = NULL;
+
+	if (!arch_supports_indirect_br_lp_instr())
+		return -EINVAL;
+
+	info = current_thread_info();
+	fcfi_status |= info->user_cfi_state.ufcfi_locked ? (1UL << 0) : 0;
+	fcfi_status |= info->user_cfi_state.ufcfi_en ? (1UL << 1) : 0;
+
+	return copy_to_user(status, &fcfi_status, sizeof(fcfi_status)) ? -EFAULT : 0;
+}
+
+int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long __user *status)
+{
+	unsigned long fcfi_status = 0;
+	struct thread_info *info = NULL;
+
+	if (!arch_supports_indirect_br_lp_instr())
+		return -EINVAL;
+
+	info = current_thread_info();
+	/* bcfi status is locked and further can't be modified by user */
+	if (info->user_cfi_state.ufcfi_locked)
+		return -EINVAL;
+
+	if (copy_from_user(&fcfi_status, status, sizeof(fcfi_status)))
+		return -EFAULT;
+
+	info->user_cfi_state.ufcfi_en = (fcfi_status & (1UL << 1)) ? 1 : 0;
+	info->user_cfi_state.ufcfi_locked = (fcfi_status & (1UL << 0)) ? 1 : 0;
+
+	return 0;
+}
+#endif