Skip to content

Commit ca1a66c

Browse files
clementlegerpalmer-dabbelt
authored andcommitted
riscv: uaccess: do not do misaligned accesses in get/put_user()
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 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. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Alexandre Ghiti <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 020667d commit ca1a66c

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

arch/riscv/include/asm/uaccess.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,19 @@ do { \
169169

170170
#endif /* CONFIG_64BIT */
171171

172+
unsigned long __must_check __asm_copy_to_user_sum_enabled(void __user *to,
173+
const void *from, unsigned long n);
174+
unsigned long __must_check __asm_copy_from_user_sum_enabled(void *to,
175+
const void __user *from, unsigned long n);
176+
172177
#define __get_user_nocheck(x, __gu_ptr, label) \
173178
do { \
179+
if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \
180+
!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) { \
181+
if (__asm_copy_from_user_sum_enabled(&(x), __gu_ptr, sizeof(*__gu_ptr))) \
182+
goto label; \
183+
break; \
184+
} \
174185
switch (sizeof(*__gu_ptr)) { \
175186
case 1: \
176187
__get_user_asm("lb", (x), __gu_ptr, label); \
@@ -297,6 +308,13 @@ do { \
297308

298309
#define __put_user_nocheck(x, __gu_ptr, label) \
299310
do { \
311+
if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && \
312+
!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) { \
313+
__inttype(x) val = (__inttype(x))x; \
314+
if (__asm_copy_to_user_sum_enabled(__gu_ptr, &(val), sizeof(*__gu_ptr))) \
315+
goto label; \
316+
break; \
317+
} \
300318
switch (sizeof(*__gu_ptr)) { \
301319
case 1: \
302320
__put_user_asm("sb", (x), __gu_ptr, label); \
@@ -450,11 +468,6 @@ static inline void user_access_restore(unsigned long enabled) { }
450468
(x) = (__force __typeof__(*(ptr)))__gu_val; \
451469
} while (0)
452470

453-
unsigned long __must_check __asm_copy_to_user_sum_enabled(void __user *to,
454-
const void *from, unsigned long n);
455-
unsigned long __must_check __asm_copy_from_user_sum_enabled(void *to,
456-
const void __user *from, unsigned long n);
457-
458471
#define unsafe_copy_to_user(_dst, _src, _len, label) \
459472
if (__asm_copy_to_user_sum_enabled(_dst, _src, _len)) \
460473
goto label;

0 commit comments

Comments
 (0)