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

Samuel Holland samuel.holland at sifive.com
Thu Nov 5 07:42:25 PST 2026


On 2026-11-03 7:20 AM, Tobias Klauser wrote:
> 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")
> Signed-off-by: Tobias Klauser <tklauser at distanz.ch>
> ---
>  arch/riscv/kernel/usercfi.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c
> index 86141e5bab82..7971a98ca0c3 100644
> --- a/arch/riscv/kernel/usercfi.c
> +++ b/arch/riscv/kernel/usercfi.c
> @@ -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.

Reviewed-by: Samuel Holland <samuel.holland at sifive.com>

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.

>  	if (amo_user_shstk((unsigned long __user *)addr, (unsigned long)ssp) == -1)
>  		return -EFAULT;
>  




More information about the linux-riscv mailing list