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