riscv: misaligned: fix misaligned accesses handling in put/get_user()
v2 cover riscv: misaligned: fix misaligned accesses handling in put/get_user()
While debugging a few problems with the misaligned access kselftest, Alexandre discovered some crash with the current code. Indeed, some misaligned access was done by the kernel using put_user(). This was resulting in trap and a kernel crash since. The path was the following:
user -> kernel -> access to user memory -> misaligned trap -> trap -> kernel -> misaligned handling -> memcpy -> crash due to failed page fault while in interrupt disabled section.
Last discussion about kernel misaligned handling and interrupt reenabling were actually not to reenable interrupt when handling misaligned access being done by kernel. The best solution being not to do any misaligned accesses to userspace memory, we considered a few options:
- Remove any call to put/get_user() potentially doing misaligned accesses
- Do not do any misaligned accesses in put/get_user() itself
The second solution was the one chosen as there are too many callsites to put/get_user() that could potentially do misaligned accesses. We tried two approaches for that, either split access in two aligned accesses (and do RMW for put_user()) or call copy_from/to_user() which does not do any misaligned accesses. The later one was the simpler to implement (although the performances are probably lower than split aligned accesses but still way better than doing misaligned access emulation) and allows to support what we wanted.
These commits are based on top of Alex dev/alex/get_user_misaligned_v1 branch.
---
v2:
- Use __inttype instead of unsigned long for pointer cast
- Add Alex patch to make unsafe func use existing assembly.
- Add missing EXPORT_SYMBOL(__asm_copy_to_user_sum_enabled)
Alexandre Ghiti (1):
riscv: make unsafe user copy routines use existing assembly routines
Clément Léger (2):
riscv: process: use unsigned int instead of unsigned long for
put_user()
riscv: uaccess: do not do misaligned accesses in get/put_user()
arch/riscv/include/asm/asm-prototypes.h | 2 +- arch/riscv/include/asm/uaccess.h | 48 +++++++++++------------- arch/riscv/kernel/process.c | 2 +- arch/riscv/lib/riscv_v_helpers.c | 11 ++++-- arch/riscv/lib/uaccess.S | 50 +++++++++++++++++-------- arch/riscv/lib/uaccess_vector.S | 15 ++++++-- 6 files changed, 78 insertions(+), 50 deletions(-)
Signature · 1 line
2.49.0
PATCH v2 1/3 riscv: make unsafe user copy routines use existing assembly routines
From: Alexandre Ghiti <[email protected]>
The current implementation is underperforming and in addition, it triggers misaligned access traps on platforms which do not handle misaligned accesses in hardware.
Use the existing assembly routines to solve both problems at once.
--- arch/riscv/include/asm/asm-prototypes.h | 2 +- arch/riscv/include/asm/uaccess.h | 33 ++++------------ arch/riscv/lib/riscv_v_helpers.c | 11 ++++-- arch/riscv/lib/uaccess.S | 50 +++++++++++++++++-------- arch/riscv/lib/uaccess_vector.S | 15 ++++++-- 5 files changed, 63 insertions(+), 48 deletions(-)
arch/riscv/include/asm/asm-prototypes.h +1 -1
@@ -12,7 +12,7 @@ long long __ashlti3(long long a, int b);#ifdef CONFIG_RISCV_ISA_V #ifdef CONFIG_MMU -asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n); +asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n, bool enable_sum);#endif /* CONFIG_MMU */ void xor_regs_2_(unsigned long bytes, unsigned long *__restrict p1,
arch/riscv/include/asm/uaccess.h +8 -25
@@ -450,35 +450,18 @@ static inline void user_access_restore(unsigned long enabled) { } (x) = (__force __typeof__(*(ptr)))__gu_val; \ } while (0) -#define unsafe_copy_loop (dst, src, len, type, op, label) \ - while (len >= sizeof(type)) { \ - op(*(type *)(src), (type __user *)(dst), label); \ - dst += sizeof(type); \ - src += sizeof(type); \ - len -= sizeof(type); \ - } +unsigned long __must_check __asm_copy_to_user_sum_enabled(void __user *to, +const void *from,unsigned long n); +unsigned long __must_check __asm_copy_from_user_sum_enabled (void *to, +const void __user *from,unsigned long n);#define unsafe_copy_to_user (_dst, _src, _len, label) \ -do { \ - char __user *__ucu_dst = (_dst); \ - const char *__ucu_src = (_src); \ - size_t __ucu_len = (_len); \ - unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, unsafe_put_user, label); \ - unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, unsafe_put_user, label); \ - unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, unsafe_put_user, label); \ - unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, unsafe_put_user, label); \ -} while (0) + if (__asm_copy_to_user_sum_enabled(_dst, _src, _len)) \ + goto label;#define unsafe_copy_from_user (_dst, _src, _len, label) \ -do { \ - char *__ucu_dst = (_dst); \ - const char __user *__ucu_src = (_src); \ - size_t __ucu_len = (_len); \ - unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u64, unsafe_get_user, label); \ - unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u32, unsafe_get_user, label); \ - unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u16, unsafe_get_user, label); \ - unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u8, unsafe_get_user, label); \ -} while (0) + if (__asm_copy_from_user_sum_enabled(_dst, _src, _len)) \ + goto label;#else /* CONFIG_MMU */ #include <asm-generic/uaccess.h>
arch/riscv/lib/riscv_v_helpers.c +8 -3
@@ -16,8 +16,11 @@#ifdef CONFIG_MMU size_t riscv_v_usercopy_threshold =CONFIG_RISCV_ISA_V_UCOPY_THRESHOLD ;int __asm_vector_usercopy (void *dst,void *src,size_t n); +int __asm_vector_usercopy_sum_enabled (void *dst,void *src,size_t n);int fallback_scalar_usercopy (void *dst,void *src,size_t n); -asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n) +int fallback_scalar_usercopy_sum_enabled (void *dst,void *src,size_t n); +asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n, +bool enable_sum) {size_t remain, copied;
@@ -26,7 +29,8 @@ asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n) goto fallback;kernel_vector_begin (); - remain =__asm_vector_usercopy (dst, src, n); + remain = enable_sum ?__asm_vector_usercopy (dst, src, n) : +__asm_vector_usercopy_sum_enabled (dst, src, n);kernel_vector_end ();if (remain) {
@@ -40,6 +44,7 @@ asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n)return remain; fallback: - return fallback_scalar_usercopy(dst, src, n); +return enable_sum ?fallback_scalar_usercopy (dst, src, n) : + fallback_scalar_usercopy_sum_enabled(dst, src, n); }#endif
arch/riscv/lib/uaccess.S +34 -16
71 lines not on this page · raw message
arch/riscv/lib/uaccess_vector.S +12 -3
@@ -24,7 +24,18 @@ SYM_FUNC_START(__asm_vector_usercopy) /* Enable access to user memory */ li t6, SR_SUM csrs CSR_STATUS, t6 + mv t6, ra + call __asm_vector_usercopy_sum_enabled + + /* Disable access to user memory */ + mv ra, t6 + li t6, SR_SUM + csrc CSR_STATUS, t6 + ret +SYM_FUNC_END(__asm_vector_usercopy) + +SYM_FUNC_START(__asm_vector_usercopy_sum_enabled) loop: vsetvli iVL, iNum, e8, ELEM_LMUL_SETTING, ta, ma fixup vle8.v vData, (pSrc), 10f
@@ -36,8 +47,6 @@ loop: /* Exception fixup for vector load is shared with normal exit */ 10: - /* Disable access to user memory */ - csrc CSR_STATUS, t6 mv a0, iNum ret
@@ -49,4 +58,4 @@ loop: csrr t2, CSR_VSTART sub iNum, iNum, t2 j 10b -SYM_FUNC_END(__asm_vector_usercopy) +SYM_FUNC_END(__asm_vector_usercopy_sum_enabled)
Signature · 1 line
2.49.0
PATCH v2 2/3 riscv: process: use unsigned int instead of unsigned long for put_user()
In reply to Clément LégerThe specification of prctl() for GET_UNALIGN_CTL states that the value is returned in an unsigned int * address passed as an unsigned long. Change the type to match that and avoid an unaligned access as well.
--- arch/riscv/kernel/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/process.c +1 -1
@@ -57,7 +57,7 @@ int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)if (!unaligned_ctl_available ())return -EINVAL ; -return put_user (tsk->thread.align_ctl, (unsigned long __user *)adr); +return put_user (tsk->thread.align_ctl, (unsigned int __user *)adr); }void __show_regs (struct pt_regs *regs)
Signature · 1 line
2.49.0
Re: [PATCH v2 2/3] riscv: process: use unsigned int instead of unsigned long for put_user()
Hi Clément,
Quoted 14 lines · Clément Léger
The specification of
prctl()forGET_UNALIGN_CTLstates that the value is returned in an unsigned int * address passed as an unsigned long. Change the type to match that and avoid an unaligned access as well.Signed-off-by Clément Léger <[email protected]>--- arch/riscv/kernel/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/kernel/process.c +0
-0
arch/riscv/kernel/process.c int get_unalign_ctl(struct task_struct *tsk, unsigned long adr) · line 63
Quoted 8 lines
arch/riscv/kernel/process.c +1
-1@@ -57,7 +57,7 @@ int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)if (!unaligned_ctl_available ())return -EINVAL ; -return put_user (tsk->thread.align_ctl, (unsigned long __user *)adr); +return put_user (tsk->thread.align_ctl, (unsigned int __user *)adr); }void __show_regs (struct pt_regs *regs)
Thanks,
Alex
PATCH v2 3/3 riscv: uaccess: do not do misaligned accesses in get/put_user()
In reply to Clément LégerDoing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.
Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.
--- arch/riscv/include/asm/uaccess.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
arch/riscv/include/asm/uaccess.h +18 -5
@@ -169,8 +169,19 @@ do { \#endif /* CONFIG_64BIT */ +unsigned long __must_check __asm_copy_to_user_sum_enabled (void __user *to, +const void *from,unsigned long n); +unsigned long __must_check __asm_copy_from_user_sum_enabled (void *to, +const void __user *from,unsigned long n); +#define __get_user_nocheck (x, __gu_ptr, label) \ do { \ + if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \ + !IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) { \ + if (__asm_copy_from_user_sum_enabled(&(x), __gu_ptr, sizeof(*__gu_ptr))) \ + goto label; \ + break; \ + } \ switch (sizeof(*__gu_ptr)) { \ case 1: \ __get_user_asm("lb", (x), __gu_ptr, label); \
@@ -297,6 +308,13 @@ do { \#define __put_user_nocheck (x, __gu_ptr, label) \ do { \ + if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \ + !IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) { \ + __inttype(x) val = (__inttype(x))x; \ + if (__asm_copy_to_user_sum_enabled(__gu_ptr, &(val), sizeof(*__gu_ptr))) \ + goto label; \ + break; \ + } \ switch (sizeof(*__gu_ptr)) { \ case 1: \ __put_user_asm("sb", (x), __gu_ptr, label); \
@@ -450,11 +468,6 @@ static inline void user_access_restore(unsigned long enabled) { } (x) = (__force __typeof__(*(ptr)))__gu_val; \ } while (0) -unsigned long __must_check __asm_copy_to_user_sum_enabled(void __user *to, -const void *from,unsigned long n); -unsigned long __must_check __asm_copy_from_user_sum_enabled (void *to, -const void __user *from,unsigned long n); -#define unsafe_copy_to_user (_dst, _src, _len, label) \ if (__asm_copy_to_user_sum_enabled(_dst, _src, _len)) \ goto label;
Signature · 1 line
2.49.0
Re: [PATCH v2 3/3] riscv: uaccess: do not do misaligned accesses in get/put_user()
Quoted 60 lines · Clément Léger
Doing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.
Such behavior was detected with
GET_UNALIGN_CTL()that was doing aput_user()with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.Signed-off-by Clément Léger <[email protected]>--- arch/riscv/include/asm/uaccess.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)arch/riscv/include/asm/uaccess.h +18
-0@@ -169,8 +169,19 @@ do { \#endif /* CONFIG_64BIT */ +unsigned long __must_check __asm_copy_to_user_sum_enabled (void __user *to, +const void *from,unsigned long n); +unsigned long __must_check __asm_copy_from_user_sum_enabled (void *to, +const void __user *from,unsigned long n); +#define __get_user_nocheck (x, __gu_ptr, label) \ do { \ + if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \ + !IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) { \ + if (__asm_copy_from_user_sum_enabled(&(x), __gu_ptr, sizeof(*__gu_ptr))) \ + goto label; \ + break; \ + } \ switch (sizeof(*__gu_ptr)) { \ case 1: \ __get_user_asm("lb", (x), __gu_ptr, label); \@@ -297,6 +308,13 @@ do { \#define __put_user_nocheck (x, __gu_ptr, label) \ do { \ + if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \ + !IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) { \ + __inttype(x) val = (__inttype(x))x; \ + if (__asm_copy_to_user_sum_enabled(__gu_ptr, &(val), sizeof(*__gu_ptr))) \ + goto label; \ + break; \ + } \ switch (sizeof(*__gu_ptr)) { \ case 1: \ __put_user_asm("sb", (x), __gu_ptr, label); \@@ -450,11 +468,6 @@ static inline void user_access_restore(unsigned long enabled) { } (x) = (__force __typeof__(*(ptr)))__gu_val; \
arch/riscv/include/asm/uaccess.h static inline void user_access_restore(unsigned long enabled) { } · line 473
Quoted 10 lines
arch/riscv/include/asm/uaccess.h +0
-5} while (0) -unsigned long __must_check __asm_copy_to_user_sum_enabled(void __user *to, -const void *from,unsigned long n); -unsigned long __must_check __asm_copy_from_user_sum_enabled (void *to, -const void __user *from,unsigned long n); -#define unsafe_copy_to_user (_dst, _src, _len, label) \ if (__asm_copy_to_user_sum_enabled(_dst, _src, _len)) \ goto label;
Even if as noted by David, there is room for improvement, for now the simplicity of this is good enough to me, so:
Thanks,
Alex
On Mon, 2 Jun 2025 21:39:13 +0200
Clément Léger <[email protected]> wrote:
...
Quoted 4 lines
The second solution was the one chosen as there are too many callsites to put/get_user() that could potentially do misaligned accesses. We tried two approaches for that, either split access in two aligned accesses (and do RMW for
put_user())
You can't do RMW because it is visible to other threads.
David
Quoted 10 lines · David Laight
On Mon, 2 Jun 2025 21:39:13 +0200
Clément Léger <[email protected]> wrote:...
Quoted 4 lines
The second solution was the one chosen as there are too many callsites to put/get_user() that could potentially do misaligned accesses. We tried two approaches for that, either split access in two aligned accesses (and do RMW for
put_user())You can't do RMW because it is visible to other threads.
Ahem yeah indeed, so the only way was to do individual lb at least up to some alignment.
Thanks,
Clément
Quoted 2 lines
David
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:
Hello:
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:
Quoted 10 lines · On Mon, 2 Jun 2025 21:39:13 +0200 you wrote:
While debugging a few problems with the misaligned access kselftest, Alexandre discovered some crash with the current code. Indeed, some misaligned access was done by the kernel using
put_user(). This was resulting in trap and a kernel crash since. The path was the following:
user -> kernel -> access to user memory -> misaligned trap -> trap -> kernel -> misaligned handling -> memcpy -> crash due to failed page fault while in interrupt disabled section.[...]
Here is the summary with links:
- [v2,1/3] riscv: make unsafe user copy routines use existing assembly routines
https://git.kernel.org/riscv/c/a4348546332c - [v2,2/3] riscv: process: use unsigned int instead of unsigned long for put_user() https://git.kernel.org/riscv/c/020667d661f9 - [v2,3/3] riscv: uaccess: do not do misaligned accesses in get/put_user() https://git.kernel.org/riscv/c/ca1a66cdd685
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html