1 /*
2 * arch/arm/include/asm/uaccess.h
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8 #ifndef _ASMARM_UACCESS_H
9 #define _ASMARM_UACCESS_H
10
11 /*
12 * User space memory access functions
13 */
14 #include <linux/string.h>
15 #include <asm/memory.h>
16 #include <asm/domain.h>
17 #include <asm/unified.h>
18 #include <asm/compiler.h>
19
20 #include <asm/extable.h>
21
22 /*
23 * These two functions allow hooking accesses to userspace to increase
24 * system integrity by ensuring that the kernel can not inadvertantly
25 * perform such accesses (eg, via list poison values) which could then
26 * be exploited for priviledge escalation.
27 */
uaccess_save_and_enable(void)28 static inline unsigned int uaccess_save_and_enable(void)
29 {
30 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
31 unsigned int old_domain = get_domain();
32
33 /* Set the current domain access to permit user accesses */
34 set_domain((old_domain & ~domain_mask(DOMAIN_USER)) |
35 domain_val(DOMAIN_USER, DOMAIN_CLIENT));
36
37 return old_domain;
38 #else
39 return 0;
40 #endif
41 }
42
uaccess_restore(unsigned int flags)43 static inline void uaccess_restore(unsigned int flags)
44 {
45 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
46 /* Restore the user access mask */
47 set_domain(flags);
48 #endif
49 }
50
51 /*
52 * These two are intentionally not defined anywhere - if the kernel
53 * code generates any references to them, that's a bug.
54 */
55 extern int __get_user_bad(void);
56 extern int __put_user_bad(void);
57
58 /*
59 * Note that this is actually 0x1,0000,0000
60 */
61 #define KERNEL_DS 0x00000000
62 #define get_ds() (KERNEL_DS)
63
64 #ifdef CONFIG_MMU
65
66 #define USER_DS TASK_SIZE
67 #define get_fs() (current_thread_info()->addr_limit)
68
set_fs(mm_segment_t fs)69 static inline void set_fs(mm_segment_t fs)
70 {
71 current_thread_info()->addr_limit = fs;
72 modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
73 }
74
75 #define segment_eq(a, b) ((a) == (b))
76
77 /* We use 33-bit arithmetic here... */
78 #define __range_ok(addr, size) ({ \
79 unsigned long flag, roksum; \
80 __chk_user_ptr(addr); \
81 __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
82 : "=&r" (flag), "=&r" (roksum) \
83 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
84 : "cc"); \
85 flag; })
86
87 /*
88 * This is a type: either unsigned long, if the argument fits into
89 * that type, or otherwise unsigned long long.
90 */
91 #define __inttype(x) \
92 __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
93
94 /*
95 * Single-value transfer routines. They automatically use the right
96 * size if we just have the right pointer type. Note that the functions
97 * which read from user space (*get_*) need to take care not to leak
98 * kernel data even if the calling code is buggy and fails to check
99 * the return value. This means zeroing out the destination variable
100 * or buffer on error. Normally this is done out of line by the
101 * fixup code, but there are a few places where it intrudes on the
102 * main code path. When we only write to user space, there is no
103 * problem.
104 */
105 extern int __get_user_1(void *);
106 extern int __get_user_2(void *);
107 extern int __get_user_4(void *);
108 extern int __get_user_32t_8(void *);
109 extern int __get_user_8(void *);
110 extern int __get_user_64t_1(void *);
111 extern int __get_user_64t_2(void *);
112 extern int __get_user_64t_4(void *);
113
114 #define __GUP_CLOBBER_1 "lr", "cc"
115 #ifdef CONFIG_CPU_USE_DOMAINS
116 #define __GUP_CLOBBER_2 "ip", "lr", "cc"
117 #else
118 #define __GUP_CLOBBER_2 "lr", "cc"
119 #endif
120 #define __GUP_CLOBBER_4 "lr", "cc"
121 #define __GUP_CLOBBER_32t_8 "lr", "cc"
122 #define __GUP_CLOBBER_8 "lr", "cc"
123
124 #define __get_user_x(__r2, __p, __e, __l, __s) \
125 __asm__ __volatile__ ( \
126 __asmeq("%0", "r0") __asmeq("%1", "r2") \
127 __asmeq("%3", "r1") \
128 "bl __get_user_" #__s \
129 : "=&r" (__e), "=r" (__r2) \
130 : "0" (__p), "r" (__l) \
131 : __GUP_CLOBBER_##__s)
132
133 /* narrowing a double-word get into a single 32bit word register: */
134 #ifdef __ARMEB__
135 #define __get_user_x_32t(__r2, __p, __e, __l, __s) \
136 __get_user_x(__r2, __p, __e, __l, 32t_8)
137 #else
138 #define __get_user_x_32t __get_user_x
139 #endif
140
141 /*
142 * storing result into proper least significant word of 64bit target var,
143 * different only for big endian case where 64 bit __r2 lsw is r3:
144 */
145 #ifdef __ARMEB__
146 #define __get_user_x_64t(__r2, __p, __e, __l, __s) \
147 __asm__ __volatile__ ( \
148 __asmeq("%0", "r0") __asmeq("%1", "r2") \
149 __asmeq("%3", "r1") \
150 "bl __get_user_64t_" #__s \
151 : "=&r" (__e), "=r" (__r2) \
152 : "0" (__p), "r" (__l) \
153 : __GUP_CLOBBER_##__s)
154 #else
155 #define __get_user_x_64t __get_user_x
156 #endif
157
158
159 #define __get_user_check(x, p) \
160 ({ \
161 unsigned long __limit = current_thread_info()->addr_limit - 1; \
162 register typeof(*(p)) __user *__p asm("r0") = (p); \
163 register __inttype(x) __r2 asm("r2"); \
164 register unsigned long __l asm("r1") = __limit; \
165 register int __e asm("r0"); \
166 unsigned int __ua_flags = uaccess_save_and_enable(); \
167 switch (sizeof(*(__p))) { \
168 case 1: \
169 if (sizeof((x)) >= 8) \
170 __get_user_x_64t(__r2, __p, __e, __l, 1); \
171 else \
172 __get_user_x(__r2, __p, __e, __l, 1); \
173 break; \
174 case 2: \
175 if (sizeof((x)) >= 8) \
176 __get_user_x_64t(__r2, __p, __e, __l, 2); \
177 else \
178 __get_user_x(__r2, __p, __e, __l, 2); \
179 break; \
180 case 4: \
181 if (sizeof((x)) >= 8) \
182 __get_user_x_64t(__r2, __p, __e, __l, 4); \
183 else \
184 __get_user_x(__r2, __p, __e, __l, 4); \
185 break; \
186 case 8: \
187 if (sizeof((x)) < 8) \
188 __get_user_x_32t(__r2, __p, __e, __l, 4); \
189 else \
190 __get_user_x(__r2, __p, __e, __l, 8); \
191 break; \
192 default: __e = __get_user_bad(); break; \
193 } \
194 uaccess_restore(__ua_flags); \
195 x = (typeof(*(p))) __r2; \
196 __e; \
197 })
198
199 #define get_user(x, p) \
200 ({ \
201 might_fault(); \
202 __get_user_check(x, p); \
203 })
204
205 extern int __put_user_1(void *, unsigned int);
206 extern int __put_user_2(void *, unsigned int);
207 extern int __put_user_4(void *, unsigned int);
208 extern int __put_user_8(void *, unsigned long long);
209
210 #define __put_user_check(__pu_val, __ptr, __err, __s) \
211 ({ \
212 unsigned long __limit = current_thread_info()->addr_limit - 1; \
213 register typeof(__pu_val) __r2 asm("r2") = __pu_val; \
214 register const void __user *__p asm("r0") = __ptr; \
215 register unsigned long __l asm("r1") = __limit; \
216 register int __e asm("r0"); \
217 __asm__ __volatile__ ( \
218 __asmeq("%0", "r0") __asmeq("%2", "r2") \
219 __asmeq("%3", "r1") \
220 "bl __put_user_" #__s \
221 : "=&r" (__e) \
222 : "0" (__p), "r" (__r2), "r" (__l) \
223 : "ip", "lr", "cc"); \
224 __err = __e; \
225 })
226
227 #else /* CONFIG_MMU */
228
229 /*
230 * uClinux has only one addr space, so has simplified address limits.
231 */
232 #define USER_DS KERNEL_DS
233
234 #define segment_eq(a, b) (1)
235 #define __addr_ok(addr) ((void)(addr), 1)
236 #define __range_ok(addr, size) ((void)(addr), 0)
237 #define get_fs() (KERNEL_DS)
238
set_fs(mm_segment_t fs)239 static inline void set_fs(mm_segment_t fs)
240 {
241 }
242
243 #define get_user(x, p) __get_user(x, p)
244 #define __put_user_check __put_user_nocheck
245
246 #endif /* CONFIG_MMU */
247
248 #define access_ok(type, addr, size) (__range_ok(addr, size) == 0)
249
250 #define user_addr_max() \
251 (uaccess_kernel() ? ~0UL : get_fs())
252
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
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 */
272 #define __get_user(x, ptr) \
273 ({ \
274 long __gu_err = 0; \
275 __get_user_err((x), (ptr), __gu_err); \
276 __gu_err; \
277 })
278
279 #define __get_user_err(x, ptr, err) \
280 do { \
281 unsigned long __gu_addr = (unsigned long)(ptr); \
282 unsigned long __gu_val; \
283 unsigned int __ua_flags; \
284 __chk_user_ptr(ptr); \
285 might_fault(); \
286 __ua_flags = uaccess_save_and_enable(); \
287 switch (sizeof(*(ptr))) { \
288 case 1: __get_user_asm_byte(__gu_val, __gu_addr, err); break; \
289 case 2: __get_user_asm_half(__gu_val, __gu_addr, err); break; \
290 case 4: __get_user_asm_word(__gu_val, __gu_addr, err); break; \
291 default: (__gu_val) = __get_user_bad(); \
292 } \
293 uaccess_restore(__ua_flags); \
294 (x) = (__typeof__(*(ptr)))__gu_val; \
295 } while (0)
296
297 #define __get_user_asm(x, addr, err, instr) \
298 __asm__ __volatile__( \
299 "1: " TUSER(instr) " %1, [%2], #0\n" \
300 "2:\n" \
301 " .pushsection .text.fixup,\"ax\"\n" \
302 " .align 2\n" \
303 "3: mov %0, %3\n" \
304 " mov %1, #0\n" \
305 " b 2b\n" \
306 " .popsection\n" \
307 " .pushsection __ex_table,\"a\"\n" \
308 " .align 3\n" \
309 " .long 1b, 3b\n" \
310 " .popsection" \
311 : "+r" (err), "=&r" (x) \
312 : "r" (addr), "i" (-EFAULT) \
313 : "cc")
314
315 #define __get_user_asm_byte(x, addr, err) \
316 __get_user_asm(x, addr, err, ldrb)
317
318 #ifndef __ARMEB__
319 #define __get_user_asm_half(x, __gu_addr, err) \
320 ({ \
321 unsigned long __b1, __b2; \
322 __get_user_asm_byte(__b1, __gu_addr, err); \
323 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
324 (x) = __b1 | (__b2 << 8); \
325 })
326 #else
327 #define __get_user_asm_half(x, __gu_addr, err) \
328 ({ \
329 unsigned long __b1, __b2; \
330 __get_user_asm_byte(__b1, __gu_addr, err); \
331 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
332 (x) = (__b1 << 8) | __b2; \
333 })
334 #endif
335
336 #define __get_user_asm_word(x, addr, err) \
337 __get_user_asm(x, addr, err, ldr)
338 #endif
339
340
341 #define __put_user_switch(x, ptr, __err, __fn) \
342 do { \
343 const __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
344 __typeof__(*(ptr)) __pu_val = (x); \
345 unsigned int __ua_flags; \
346 might_fault(); \
347 __ua_flags = uaccess_save_and_enable(); \
348 switch (sizeof(*(ptr))) { \
349 case 1: __fn(__pu_val, __pu_ptr, __err, 1); break; \
350 case 2: __fn(__pu_val, __pu_ptr, __err, 2); break; \
351 case 4: __fn(__pu_val, __pu_ptr, __err, 4); break; \
352 case 8: __fn(__pu_val, __pu_ptr, __err, 8); break; \
353 default: __err = __put_user_bad(); break; \
354 } \
355 uaccess_restore(__ua_flags); \
356 } while (0)
357
358 #define put_user(x, ptr) \
359 ({ \
360 int __pu_err = 0; \
361 __put_user_switch((x), (ptr), __pu_err, __put_user_check); \
362 __pu_err; \
363 })
364
365 #define __put_user(x, ptr) \
366 ({ \
367 long __pu_err = 0; \
368 __put_user_switch((x), (ptr), __pu_err, __put_user_nocheck); \
369 __pu_err; \
370 })
371
372 #define __put_user_error(x, ptr, err) \
373 ({ \
374 __put_user_switch((x), (ptr), (err), __put_user_nocheck); \
375 (void) 0; \
376 })
377
378 #define __put_user_nocheck(x, __pu_ptr, __err, __size) \
379 do { \
380 unsigned long __pu_addr = (unsigned long)__pu_ptr; \
381 __put_user_nocheck_##__size(x, __pu_addr, __err); \
382 } while (0)
383
384 #define __put_user_nocheck_1 __put_user_asm_byte
385 #define __put_user_nocheck_2 __put_user_asm_half
386 #define __put_user_nocheck_4 __put_user_asm_word
387 #define __put_user_nocheck_8 __put_user_asm_dword
388
389 #define __put_user_asm(x, __pu_addr, err, instr) \
390 __asm__ __volatile__( \
391 "1: " TUSER(instr) " %1, [%2], #0\n" \
392 "2:\n" \
393 " .pushsection .text.fixup,\"ax\"\n" \
394 " .align 2\n" \
395 "3: mov %0, %3\n" \
396 " b 2b\n" \
397 " .popsection\n" \
398 " .pushsection __ex_table,\"a\"\n" \
399 " .align 3\n" \
400 " .long 1b, 3b\n" \
401 " .popsection" \
402 : "+r" (err) \
403 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
404 : "cc")
405
406 #define __put_user_asm_byte(x, __pu_addr, err) \
407 __put_user_asm(x, __pu_addr, err, strb)
408
409 #ifndef __ARMEB__
410 #define __put_user_asm_half(x, __pu_addr, err) \
411 ({ \
412 unsigned long __temp = (__force unsigned long)(x); \
413 __put_user_asm_byte(__temp, __pu_addr, err); \
414 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
415 })
416 #else
417 #define __put_user_asm_half(x, __pu_addr, err) \
418 ({ \
419 unsigned long __temp = (__force unsigned long)(x); \
420 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
421 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
422 })
423 #endif
424
425 #define __put_user_asm_word(x, __pu_addr, err) \
426 __put_user_asm(x, __pu_addr, err, str)
427
428 #ifndef __ARMEB__
429 #define __reg_oper0 "%R2"
430 #define __reg_oper1 "%Q2"
431 #else
432 #define __reg_oper0 "%Q2"
433 #define __reg_oper1 "%R2"
434 #endif
435
436 #define __put_user_asm_dword(x, __pu_addr, err) \
437 __asm__ __volatile__( \
438 ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \
439 ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \
440 THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \
441 THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \
442 "3:\n" \
443 " .pushsection .text.fixup,\"ax\"\n" \
444 " .align 2\n" \
445 "4: mov %0, %3\n" \
446 " b 3b\n" \
447 " .popsection\n" \
448 " .pushsection __ex_table,\"a\"\n" \
449 " .align 3\n" \
450 " .long 1b, 4b\n" \
451 " .long 2b, 4b\n" \
452 " .popsection" \
453 : "+r" (err), "+r" (__pu_addr) \
454 : "r" (x), "i" (-EFAULT) \
455 : "cc")
456
457
458 #ifdef CONFIG_MMU
459 extern unsigned long __must_check
460 arm_copy_from_user(void *to, const void __user *from, unsigned long n);
461
462 static inline unsigned long __must_check
raw_copy_from_user(void * to,const void __user * from,unsigned long n)463 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
464 {
465 unsigned int __ua_flags;
466
467 __ua_flags = uaccess_save_and_enable();
468 n = arm_copy_from_user(to, from, n);
469 uaccess_restore(__ua_flags);
470 return n;
471 }
472
473 extern unsigned long __must_check
474 arm_copy_to_user(void __user *to, const void *from, unsigned long n);
475 extern unsigned long __must_check
476 __copy_to_user_std(void __user *to, const void *from, unsigned long n);
477
478 static inline unsigned long __must_check
raw_copy_to_user(void __user * to,const void * from,unsigned long n)479 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
480 {
481 #ifndef CONFIG_UACCESS_WITH_MEMCPY
482 unsigned int __ua_flags;
483 __ua_flags = uaccess_save_and_enable();
484 n = arm_copy_to_user(to, from, n);
485 uaccess_restore(__ua_flags);
486 return n;
487 #else
488 return arm_copy_to_user(to, from, n);
489 #endif
490 }
491
492 extern unsigned long __must_check
493 arm_clear_user(void __user *addr, unsigned long n);
494 extern unsigned long __must_check
495 __clear_user_std(void __user *addr, unsigned long n);
496
497 static inline unsigned long __must_check
__clear_user(void __user * addr,unsigned long n)498 __clear_user(void __user *addr, unsigned long n)
499 {
500 unsigned int __ua_flags = uaccess_save_and_enable();
501 n = arm_clear_user(addr, n);
502 uaccess_restore(__ua_flags);
503 return n;
504 }
505
506 #else
507 static inline unsigned long
raw_copy_from_user(void * to,const void __user * from,unsigned long n)508 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
509 {
510 memcpy(to, (const void __force *)from, n);
511 return 0;
512 }
513 static inline unsigned long
raw_copy_to_user(void __user * to,const void * from,unsigned long n)514 raw_copy_to_user(void __user *to, const void *from, unsigned long n)
515 {
516 memcpy((void __force *)to, from, n);
517 return 0;
518 }
519 #define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0)
520 #endif
521 #define INLINE_COPY_TO_USER
522 #define INLINE_COPY_FROM_USER
523
clear_user(void __user * to,unsigned long n)524 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
525 {
526 if (access_ok(VERIFY_WRITE, to, n))
527 n = __clear_user(to, n);
528 return n;
529 }
530
531 /* These are from lib/ code, and use __get_user() and friends */
532 extern long strncpy_from_user(char *dest, const char __user *src, long count);
533
534 extern __must_check long strnlen_user(const char __user *str, long n);
535
536 #endif /* _ASMARM_UACCESS_H */
537