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 " .pushsection __ex_table,\"a\"\n" \
346 " .align 3\n" \
347 " .long 1b, 3b\n" \
348 " .popsection" \
349 : "+r" (err), "=&r" (x) \
350 : "r" (addr), "i" (-EFAULT) \
351 : "cc")
352
353 #define __get_user_asm_byte(x, addr, err) \
354 __get_user_asm(x, addr, err, ldrb)
355
356 #if __LINUX_ARM_ARCH__ >= 6
357
358 #define __get_user_asm_half(x, addr, err) \
359 __get_user_asm(x, addr, err, ldrh)
360
361 #else
362
363 #ifndef __ARMEB__
364 #define __get_user_asm_half(x, __gu_addr, err) \
365 ({ \
366 unsigned long __b1, __b2; \
367 __get_user_asm_byte(__b1, __gu_addr, err); \
368 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
369 (x) = __b1 | (__b2 << 8); \
370 })
371 #else
372 #define __get_user_asm_half(x, __gu_addr, err) \
373 ({ \
374 unsigned long __b1, __b2; \
375 __get_user_asm_byte(__b1, __gu_addr, err); \
376 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
377 (x) = (__b1 << 8) | __b2; \
378 })
379 #endif
380
381 #endif /* __LINUX_ARM_ARCH__ >= 6 */
382
383 #define __get_user_asm_word(x, addr, err) \
384 __get_user_asm(x, addr, err, ldr)
385 #endif
386
387
388 #define __put_user_switch(x, ptr, __err, __fn) \
389 do { \
390 const __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
391 __typeof__(*(ptr)) __pu_val = (x); \
392 unsigned int __ua_flags; \
393 might_fault(); \
394 __ua_flags = uaccess_save_and_enable(); \
395 switch (sizeof(*(ptr))) { \
396 case 1: __fn(__pu_val, __pu_ptr, __err, 1); break; \
397 case 2: __fn(__pu_val, __pu_ptr, __err, 2); break; \
398 case 4: __fn(__pu_val, __pu_ptr, __err, 4); break; \
399 case 8: __fn(__pu_val, __pu_ptr, __err, 8); break; \
400 default: __err = __put_user_bad(); break; \
401 } \
402 uaccess_restore(__ua_flags); \
403 } while (0)
404
405 #define put_user(x, ptr) \
406 ({ \
407 int __pu_err = 0; \
408 __put_user_switch((x), (ptr), __pu_err, __put_user_check); \
409 __pu_err; \
410 })
411
412 #ifdef CONFIG_CPU_SPECTRE
413 /*
414 * When mitigating Spectre variant 1.1, all accessors need to include
415 * verification of the address space.
416 */
417 #define __put_user(x, ptr) put_user(x, ptr)
418
419 #else
420 #define __put_user(x, ptr) \
421 ({ \
422 long __pu_err = 0; \
423 __put_user_switch((x), (ptr), __pu_err, __put_user_nocheck); \
424 __pu_err; \
425 })
426
427 #define __put_user_nocheck(x, __pu_ptr, __err, __size) \
428 do { \
429 unsigned long __pu_addr = (unsigned long)__pu_ptr; \
430 __put_user_nocheck_##__size(x, __pu_addr, __err); \
431 } while (0)
432
433 #define __put_user_nocheck_1 __put_user_asm_byte
434 #define __put_user_nocheck_2 __put_user_asm_half
435 #define __put_user_nocheck_4 __put_user_asm_word
436 #define __put_user_nocheck_8 __put_user_asm_dword
437
438 #define __put_user_asm(x, __pu_addr, err, instr) \
439 __asm__ __volatile__( \
440 "1: " TUSER(instr) " %1, [%2], #0\n" \
441 "2:\n" \
442 " .pushsection .text.fixup,\"ax\"\n" \
443 " .align 2\n" \
444 "3: mov %0, %3\n" \
445 " b 2b\n" \
446 " .popsection\n" \
447 " .pushsection __ex_table,\"a\"\n" \
448 " .align 3\n" \
449 " .long 1b, 3b\n" \
450 " .popsection" \
451 : "+r" (err) \
452 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
453 : "cc")
454
455 #define __put_user_asm_byte(x, __pu_addr, err) \
456 __put_user_asm(x, __pu_addr, err, strb)
457
458 #if __LINUX_ARM_ARCH__ >= 6
459
460 #define __put_user_asm_half(x, __pu_addr, err) \
461 __put_user_asm(x, __pu_addr, err, strh)
462
463 #else
464
465 #ifndef __ARMEB__
466 #define __put_user_asm_half(x, __pu_addr, err) \
467 ({ \
468 unsigned long __temp = (__force unsigned long)(x); \
469 __put_user_asm_byte(__temp, __pu_addr, err); \
470 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
471 })
472 #else
473 #define __put_user_asm_half(x, __pu_addr, err) \
474 ({ \
475 unsigned long __temp = (__force unsigned long)(x); \
476 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
477 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
478 })
479 #endif
480
481 #endif /* __LINUX_ARM_ARCH__ >= 6 */
482
483 #define __put_user_asm_word(x, __pu_addr, err) \
484 __put_user_asm(x, __pu_addr, err, str)
485
486 #ifndef __ARMEB__
487 #define __reg_oper0 "%R2"
488 #define __reg_oper1 "%Q2"
489 #else
490 #define __reg_oper0 "%Q2"
491 #define __reg_oper1 "%R2"
492 #endif
493
494 #define __put_user_asm_dword(x, __pu_addr, err) \
495 __asm__ __volatile__( \
496 ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \
497 ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \
498 THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \
499 THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \
500 "3:\n" \
501 " .pushsection .text.fixup,\"ax\"\n" \
502 " .align 2\n" \
503 "4: mov %0, %3\n" \
504 " b 3b\n" \
505 " .popsection\n" \
506 " .pushsection __ex_table,\"a\"\n" \
507 " .align 3\n" \
508 " .long 1b, 4b\n" \
509 " .long 2b, 4b\n" \
510 " .popsection" \
511 : "+r" (err), "+r" (__pu_addr) \
512 : "r" (x), "i" (-EFAULT) \
513 : "cc")
514
515 #endif /* !CONFIG_CPU_SPECTRE */
516
517 #ifdef CONFIG_MMU
518 extern unsigned long __must_check
519 arm_copy_from_user(void *to, const void __user *from, unsigned long n);
520
521 static inline unsigned long __must_check
raw_copy_from_user(void * to,const void __user * from,unsigned long n)522 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
523 {
524 unsigned int __ua_flags;
525
526 __ua_flags = uaccess_save_and_enable();
527 n = arm_copy_from_user(to, from, n);
528 uaccess_restore(__ua_flags);
529 return n;
530 }
531
532 extern unsigned long __must_check
533 arm_copy_to_user(void __user *to, const void *from, unsigned long n);
534 extern unsigned long __must_check
535 __copy_to_user_std(void __user *to, const void *from, unsigned long n);
536
537 static inline unsigned long __must_check
raw_copy_to_user(void __user * to,const void * from,unsigned long n)538 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
539 {
540 #ifndef CONFIG_UACCESS_WITH_MEMCPY
541 unsigned int __ua_flags;
542 __ua_flags = uaccess_save_and_enable();
543 n = arm_copy_to_user(to, from, n);
544 uaccess_restore(__ua_flags);
545 return n;
546 #else
547 return arm_copy_to_user(to, from, n);
548 #endif
549 }
550
551 extern unsigned long __must_check
552 arm_clear_user(void __user *addr, unsigned long n);
553 extern unsigned long __must_check
554 __clear_user_std(void __user *addr, unsigned long n);
555
556 static inline unsigned long __must_check
__clear_user(void __user * addr,unsigned long n)557 __clear_user(void __user *addr, unsigned long n)
558 {
559 unsigned int __ua_flags = uaccess_save_and_enable();
560 n = arm_clear_user(addr, n);
561 uaccess_restore(__ua_flags);
562 return n;
563 }
564
565 #else
566 static inline unsigned long
raw_copy_from_user(void * to,const void __user * from,unsigned long n)567 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
568 {
569 memcpy(to, (const void __force *)from, n);
570 return 0;
571 }
572 static inline unsigned long
raw_copy_to_user(void __user * to,const void * from,unsigned long n)573 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
574 {
575 memcpy((void __force *)to, from, n);
576 return 0;
577 }
578 #define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0)
579 #endif
580 #define INLINE_COPY_TO_USER
581 #define INLINE_COPY_FROM_USER
582
clear_user(void __user * to,unsigned long n)583 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
584 {
585 if (access_ok(to, n))
586 n = __clear_user(to, n);
587 return n;
588 }
589
590 /* These are from lib/ code, and use __get_user() and friends */
591 extern long strncpy_from_user(char *dest, const char __user *src, long count);
592
593 extern __must_check long strnlen_user(const char __user *str, long n);
594
595 #endif /* _ASMARM_UACCESS_H */
596