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