Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Russell King | 4baa992 | 2008-08-02 10:55:55 +0100 | [diff] [blame] | 3 | * arch/arm/include/asm/uaccess.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | #ifndef _ASMARM_UACCESS_H |
| 6 | #define _ASMARM_UACCESS_H |
| 7 | |
| 8 | /* |
| 9 | * User space memory access functions |
| 10 | */ |
Russell King | 87c5257 | 2008-11-29 17:35:51 +0000 | [diff] [blame] | 11 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/memory.h> |
| 13 | #include <asm/domain.h> |
Ard Biesheuvel | 1542026 | 2022-01-18 13:45:09 +0100 | [diff] [blame] | 14 | #include <asm/unaligned.h> |
Catalin Marinas | 8b59278 | 2009-07-24 12:32:57 +0100 | [diff] [blame] | 15 | #include <asm/unified.h> |
David Howells | 9f97da7 | 2012-03-28 18:30:01 +0100 | [diff] [blame] | 16 | #include <asm/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Al Viro | 0f9b38c | 2016-12-25 03:33:03 -0500 | [diff] [blame] | 18 | #include <asm/extable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 21 | * These two functions allow hooking accesses to userspace to increase |
| 22 | * system integrity by ensuring that the kernel can not inadvertantly |
| 23 | * perform such accesses (eg, via list poison values) which could then |
| 24 | * be exploited for priviledge escalation. |
| 25 | */ |
Masahiro Yamada | 851140a | 2019-10-02 11:28:02 +0100 | [diff] [blame] | 26 | static __always_inline unsigned int uaccess_save_and_enable(void) |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 27 | { |
Russell King | a5e090a | 2015-08-19 20:40:41 +0100 | [diff] [blame] | 28 | #ifdef CONFIG_CPU_SW_DOMAIN_PAN |
| 29 | unsigned int old_domain = get_domain(); |
| 30 | |
| 31 | /* Set the current domain access to permit user accesses */ |
| 32 | set_domain((old_domain & ~domain_mask(DOMAIN_USER)) | |
| 33 | domain_val(DOMAIN_USER, DOMAIN_CLIENT)); |
| 34 | |
| 35 | return old_domain; |
| 36 | #else |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 37 | return 0; |
Russell King | a5e090a | 2015-08-19 20:40:41 +0100 | [diff] [blame] | 38 | #endif |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Masahiro Yamada | 851140a | 2019-10-02 11:28:02 +0100 | [diff] [blame] | 41 | static __always_inline void uaccess_restore(unsigned int flags) |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 42 | { |
Russell King | a5e090a | 2015-08-19 20:40:41 +0100 | [diff] [blame] | 43 | #ifdef CONFIG_CPU_SW_DOMAIN_PAN |
| 44 | /* Restore the user access mask */ |
| 45 | set_domain(flags); |
| 46 | #endif |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 50 | * These two are intentionally not defined anywhere - if the kernel |
| 51 | * code generates any references to them, that's a bug. |
| 52 | */ |
| 53 | extern int __get_user_bad(void); |
| 54 | extern int __put_user_bad(void); |
| 55 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_MMU |
| 57 | |
Russell King | 2350ebe | 2020-07-22 09:50:23 +0100 | [diff] [blame] | 58 | /* |
| 59 | * We use 33-bit arithmetic here. Success returns zero, failure returns |
| 60 | * addr_limit. We take advantage that addr_limit will be zero for KERNEL_DS, |
| 61 | * so this will always return success in that case. |
| 62 | */ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 63 | #define __range_ok(addr, size) ({ \ |
Tilman Schmidt | 16cf5b3 | 2007-02-10 01:45:41 -0800 | [diff] [blame] | 64 | unsigned long flag, roksum; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | __chk_user_ptr(addr); \ |
Stefan Agner | 32fdb04 | 2019-01-24 21:42:54 +0100 | [diff] [blame] | 66 | __asm__(".syntax unified\n" \ |
| 67 | "adds %1, %2, %3; sbcscc %1, %1, %0; movcc %0, #0" \ |
Tilman Schmidt | 16cf5b3 | 2007-02-10 01:45:41 -0800 | [diff] [blame] | 68 | : "=&r" (flag), "=&r" (roksum) \ |
Arnd Bergmann | 8ac6f5d | 2021-08-11 08:30:26 +0100 | [diff] [blame] | 69 | : "r" (addr), "Ir" (size), "0" (TASK_SIZE) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | : "cc"); \ |
| 71 | flag; }) |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* |
Russell King | d09fbb3 | 2018-07-09 15:22:45 +0100 | [diff] [blame] | 74 | * This is a type: either unsigned long, if the argument fits into |
| 75 | * that type, or otherwise unsigned long long. |
| 76 | */ |
| 77 | #define __inttype(x) \ |
| 78 | __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL)) |
| 79 | |
| 80 | /* |
Julien Thierry | afaf683 | 2018-09-11 10:14:50 +0100 | [diff] [blame] | 81 | * Sanitise a uaccess pointer such that it becomes NULL if addr+size |
| 82 | * is above the current addr_limit. |
| 83 | */ |
| 84 | #define uaccess_mask_range_ptr(ptr, size) \ |
| 85 | ((__typeof__(ptr))__uaccess_mask_range_ptr(ptr, size)) |
| 86 | static inline void __user *__uaccess_mask_range_ptr(const void __user *ptr, |
| 87 | size_t size) |
| 88 | { |
| 89 | void __user *safe_ptr = (void __user *)ptr; |
| 90 | unsigned long tmp; |
| 91 | |
| 92 | asm volatile( |
Stefan Agner | fe09d9c | 2019-03-23 12:10:44 +0100 | [diff] [blame] | 93 | " .syntax unified\n" |
Julien Thierry | afaf683 | 2018-09-11 10:14:50 +0100 | [diff] [blame] | 94 | " sub %1, %3, #1\n" |
| 95 | " subs %1, %1, %0\n" |
| 96 | " addhs %1, %1, #1\n" |
Stefan Agner | fe09d9c | 2019-03-23 12:10:44 +0100 | [diff] [blame] | 97 | " subshs %1, %1, %2\n" |
Julien Thierry | afaf683 | 2018-09-11 10:14:50 +0100 | [diff] [blame] | 98 | " movlo %0, #0\n" |
| 99 | : "+r" (safe_ptr), "=&r" (tmp) |
Arnd Bergmann | 8ac6f5d | 2021-08-11 08:30:26 +0100 | [diff] [blame] | 100 | : "r" (size), "r" (TASK_SIZE) |
Julien Thierry | afaf683 | 2018-09-11 10:14:50 +0100 | [diff] [blame] | 101 | : "cc"); |
| 102 | |
| 103 | csdb(); |
| 104 | return safe_ptr; |
| 105 | } |
| 106 | |
| 107 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | * Single-value transfer routines. They automatically use the right |
| 109 | * size if we just have the right pointer type. Note that the functions |
| 110 | * which read from user space (*get_*) need to take care not to leak |
| 111 | * kernel data even if the calling code is buggy and fails to check |
| 112 | * the return value. This means zeroing out the destination variable |
| 113 | * or buffer on error. Normally this is done out of line by the |
| 114 | * fixup code, but there are a few places where it intrudes on the |
| 115 | * main code path. When we only write to user space, there is no |
| 116 | * problem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | extern int __get_user_1(void *); |
| 119 | extern int __get_user_2(void *); |
| 120 | extern int __get_user_4(void *); |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 121 | extern int __get_user_32t_8(void *); |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 122 | extern int __get_user_8(void *); |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 123 | extern int __get_user_64t_1(void *); |
| 124 | extern int __get_user_64t_2(void *); |
| 125 | extern int __get_user_64t_4(void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 127 | #define __GUP_CLOBBER_1 "lr", "cc" |
| 128 | #ifdef CONFIG_CPU_USE_DOMAINS |
| 129 | #define __GUP_CLOBBER_2 "ip", "lr", "cc" |
| 130 | #else |
| 131 | #define __GUP_CLOBBER_2 "lr", "cc" |
| 132 | #endif |
| 133 | #define __GUP_CLOBBER_4 "lr", "cc" |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 134 | #define __GUP_CLOBBER_32t_8 "lr", "cc" |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 135 | #define __GUP_CLOBBER_8 "lr", "cc" |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 136 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 137 | #define __get_user_x(__r2, __p, __e, __l, __s) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | __asm__ __volatile__ ( \ |
| 139 | __asmeq("%0", "r0") __asmeq("%1", "r2") \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 140 | __asmeq("%3", "r1") \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | "bl __get_user_" #__s \ |
| 142 | : "=&r" (__e), "=r" (__r2) \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 143 | : "0" (__p), "r" (__l) \ |
| 144 | : __GUP_CLOBBER_##__s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 146 | /* narrowing a double-word get into a single 32bit word register: */ |
| 147 | #ifdef __ARMEB__ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 148 | #define __get_user_x_32t(__r2, __p, __e, __l, __s) \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 149 | __get_user_x(__r2, __p, __e, __l, 32t_8) |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 150 | #else |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 151 | #define __get_user_x_32t __get_user_x |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 152 | #endif |
| 153 | |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 154 | /* |
| 155 | * storing result into proper least significant word of 64bit target var, |
| 156 | * different only for big endian case where 64 bit __r2 lsw is r3: |
| 157 | */ |
| 158 | #ifdef __ARMEB__ |
| 159 | #define __get_user_x_64t(__r2, __p, __e, __l, __s) \ |
| 160 | __asm__ __volatile__ ( \ |
| 161 | __asmeq("%0", "r0") __asmeq("%1", "r2") \ |
| 162 | __asmeq("%3", "r1") \ |
| 163 | "bl __get_user_64t_" #__s \ |
| 164 | : "=&r" (__e), "=r" (__r2) \ |
| 165 | : "0" (__p), "r" (__l) \ |
| 166 | : __GUP_CLOBBER_##__s) |
| 167 | #else |
| 168 | #define __get_user_x_64t __get_user_x |
| 169 | #endif |
| 170 | |
| 171 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 172 | #define __get_user_check(x, p) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | ({ \ |
Arnd Bergmann | 8ac6f5d | 2021-08-11 08:30:26 +0100 | [diff] [blame] | 174 | unsigned long __limit = TASK_SIZE - 1; \ |
Stefan Agner | db4667a | 2018-05-08 22:51:50 +0100 | [diff] [blame] | 175 | register typeof(*(p)) __user *__p asm("r0") = (p); \ |
Russell King | d09fbb3 | 2018-07-09 15:22:45 +0100 | [diff] [blame] | 176 | register __inttype(x) __r2 asm("r2"); \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 177 | register unsigned long __l asm("r1") = __limit; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | register int __e asm("r0"); \ |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 179 | unsigned int __ua_flags = uaccess_save_and_enable(); \ |
Lexi Shao | df909df | 2021-09-23 03:41:25 +0100 | [diff] [blame] | 180 | int __tmp_e; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | switch (sizeof(*(__p))) { \ |
| 182 | case 1: \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 183 | if (sizeof((x)) >= 8) \ |
| 184 | __get_user_x_64t(__r2, __p, __e, __l, 1); \ |
| 185 | else \ |
| 186 | __get_user_x(__r2, __p, __e, __l, 1); \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 187 | break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | case 2: \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 189 | if (sizeof((x)) >= 8) \ |
| 190 | __get_user_x_64t(__r2, __p, __e, __l, 2); \ |
| 191 | else \ |
| 192 | __get_user_x(__r2, __p, __e, __l, 2); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | break; \ |
| 194 | case 4: \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 195 | if (sizeof((x)) >= 8) \ |
| 196 | __get_user_x_64t(__r2, __p, __e, __l, 4); \ |
| 197 | else \ |
| 198 | __get_user_x(__r2, __p, __e, __l, 4); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | break; \ |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 200 | case 8: \ |
| 201 | if (sizeof((x)) < 8) \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 202 | __get_user_x_32t(__r2, __p, __e, __l, 4); \ |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 203 | else \ |
| 204 | __get_user_x(__r2, __p, __e, __l, 8); \ |
| 205 | break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | default: __e = __get_user_bad(); break; \ |
| 207 | } \ |
Lexi Shao | df909df | 2021-09-23 03:41:25 +0100 | [diff] [blame] | 208 | __tmp_e = __e; \ |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 209 | uaccess_restore(__ua_flags); \ |
Russell King | d2c5b69 | 2005-11-18 14:22:03 +0000 | [diff] [blame] | 210 | x = (typeof(*(p))) __r2; \ |
Lexi Shao | df909df | 2021-09-23 03:41:25 +0100 | [diff] [blame] | 211 | __tmp_e; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | }) |
| 213 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 214 | #define get_user(x, p) \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 215 | ({ \ |
| 216 | might_fault(); \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 217 | __get_user_check(x, p); \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 218 | }) |
| 219 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 220 | extern int __put_user_1(void *, unsigned int); |
| 221 | extern int __put_user_2(void *, unsigned int); |
| 222 | extern int __put_user_4(void *, unsigned int); |
| 223 | extern int __put_user_8(void *, unsigned long long); |
| 224 | |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 225 | #define __put_user_check(__pu_val, __ptr, __err, __s) \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 226 | ({ \ |
Arnd Bergmann | 8ac6f5d | 2021-08-11 08:30:26 +0100 | [diff] [blame] | 227 | unsigned long __limit = TASK_SIZE - 1; \ |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 228 | register typeof(__pu_val) __r2 asm("r2") = __pu_val; \ |
| 229 | register const void __user *__p asm("r0") = __ptr; \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 230 | register unsigned long __l asm("r1") = __limit; \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 231 | register int __e asm("r0"); \ |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 232 | __asm__ __volatile__ ( \ |
| 233 | __asmeq("%0", "r0") __asmeq("%2", "r2") \ |
| 234 | __asmeq("%3", "r1") \ |
| 235 | "bl __put_user_" #__s \ |
| 236 | : "=&r" (__e) \ |
| 237 | : "0" (__p), "r" (__r2), "r" (__l) \ |
| 238 | : "ip", "lr", "cc"); \ |
| 239 | __err = __e; \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 240 | }) |
| 241 | |
| 242 | #else /* CONFIG_MMU */ |
| 243 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 244 | #define __addr_ok(addr) ((void)(addr), 1) |
| 245 | #define __range_ok(addr, size) ((void)(addr), 0) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 246 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 247 | #define get_user(x, p) __get_user(x, p) |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 248 | #define __put_user_check __put_user_nocheck |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 249 | |
| 250 | #endif /* CONFIG_MMU */ |
| 251 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 252 | #define access_ok(addr, size) (__range_ok(addr, size) == 0) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 253 | |
Russell King | b1cd0a1 | 2018-07-09 10:28:12 +0100 | [diff] [blame] | 254 | #ifdef CONFIG_CPU_SPECTRE |
| 255 | /* |
| 256 | * When mitigating Spectre variant 1, it is not worth fixing the non- |
| 257 | * verifying accessors, because we need to add verification of the |
| 258 | * address space there. Force these to use the standard get_user() |
| 259 | * version instead. |
| 260 | */ |
| 261 | #define __get_user(x, ptr) get_user(x, ptr) |
| 262 | #else |
| 263 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 264 | /* |
| 265 | * The "__xxx" versions of the user access functions do not verify the |
| 266 | * address space - it must have been done previously with a separate |
| 267 | * "access_ok()" call. |
| 268 | * |
| 269 | * The "xxx_error" versions set the third argument to EFAULT if an |
| 270 | * error occurs, and leave it unchanged on success. Note that these |
| 271 | * versions are void (ie, don't return a value as such). |
| 272 | */ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 273 | #define __get_user(x, ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | ({ \ |
| 275 | long __gu_err = 0; \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 276 | __get_user_err((x), (ptr), __gu_err, TUSER()); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | __gu_err; \ |
| 278 | }) |
| 279 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 280 | #define __get_user_err(x, ptr, err, __t) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | do { \ |
| 282 | unsigned long __gu_addr = (unsigned long)(ptr); \ |
| 283 | unsigned long __gu_val; \ |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 284 | unsigned int __ua_flags; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | __chk_user_ptr(ptr); \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 286 | might_fault(); \ |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 287 | __ua_flags = uaccess_save_and_enable(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | switch (sizeof(*(ptr))) { \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 289 | case 1: __get_user_asm_byte(__gu_val, __gu_addr, err, __t); break; \ |
| 290 | case 2: __get_user_asm_half(__gu_val, __gu_addr, err, __t); break; \ |
| 291 | case 4: __get_user_asm_word(__gu_val, __gu_addr, err, __t); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | default: (__gu_val) = __get_user_bad(); \ |
| 293 | } \ |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 294 | uaccess_restore(__ua_flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | (x) = (__typeof__(*(ptr)))__gu_val; \ |
| 296 | } while (0) |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 297 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Russell King | b64d1f6 | 2015-08-18 23:06:25 +0100 | [diff] [blame] | 299 | #define __get_user_asm(x, addr, err, instr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | __asm__ __volatile__( \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 301 | "1: " instr " %1, [%2], #0\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | "2:\n" \ |
Ard Biesheuvel | c4a84ae | 2015-03-24 10:41:09 +0100 | [diff] [blame] | 303 | " .pushsection .text.fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | " .align 2\n" \ |
| 305 | "3: mov %0, %3\n" \ |
| 306 | " mov %1, #0\n" \ |
| 307 | " b 2b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 308 | " .popsection\n" \ |
| 309 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | " .align 3\n" \ |
| 311 | " .long 1b, 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 312 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | : "+r" (err), "=&r" (x) \ |
| 314 | : "r" (addr), "i" (-EFAULT) \ |
| 315 | : "cc") |
| 316 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 317 | #define __get_user_asm_byte(x, addr, err, __t) \ |
| 318 | __get_user_asm(x, addr, err, "ldrb" __t) |
Russell King | b64d1f6 | 2015-08-18 23:06:25 +0100 | [diff] [blame] | 319 | |
Vincent Whitchurch | 344eb553 | 2018-11-09 10:12:30 +0100 | [diff] [blame] | 320 | #if __LINUX_ARM_ARCH__ >= 6 |
| 321 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 322 | #define __get_user_asm_half(x, addr, err, __t) \ |
| 323 | __get_user_asm(x, addr, err, "ldrh" __t) |
Vincent Whitchurch | 344eb553 | 2018-11-09 10:12:30 +0100 | [diff] [blame] | 324 | |
| 325 | #else |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | #ifndef __ARMEB__ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 328 | #define __get_user_asm_half(x, __gu_addr, err, __t) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | ({ \ |
| 330 | unsigned long __b1, __b2; \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 331 | __get_user_asm_byte(__b1, __gu_addr, err, __t); \ |
| 332 | __get_user_asm_byte(__b2, __gu_addr + 1, err, __t); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | (x) = __b1 | (__b2 << 8); \ |
| 334 | }) |
| 335 | #else |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 336 | #define __get_user_asm_half(x, __gu_addr, err, __t) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | ({ \ |
| 338 | unsigned long __b1, __b2; \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 339 | __get_user_asm_byte(__b1, __gu_addr, err, __t); \ |
| 340 | __get_user_asm_byte(__b2, __gu_addr + 1, err, __t); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | (x) = (__b1 << 8) | __b2; \ |
| 342 | }) |
| 343 | #endif |
| 344 | |
Vincent Whitchurch | 344eb553 | 2018-11-09 10:12:30 +0100 | [diff] [blame] | 345 | #endif /* __LINUX_ARM_ARCH__ >= 6 */ |
| 346 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 347 | #define __get_user_asm_word(x, addr, err, __t) \ |
| 348 | __get_user_asm(x, addr, err, "ldr" __t) |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 349 | |
| 350 | #define __put_user_switch(x, ptr, __err, __fn) \ |
| 351 | do { \ |
| 352 | const __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \ |
| 353 | __typeof__(*(ptr)) __pu_val = (x); \ |
| 354 | unsigned int __ua_flags; \ |
| 355 | might_fault(); \ |
| 356 | __ua_flags = uaccess_save_and_enable(); \ |
| 357 | switch (sizeof(*(ptr))) { \ |
| 358 | case 1: __fn(__pu_val, __pu_ptr, __err, 1); break; \ |
| 359 | case 2: __fn(__pu_val, __pu_ptr, __err, 2); break; \ |
| 360 | case 4: __fn(__pu_val, __pu_ptr, __err, 4); break; \ |
| 361 | case 8: __fn(__pu_val, __pu_ptr, __err, 8); break; \ |
| 362 | default: __err = __put_user_bad(); break; \ |
| 363 | } \ |
| 364 | uaccess_restore(__ua_flags); \ |
| 365 | } while (0) |
| 366 | |
| 367 | #define put_user(x, ptr) \ |
| 368 | ({ \ |
| 369 | int __pu_err = 0; \ |
| 370 | __put_user_switch((x), (ptr), __pu_err, __put_user_check); \ |
| 371 | __pu_err; \ |
| 372 | }) |
| 373 | |
Julien Thierry | e3aa624 | 2018-09-11 10:13:39 +0100 | [diff] [blame] | 374 | #ifdef CONFIG_CPU_SPECTRE |
| 375 | /* |
| 376 | * When mitigating Spectre variant 1.1, all accessors need to include |
| 377 | * verification of the address space. |
| 378 | */ |
| 379 | #define __put_user(x, ptr) put_user(x, ptr) |
| 380 | |
| 381 | #else |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 382 | #define __put_user(x, ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | ({ \ |
| 384 | long __pu_err = 0; \ |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 385 | __put_user_switch((x), (ptr), __pu_err, __put_user_nocheck); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | __pu_err; \ |
| 387 | }) |
| 388 | |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 389 | #define __put_user_nocheck(x, __pu_ptr, __err, __size) \ |
| 390 | do { \ |
| 391 | unsigned long __pu_addr = (unsigned long)__pu_ptr; \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 392 | __put_user_nocheck_##__size(x, __pu_addr, __err, TUSER());\ |
Russell King | 9f73bd8 | 2016-02-04 16:54:45 +0000 | [diff] [blame] | 393 | } while (0) |
| 394 | |
| 395 | #define __put_user_nocheck_1 __put_user_asm_byte |
| 396 | #define __put_user_nocheck_2 __put_user_asm_half |
| 397 | #define __put_user_nocheck_4 __put_user_asm_word |
| 398 | #define __put_user_nocheck_8 __put_user_asm_dword |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 400 | #endif /* !CONFIG_CPU_SPECTRE */ |
| 401 | |
Russell King | b64d1f6 | 2015-08-18 23:06:25 +0100 | [diff] [blame] | 402 | #define __put_user_asm(x, __pu_addr, err, instr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | __asm__ __volatile__( \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 404 | "1: " instr " %1, [%2], #0\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | "2:\n" \ |
Ard Biesheuvel | c4a84ae | 2015-03-24 10:41:09 +0100 | [diff] [blame] | 406 | " .pushsection .text.fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | " .align 2\n" \ |
| 408 | "3: mov %0, %3\n" \ |
| 409 | " b 2b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 410 | " .popsection\n" \ |
| 411 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | " .align 3\n" \ |
| 413 | " .long 1b, 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 414 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | : "+r" (err) \ |
| 416 | : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \ |
| 417 | : "cc") |
| 418 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 419 | #define __put_user_asm_byte(x, __pu_addr, err, __t) \ |
| 420 | __put_user_asm(x, __pu_addr, err, "strb" __t) |
Russell King | b64d1f6 | 2015-08-18 23:06:25 +0100 | [diff] [blame] | 421 | |
Vincent Whitchurch | 344eb553 | 2018-11-09 10:12:30 +0100 | [diff] [blame] | 422 | #if __LINUX_ARM_ARCH__ >= 6 |
| 423 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 424 | #define __put_user_asm_half(x, __pu_addr, err, __t) \ |
| 425 | __put_user_asm(x, __pu_addr, err, "strh" __t) |
Vincent Whitchurch | 344eb553 | 2018-11-09 10:12:30 +0100 | [diff] [blame] | 426 | |
| 427 | #else |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | #ifndef __ARMEB__ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 430 | #define __put_user_asm_half(x, __pu_addr, err, __t) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | ({ \ |
Michael S. Tsirkin | e8b94de | 2015-01-06 14:37:22 +0200 | [diff] [blame] | 432 | unsigned long __temp = (__force unsigned long)(x); \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 433 | __put_user_asm_byte(__temp, __pu_addr, err, __t); \ |
| 434 | __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err, __t);\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | }) |
| 436 | #else |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 437 | #define __put_user_asm_half(x, __pu_addr, err, __t) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | ({ \ |
Michael S. Tsirkin | e8b94de | 2015-01-06 14:37:22 +0200 | [diff] [blame] | 439 | unsigned long __temp = (__force unsigned long)(x); \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 440 | __put_user_asm_byte(__temp >> 8, __pu_addr, err, __t); \ |
| 441 | __put_user_asm_byte(__temp, __pu_addr + 1, err, __t); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | }) |
| 443 | #endif |
| 444 | |
Vincent Whitchurch | 344eb553 | 2018-11-09 10:12:30 +0100 | [diff] [blame] | 445 | #endif /* __LINUX_ARM_ARCH__ >= 6 */ |
| 446 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 447 | #define __put_user_asm_word(x, __pu_addr, err, __t) \ |
| 448 | __put_user_asm(x, __pu_addr, err, "str" __t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | #ifndef __ARMEB__ |
| 451 | #define __reg_oper0 "%R2" |
| 452 | #define __reg_oper1 "%Q2" |
| 453 | #else |
| 454 | #define __reg_oper0 "%Q2" |
| 455 | #define __reg_oper1 "%R2" |
| 456 | #endif |
| 457 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 458 | #define __put_user_asm_dword(x, __pu_addr, err, __t) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | __asm__ __volatile__( \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 460 | ARM( "1: str" __t " " __reg_oper1 ", [%1], #4\n" ) \ |
| 461 | ARM( "2: str" __t " " __reg_oper0 ", [%1]\n" ) \ |
| 462 | THUMB( "1: str" __t " " __reg_oper1 ", [%1]\n" ) \ |
| 463 | THUMB( "2: str" __t " " __reg_oper0 ", [%1, #4]\n" ) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | "3:\n" \ |
Ard Biesheuvel | c4a84ae | 2015-03-24 10:41:09 +0100 | [diff] [blame] | 465 | " .pushsection .text.fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | " .align 2\n" \ |
| 467 | "4: mov %0, %3\n" \ |
| 468 | " b 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 469 | " .popsection\n" \ |
| 470 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | " .align 3\n" \ |
| 472 | " .long 1b, 4b\n" \ |
| 473 | " .long 2b, 4b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 474 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | : "+r" (err), "+r" (__pu_addr) \ |
| 476 | : "r" (x), "i" (-EFAULT) \ |
| 477 | : "cc") |
| 478 | |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 479 | #define HAVE_GET_KERNEL_NOFAULT |
| 480 | |
| 481 | #define __get_kernel_nofault(dst, src, type, err_label) \ |
| 482 | do { \ |
| 483 | const type *__pk_ptr = (src); \ |
| 484 | unsigned long __src = (unsigned long)(__pk_ptr); \ |
| 485 | type __val; \ |
| 486 | int __err = 0; \ |
| 487 | switch (sizeof(type)) { \ |
| 488 | case 1: __get_user_asm_byte(__val, __src, __err, ""); break; \ |
| 489 | case 2: __get_user_asm_half(__val, __src, __err, ""); break; \ |
| 490 | case 4: __get_user_asm_word(__val, __src, __err, ""); break; \ |
| 491 | case 8: { \ |
| 492 | u32 *__v32 = (u32*)&__val; \ |
| 493 | __get_user_asm_word(__v32[0], __src, __err, ""); \ |
| 494 | if (__err) \ |
| 495 | break; \ |
| 496 | __get_user_asm_word(__v32[1], __src+4, __err, ""); \ |
| 497 | break; \ |
| 498 | } \ |
| 499 | default: __err = __get_user_bad(); break; \ |
| 500 | } \ |
Ard Biesheuvel | 1542026 | 2022-01-18 13:45:09 +0100 | [diff] [blame] | 501 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) \ |
| 502 | put_unaligned(__val, (type *)(dst)); \ |
| 503 | else \ |
| 504 | *(type *)(dst) = __val; /* aligned by caller */ \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 505 | if (__err) \ |
| 506 | goto err_label; \ |
| 507 | } while (0) |
| 508 | |
| 509 | #define __put_kernel_nofault(dst, src, type, err_label) \ |
| 510 | do { \ |
| 511 | const type *__pk_ptr = (dst); \ |
| 512 | unsigned long __dst = (unsigned long)__pk_ptr; \ |
| 513 | int __err = 0; \ |
Ard Biesheuvel | 1542026 | 2022-01-18 13:45:09 +0100 | [diff] [blame] | 514 | type __val = IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) \ |
| 515 | ? get_unaligned((type *)(src)) \ |
| 516 | : *(type *)(src); /* aligned by caller */ \ |
Arnd Bergmann | 2df4c9a | 2021-08-11 08:30:25 +0100 | [diff] [blame] | 517 | switch (sizeof(type)) { \ |
| 518 | case 1: __put_user_asm_byte(__val, __dst, __err, ""); break; \ |
| 519 | case 2: __put_user_asm_half(__val, __dst, __err, ""); break; \ |
| 520 | case 4: __put_user_asm_word(__val, __dst, __err, ""); break; \ |
| 521 | case 8: __put_user_asm_dword(__val, __dst, __err, ""); break; \ |
| 522 | default: __err = __put_user_bad(); break; \ |
| 523 | } \ |
| 524 | if (__err) \ |
| 525 | goto err_label; \ |
| 526 | } while (0) |
Russell King | 02fcb97 | 2006-06-21 14:44:52 +0100 | [diff] [blame] | 527 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 528 | #ifdef CONFIG_MMU |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 529 | extern unsigned long __must_check |
| 530 | arm_copy_from_user(void *to, const void __user *from, unsigned long n); |
| 531 | |
| 532 | static inline unsigned long __must_check |
Al Viro | 4de5b63 | 2017-03-21 08:23:33 -0400 | [diff] [blame] | 533 | raw_copy_from_user(void *to, const void __user *from, unsigned long n) |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 534 | { |
Kees Cook | dfd45b6 | 2016-06-23 15:06:53 -0700 | [diff] [blame] | 535 | unsigned int __ua_flags; |
| 536 | |
Kees Cook | dfd45b6 | 2016-06-23 15:06:53 -0700 | [diff] [blame] | 537 | __ua_flags = uaccess_save_and_enable(); |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 538 | n = arm_copy_from_user(to, from, n); |
| 539 | uaccess_restore(__ua_flags); |
| 540 | return n; |
| 541 | } |
| 542 | |
| 543 | extern unsigned long __must_check |
| 544 | arm_copy_to_user(void __user *to, const void *from, unsigned long n); |
| 545 | extern unsigned long __must_check |
| 546 | __copy_to_user_std(void __user *to, const void *from, unsigned long n); |
| 547 | |
| 548 | static inline unsigned long __must_check |
Al Viro | 4de5b63 | 2017-03-21 08:23:33 -0400 | [diff] [blame] | 549 | raw_copy_to_user(void __user *to, const void *from, unsigned long n) |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 550 | { |
Russell King | c014953d | 2015-12-05 13:42:07 +0000 | [diff] [blame] | 551 | #ifndef CONFIG_UACCESS_WITH_MEMCPY |
Kees Cook | dfd45b6 | 2016-06-23 15:06:53 -0700 | [diff] [blame] | 552 | unsigned int __ua_flags; |
Kees Cook | dfd45b6 | 2016-06-23 15:06:53 -0700 | [diff] [blame] | 553 | __ua_flags = uaccess_save_and_enable(); |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 554 | n = arm_copy_to_user(to, from, n); |
| 555 | uaccess_restore(__ua_flags); |
| 556 | return n; |
Russell King | c014953d | 2015-12-05 13:42:07 +0000 | [diff] [blame] | 557 | #else |
| 558 | return arm_copy_to_user(to, from, n); |
| 559 | #endif |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | extern unsigned long __must_check |
| 563 | arm_clear_user(void __user *addr, unsigned long n); |
| 564 | extern unsigned long __must_check |
| 565 | __clear_user_std(void __user *addr, unsigned long n); |
| 566 | |
| 567 | static inline unsigned long __must_check |
| 568 | __clear_user(void __user *addr, unsigned long n) |
| 569 | { |
| 570 | unsigned int __ua_flags = uaccess_save_and_enable(); |
| 571 | n = arm_clear_user(addr, n); |
| 572 | uaccess_restore(__ua_flags); |
| 573 | return n; |
| 574 | } |
| 575 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 576 | #else |
Al Viro | 4de5b63 | 2017-03-21 08:23:33 -0400 | [diff] [blame] | 577 | static inline unsigned long |
| 578 | raw_copy_from_user(void *to, const void __user *from, unsigned long n) |
| 579 | { |
| 580 | memcpy(to, (const void __force *)from, n); |
| 581 | return 0; |
| 582 | } |
| 583 | static inline unsigned long |
| 584 | raw_copy_to_user(void __user *to, const void *from, unsigned long n) |
| 585 | { |
| 586 | memcpy((void __force *)to, from, n); |
| 587 | return 0; |
| 588 | } |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 589 | #define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 590 | #endif |
Al Viro | 4de5b63 | 2017-03-21 08:23:33 -0400 | [diff] [blame] | 591 | #define INLINE_COPY_TO_USER |
| 592 | #define INLINE_COPY_FROM_USER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Russell King | 9957329 | 2006-10-26 10:27:42 +0100 | [diff] [blame] | 594 | static inline unsigned long __must_check clear_user(void __user *to, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 596 | if (access_ok(to, n)) |
Russell King | 02fcb97 | 2006-06-21 14:44:52 +0100 | [diff] [blame] | 597 | n = __clear_user(to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | return n; |
| 599 | } |
| 600 | |
Russell King | 3fba7e2 | 2015-08-19 11:02:28 +0100 | [diff] [blame] | 601 | /* These are from lib/ code, and use __get_user() and friends */ |
Will Deacon | 8c56cc8 | 2012-07-06 15:45:39 +0100 | [diff] [blame] | 602 | extern long strncpy_from_user(char *dest, const char __user *src, long count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Will Deacon | 8c56cc8 | 2012-07-06 15:45:39 +0100 | [diff] [blame] | 604 | extern __must_check long strnlen_user(const char __user *str, long n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | #endif /* _ASMARM_UACCESS_H */ |