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