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