[PATCH 1/2] riscv: Use pointer masking to limit uaccess speculation
Deepak Gupta
debug at rivosinc.com
Sat Dec 27 16:41:36 PST 2025
On Sat, Dec 27, 2025 at 01:57:03PM +0100, Lukas Gerlach wrote:
>Thanks for the review. You're right - we should only clear the sign bit
>(b38/b47/b56 depending on mode), not b63. Clearing upper bits would
>interfere with pointer masking.
>
>Here's a fix that computes the sign bit position arithmetically to avoid
>branches, this ensures the mitigation cannot be bypassed under speculation.
>This is basically the VA_BITS macro but computed in a branch-free way.
>
>In arch/riscv/include/asm/uaccess.h:
>
> #define UACCESS_SIGN_BIT \
> (VA_BITS_SV39 - 1 + 9*((unsigned long)pgtable_l4_enabled) + \
> 9*((unsigned long)pgtable_l5_enabled))
>
> #define uaccess_mask_ptr(ptr) ((__typeof__(ptr))__uaccess_mask_ptr(ptr))
> static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
> {
> return (void __user *)((unsigned long)ptr & ~BIT_ULL(UACCESS_SIGN_BIT));
> }
>
>This evaluates to bit 38 for Sv39, bit 47 for Sv48, and bit 56 for Sv57.
looks good to me.
Although, I am concerned about maintainibility and bit-rotting.
I would suggest to fix VA_BITS definition instead of defining a new macro here.
More information about the linux-riscv
mailing list