Skip to contentSkip to conversation

riscv: cfi: Bounds check restore token in create_rstor_token() before swap

PATCH Superseded Tobias Klauser 2 participants 3 messages Nov 3 – Nov 10 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: cfi: Bounds check restore token in create_rstor_token() before swap

create_rstor_token() computes addr and passes it to amo_user_shstk(), which executes ssamoswap.d on it under __enable_user_access() without ever validating that addr is a legitimate user-space address. The sibling function restore_user_shstk() had the same issue and was fixed by commit df333b72c0e1 ("riscv: cfi: Bounds check restore token before swap").

Fixes c70772afd5cc ("riscv/mm: Implement map_shadow_stack() syscall")
---
 arch/riscv/kernel/usercfi.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/usercfi.c +3 -0
index 86141e5bab82..7971a98ca0c3 100644
@@ -158,6 +158,9 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr)
 	/* On RISC-V we're constructing token to be function of address itself */
 	addr = ssp - SHSTK_ENTRY_SIZE;
 
+	if (!access_ok((unsigned long __user *)addr, SHSTK_ENTRY_SIZE))
+		return -EFAULT;
+
 	if (amo_user_shstk((unsigned long __user *)addr, (unsigned long)ssp) == -1)
 		return -EFAULT;
 
Signature · 7 lines
2.56.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Tobias Klauser

create_rstor_token() computes addr and passes it to amo_user_shstk(), which executes ssamoswap.d on it under __enable_user_access() without ever validating that addr is a legitimate user-space address. The sibling function restore_user_shstk() had the same issue and was fixed by commit df333b72c0e1 ("riscv: cfi: Bounds check restore token before swap").

Fixes c70772afd5cc ("riscv/mm: Implement map_shadow_stack() syscall")
---
 arch/riscv/kernel/usercfi.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/usercfi.c +0 -0
index 86141e5bab82..7971a98ca0c3 100644

arch/riscv/kernel/usercfi.c static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) · line 163

Quoted 6 lines
arch/riscv/kernel/usercfi.c +3 -0
@@ -158,6 +158,9 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr)
 	/* On RISC-V we're constructing token to be function of address itself */
 	addr = ssp - SHSTK_ENTRY_SIZE;
 
+	if (!access_ok((unsigned long __user *)addr, SHSTK_ENTRY_SIZE))
+		return -EFAULT;
+

Ah, this is necessary because user_shdw_stk is not trusted; in save_user_shstk() it is the user ssp CSR value set via TASK_TI_USER_SSP in entry.S. I had missed this earlier.

Though since it turns out both callers of amo_user_shstk() need this check, it could make sense to move it there to deduplicate it.

arch/riscv/kernel/usercfi.c static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) · line 166

Quoted 3 lines
arch/riscv/kernel/usercfi.c +0 -0
 	if (amo_user_shstk((unsigned long __user *)addr, (unsigned long)ssp) == -1)
 		return -EFAULT;
 

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#2 lore raw
Quoted 30 lines · Samuel Holland
Quoted 18 lines · Tobias Klauser

create_rstor_token() computes addr and passes it to amo_user_shstk(), which executes ssamoswap.d on it under __enable_user_access() without ever validating that addr is a legitimate user-space address. The sibling function restore_user_shstk() had the same issue and was fixed by commit df333b72c0e1 ("riscv: cfi: Bounds check restore token before swap").

Fixes c70772afd5cc ("riscv/mm: Implement map_shadow_stack() syscall")
---
 arch/riscv/kernel/usercfi.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kernel/usercfi.c +0 -0
index 86141e5bab82..7971a98ca0c3 100644

arch/riscv/kernel/usercfi.c static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) · line 163

Quoted 6 lines
arch/riscv/kernel/usercfi.c +3 -0
@@ -158,6 +158,9 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr)
 	/* On RISC-V we're constructing token to be function of address itself */
 	addr = ssp - SHSTK_ENTRY_SIZE;
 
+	if (!access_ok((unsigned long __user *)addr, SHSTK_ENTRY_SIZE))
+		return -EFAULT;
+

Ah, this is necessary because user_shdw_stk is not trusted; in save_user_shstk() it is the user ssp CSR value set via TASK_TI_USER_SSP in entry.S. I had missed this earlier.

Quoted 4 lines

Though since it turns out both callers of amo_user_shstk() need this check, it could make sense to move it there to deduplicate it.

Thanks, that makes sense. I'll move it to amo_user_shstk in a v2 as suggested.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv