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