riscv: cfi: Bounds check restore token in create_rstor_token() before swap
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").
--- arch/riscv/kernel/usercfi.c | 3 +++ 1 file changed, 3 insertions(+)
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 ; +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
Quoted 18 lines · Tobias Klauser
create_rstor_token()computes addr and passes it toamo_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 functionrestore_user_shstk()had the same issue and was fixed by commitdf333b72c0e1("riscv: cfi: Bounds check restore token before swap").Fixes c70772afd5cc ("riscv/mm: Implement map_shadow_stack() syscall")Signed-off-by Tobias Klauser <[email protected]>--- arch/riscv/kernel/usercfi.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/usercfi.c +0
-0
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
-0if (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
Quoted 30 lines · Samuel Holland
Quoted 18 lines · Tobias Klauser
create_rstor_token()computes addr and passes it toamo_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 functionrestore_user_shstk()had the same issue and was fixed by commitdf333b72c0e1("riscv: cfi: Bounds check restore token before swap").Fixes c70772afd5cc ("riscv/mm: Implement map_shadow_stack() syscall")Signed-off-by Tobias Klauser <[email protected]>--- arch/riscv/kernel/usercfi.c | 3 +++ 1 file changed, 3 insertions(+)arch/riscv/kernel/usercfi.c +0
-0arch/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_stkis not trusted; insave_user_shstk()it is the user ssp CSR value set viaTASK_TI_USER_SSPinentry.S. I had missed this earlier.
Quoted 4 lines
Reviewed-by Samuel Holland <[email protected]>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