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