2 * arch/arm/include/asm/uaccess.h
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.
8 #ifndef _ASMARM_UACCESS_H
9 #define _ASMARM_UACCESS_H
12 * User space memory access functions
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>
20 #include <asm/extable.h>
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.
28 static inline unsigned int uaccess_save_and_enable(void)
30 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
31 unsigned int old_domain
= get_domain();
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
));
43 static inline void uaccess_restore(unsigned int flags
)
45 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
46 /* Restore the user access mask */
52 * These two are intentionally not defined anywhere - if the kernel
53 * code generates any references to them, that's a bug.
55 extern int __get_user_bad(void);
56 extern int __put_user_bad(void);
59 * Note that this is actually 0x1,0000,0000
61 #define KERNEL_DS 0x00000000
62 #define get_ds() (KERNEL_DS)
66 #define USER_DS TASK_SIZE
67 #define get_fs() (current_thread_info()->addr_limit)
69 static inline void set_fs(mm_segment_t fs
)
71 current_thread_info()->addr_limit
= fs
;
72 modify_domain(DOMAIN_KERNEL
, fs
? DOMAIN_CLIENT
: DOMAIN_MANAGER
);
75 #define segment_eq(a, b) ((a) == (b))
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) \
88 * Single-value transfer routines. They automatically use the right
89 * size if we just have the right pointer type. Note that the functions
90 * which read from user space (*get_*) need to take care not to leak
91 * kernel data even if the calling code is buggy and fails to check
92 * the return value. This means zeroing out the destination variable
93 * or buffer on error. Normally this is done out of line by the
94 * fixup code, but there are a few places where it intrudes on the
95 * main code path. When we only write to user space, there is no
98 extern int __get_user_1(void *);
99 extern int __get_user_2(void *);
100 extern int __get_user_4(void *);
101 extern int __get_user_32t_8(void *);
102 extern int __get_user_8(void *);
103 extern int __get_user_64t_1(void *);
104 extern int __get_user_64t_2(void *);
105 extern int __get_user_64t_4(void *);
107 #define __GUP_CLOBBER_1 "lr", "cc"
108 #ifdef CONFIG_CPU_USE_DOMAINS
109 #define __GUP_CLOBBER_2 "ip", "lr", "cc"
111 #define __GUP_CLOBBER_2 "lr", "cc"
113 #define __GUP_CLOBBER_4 "lr", "cc"
114 #define __GUP_CLOBBER_32t_8 "lr", "cc"
115 #define __GUP_CLOBBER_8 "lr", "cc"
117 #define __get_user_x(__r2, __p, __e, __l, __s) \
118 __asm__ __volatile__ ( \
119 __asmeq("%0", "r0") __asmeq("%1", "r2") \
120 __asmeq("%3", "r1") \
121 "bl __get_user_" #__s \
122 : "=&r" (__e), "=r" (__r2) \
123 : "0" (__p), "r" (__l) \
124 : __GUP_CLOBBER_##__s)
126 /* narrowing a double-word get into a single 32bit word register: */
128 #define __get_user_x_32t(__r2, __p, __e, __l, __s) \
129 __get_user_x(__r2, __p, __e, __l, 32t_8)
131 #define __get_user_x_32t __get_user_x
135 * storing result into proper least significant word of 64bit target var,
136 * different only for big endian case where 64 bit __r2 lsw is r3:
139 #define __get_user_x_64t(__r2, __p, __e, __l, __s) \
140 __asm__ __volatile__ ( \
141 __asmeq("%0", "r0") __asmeq("%1", "r2") \
142 __asmeq("%3", "r1") \
143 "bl __get_user_64t_" #__s \
144 : "=&r" (__e), "=r" (__r2) \
145 : "0" (__p), "r" (__l) \
146 : __GUP_CLOBBER_##__s)
148 #define __get_user_x_64t __get_user_x
152 #define __get_user_check(x, p) \
154 unsigned long __limit = current_thread_info()->addr_limit - 1; \
155 register const typeof(*(p)) __user *__p asm("r0") = (p);\
156 register typeof(x) __r2 asm("r2"); \
157 register unsigned long __l asm("r1") = __limit; \
158 register int __e asm("r0"); \
159 unsigned int __ua_flags = uaccess_save_and_enable(); \
160 switch (sizeof(*(__p))) { \
162 if (sizeof((x)) >= 8) \
163 __get_user_x_64t(__r2, __p, __e, __l, 1); \
165 __get_user_x(__r2, __p, __e, __l, 1); \
168 if (sizeof((x)) >= 8) \
169 __get_user_x_64t(__r2, __p, __e, __l, 2); \
171 __get_user_x(__r2, __p, __e, __l, 2); \
174 if (sizeof((x)) >= 8) \
175 __get_user_x_64t(__r2, __p, __e, __l, 4); \
177 __get_user_x(__r2, __p, __e, __l, 4); \
180 if (sizeof((x)) < 8) \
181 __get_user_x_32t(__r2, __p, __e, __l, 4); \
183 __get_user_x(__r2, __p, __e, __l, 8); \
185 default: __e = __get_user_bad(); break; \
187 uaccess_restore(__ua_flags); \
188 x = (typeof(*(p))) __r2; \
192 #define get_user(x, p) \
195 __get_user_check(x, p); \
198 extern int __put_user_1(void *, unsigned int);
199 extern int __put_user_2(void *, unsigned int);
200 extern int __put_user_4(void *, unsigned int);
201 extern int __put_user_8(void *, unsigned long long);
203 #define __put_user_check(__pu_val, __ptr, __err, __s) \
205 unsigned long __limit = current_thread_info()->addr_limit - 1; \
206 register typeof(__pu_val) __r2 asm("r2") = __pu_val; \
207 register const void __user *__p asm("r0") = __ptr; \
208 register unsigned long __l asm("r1") = __limit; \
209 register int __e asm("r0"); \
210 __asm__ __volatile__ ( \
211 __asmeq("%0", "r0") __asmeq("%2", "r2") \
212 __asmeq("%3", "r1") \
213 "bl __put_user_" #__s \
215 : "0" (__p), "r" (__r2), "r" (__l) \
216 : "ip", "lr", "cc"); \
220 #else /* CONFIG_MMU */
223 * uClinux has only one addr space, so has simplified address limits.
225 #define USER_DS KERNEL_DS
227 #define segment_eq(a, b) (1)
228 #define __addr_ok(addr) ((void)(addr), 1)
229 #define __range_ok(addr, size) ((void)(addr), 0)
230 #define get_fs() (KERNEL_DS)
232 static inline void set_fs(mm_segment_t fs
)
236 #define get_user(x, p) __get_user(x, p)
237 #define __put_user_check __put_user_nocheck
239 #endif /* CONFIG_MMU */
241 #define access_ok(type, addr, size) (__range_ok(addr, size) == 0)
243 #define user_addr_max() \
244 (uaccess_kernel() ? ~0UL : get_fs())
247 * The "__xxx" versions of the user access functions do not verify the
248 * address space - it must have been done previously with a separate
249 * "access_ok()" call.
251 * The "xxx_error" versions set the third argument to EFAULT if an
252 * error occurs, and leave it unchanged on success. Note that these
253 * versions are void (ie, don't return a value as such).
255 #define __get_user(x, ptr) \
258 __get_user_err((x), (ptr), __gu_err); \
262 #define __get_user_error(x, ptr, err) \
264 __get_user_err((x), (ptr), err); \
268 #define __get_user_err(x, ptr, err) \
270 unsigned long __gu_addr = (unsigned long)(ptr); \
271 unsigned long __gu_val; \
272 unsigned int __ua_flags; \
273 __chk_user_ptr(ptr); \
275 __ua_flags = uaccess_save_and_enable(); \
276 switch (sizeof(*(ptr))) { \
277 case 1: __get_user_asm_byte(__gu_val, __gu_addr, err); break; \
278 case 2: __get_user_asm_half(__gu_val, __gu_addr, err); break; \
279 case 4: __get_user_asm_word(__gu_val, __gu_addr, err); break; \
280 default: (__gu_val) = __get_user_bad(); \
282 uaccess_restore(__ua_flags); \
283 (x) = (__typeof__(*(ptr)))__gu_val; \
286 #define __get_user_asm(x, addr, err, instr) \
287 __asm__ __volatile__( \
288 "1: " TUSER(instr) " %1, [%2], #0\n" \
290 " .pushsection .text.fixup,\"ax\"\n" \
296 " .pushsection __ex_table,\"a\"\n" \
300 : "+r" (err), "=&r" (x) \
301 : "r" (addr), "i" (-EFAULT) \
304 #define __get_user_asm_byte(x, addr, err) \
305 __get_user_asm(x, addr, err, ldrb)
308 #define __get_user_asm_half(x, __gu_addr, err) \
310 unsigned long __b1, __b2; \
311 __get_user_asm_byte(__b1, __gu_addr, err); \
312 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
313 (x) = __b1 | (__b2 << 8); \
316 #define __get_user_asm_half(x, __gu_addr, err) \
318 unsigned long __b1, __b2; \
319 __get_user_asm_byte(__b1, __gu_addr, err); \
320 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
321 (x) = (__b1 << 8) | __b2; \
325 #define __get_user_asm_word(x, addr, err) \
326 __get_user_asm(x, addr, err, ldr)
329 #define __put_user_switch(x, ptr, __err, __fn) \
331 const __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
332 __typeof__(*(ptr)) __pu_val = (x); \
333 unsigned int __ua_flags; \
335 __ua_flags = uaccess_save_and_enable(); \
336 switch (sizeof(*(ptr))) { \
337 case 1: __fn(__pu_val, __pu_ptr, __err, 1); break; \
338 case 2: __fn(__pu_val, __pu_ptr, __err, 2); break; \
339 case 4: __fn(__pu_val, __pu_ptr, __err, 4); break; \
340 case 8: __fn(__pu_val, __pu_ptr, __err, 8); break; \
341 default: __err = __put_user_bad(); break; \
343 uaccess_restore(__ua_flags); \
346 #define put_user(x, ptr) \
349 __put_user_switch((x), (ptr), __pu_err, __put_user_check); \
353 #define __put_user(x, ptr) \
356 __put_user_switch((x), (ptr), __pu_err, __put_user_nocheck); \
360 #define __put_user_error(x, ptr, err) \
362 __put_user_switch((x), (ptr), (err), __put_user_nocheck); \
366 #define __put_user_nocheck(x, __pu_ptr, __err, __size) \
368 unsigned long __pu_addr = (unsigned long)__pu_ptr; \
369 __put_user_nocheck_##__size(x, __pu_addr, __err); \
372 #define __put_user_nocheck_1 __put_user_asm_byte
373 #define __put_user_nocheck_2 __put_user_asm_half
374 #define __put_user_nocheck_4 __put_user_asm_word
375 #define __put_user_nocheck_8 __put_user_asm_dword
377 #define __put_user_asm(x, __pu_addr, err, instr) \
378 __asm__ __volatile__( \
379 "1: " TUSER(instr) " %1, [%2], #0\n" \
381 " .pushsection .text.fixup,\"ax\"\n" \
386 " .pushsection __ex_table,\"a\"\n" \
391 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
394 #define __put_user_asm_byte(x, __pu_addr, err) \
395 __put_user_asm(x, __pu_addr, err, strb)
398 #define __put_user_asm_half(x, __pu_addr, err) \
400 unsigned long __temp = (__force unsigned long)(x); \
401 __put_user_asm_byte(__temp, __pu_addr, err); \
402 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
405 #define __put_user_asm_half(x, __pu_addr, err) \
407 unsigned long __temp = (__force unsigned long)(x); \
408 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
409 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
413 #define __put_user_asm_word(x, __pu_addr, err) \
414 __put_user_asm(x, __pu_addr, err, str)
417 #define __reg_oper0 "%R2"
418 #define __reg_oper1 "%Q2"
420 #define __reg_oper0 "%Q2"
421 #define __reg_oper1 "%R2"
424 #define __put_user_asm_dword(x, __pu_addr, err) \
425 __asm__ __volatile__( \
426 ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \
427 ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \
428 THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \
429 THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \
431 " .pushsection .text.fixup,\"ax\"\n" \
436 " .pushsection __ex_table,\"a\"\n" \
441 : "+r" (err), "+r" (__pu_addr) \
442 : "r" (x), "i" (-EFAULT) \
447 extern unsigned long __must_check
448 arm_copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
450 static inline unsigned long __must_check
451 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
453 unsigned int __ua_flags
;
455 __ua_flags
= uaccess_save_and_enable();
456 n
= arm_copy_from_user(to
, from
, n
);
457 uaccess_restore(__ua_flags
);
461 extern unsigned long __must_check
462 arm_copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
463 extern unsigned long __must_check
464 __copy_to_user_std(void __user
*to
, const void *from
, unsigned long n
);
466 static inline unsigned long __must_check
467 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
469 #ifndef CONFIG_UACCESS_WITH_MEMCPY
470 unsigned int __ua_flags
;
471 __ua_flags
= uaccess_save_and_enable();
472 n
= arm_copy_to_user(to
, from
, n
);
473 uaccess_restore(__ua_flags
);
476 return arm_copy_to_user(to
, from
, n
);
480 extern unsigned long __must_check
481 arm_clear_user(void __user
*addr
, unsigned long n
);
482 extern unsigned long __must_check
483 __clear_user_std(void __user
*addr
, unsigned long n
);
485 static inline unsigned long __must_check
486 __clear_user(void __user
*addr
, unsigned long n
)
488 unsigned int __ua_flags
= uaccess_save_and_enable();
489 n
= arm_clear_user(addr
, n
);
490 uaccess_restore(__ua_flags
);
495 static inline unsigned long
496 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
498 memcpy(to
, (const void __force
*)from
, n
);
501 static inline unsigned long
502 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
504 memcpy((void __force
*)to
, from
, n
);
507 #define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0)
509 #define INLINE_COPY_TO_USER
510 #define INLINE_COPY_FROM_USER
512 static inline unsigned long __must_check
clear_user(void __user
*to
, unsigned long n
)
514 if (access_ok(VERIFY_WRITE
, to
, n
))
515 n
= __clear_user(to
, n
);
519 /* These are from lib/ code, and use __get_user() and friends */
520 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
522 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
524 #endif /* _ASMARM_UACCESS_H */