uaccess.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/uaccess.h
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. */
  7. #ifndef __ASM_UACCESS_H
  8. #define __ASM_UACCESS_H
  9. #include <asm/alternative.h>
  10. #include <asm/kernel-pgtable.h>
  11. #include <asm/sysreg.h>
  12. /*
  13. * User space memory access functions
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/kasan-checks.h>
  17. #include <linux/string.h>
  18. #include <asm/asm-extable.h>
  19. #include <asm/cpufeature.h>
  20. #include <asm/mmu.h>
  21. #include <asm/mte.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/memory.h>
  24. #include <asm/extable.h>
  25. static inline int __access_ok(const void __user *ptr, unsigned long size);
  26. /*
  27. * Test whether a block of memory is a valid user space address.
  28. * Returns 1 if the range is valid, 0 otherwise.
  29. *
  30. * This is equivalent to the following test:
  31. * (u65)addr + (u65)size <= (u65)TASK_SIZE_MAX
  32. */
  33. static inline int access_ok(const void __user *addr, unsigned long size)
  34. {
  35. /*
  36. * Asynchronous I/O running in a kernel thread does not have the
  37. * TIF_TAGGED_ADDR flag of the process owning the mm, so always untag
  38. * the user address before checking.
  39. */
  40. if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI) &&
  41. (current->flags & PF_KTHREAD || test_thread_flag(TIF_TAGGED_ADDR)))
  42. addr = untagged_addr(addr);
  43. return likely(__access_ok(addr, size));
  44. }
  45. #define access_ok access_ok
  46. #include <asm-generic/access_ok.h>
  47. /*
  48. * User access enabling/disabling.
  49. */
  50. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  51. static inline void __uaccess_ttbr0_disable(void)
  52. {
  53. unsigned long flags, ttbr;
  54. local_irq_save(flags);
  55. ttbr = read_sysreg(ttbr1_el1);
  56. ttbr &= ~TTBR_ASID_MASK;
  57. /* reserved_pg_dir placed before swapper_pg_dir */
  58. write_sysreg(ttbr - RESERVED_SWAPPER_OFFSET, ttbr0_el1);
  59. /* Set reserved ASID */
  60. write_sysreg(ttbr, ttbr1_el1);
  61. isb();
  62. local_irq_restore(flags);
  63. }
  64. static inline void __uaccess_ttbr0_enable(void)
  65. {
  66. unsigned long flags, ttbr0, ttbr1;
  67. /*
  68. * Disable interrupts to avoid preemption between reading the 'ttbr0'
  69. * variable and the MSR. A context switch could trigger an ASID
  70. * roll-over and an update of 'ttbr0'.
  71. */
  72. local_irq_save(flags);
  73. ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
  74. /* Restore active ASID */
  75. ttbr1 = read_sysreg(ttbr1_el1);
  76. ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */
  77. ttbr1 |= ttbr0 & TTBR_ASID_MASK;
  78. write_sysreg(ttbr1, ttbr1_el1);
  79. /* Restore user page table */
  80. write_sysreg(ttbr0, ttbr0_el1);
  81. isb();
  82. local_irq_restore(flags);
  83. }
  84. static inline bool uaccess_ttbr0_disable(void)
  85. {
  86. if (!system_uses_ttbr0_pan())
  87. return false;
  88. __uaccess_ttbr0_disable();
  89. return true;
  90. }
  91. static inline bool uaccess_ttbr0_enable(void)
  92. {
  93. if (!system_uses_ttbr0_pan())
  94. return false;
  95. __uaccess_ttbr0_enable();
  96. return true;
  97. }
  98. #else
  99. static inline bool uaccess_ttbr0_disable(void)
  100. {
  101. return false;
  102. }
  103. static inline bool uaccess_ttbr0_enable(void)
  104. {
  105. return false;
  106. }
  107. #endif
  108. static inline void __uaccess_disable_hw_pan(void)
  109. {
  110. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,
  111. CONFIG_ARM64_PAN));
  112. }
  113. static inline void __uaccess_enable_hw_pan(void)
  114. {
  115. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
  116. CONFIG_ARM64_PAN));
  117. }
  118. static inline void uaccess_disable_privileged(void)
  119. {
  120. mte_disable_tco();
  121. if (uaccess_ttbr0_disable())
  122. return;
  123. __uaccess_enable_hw_pan();
  124. }
  125. static inline void uaccess_enable_privileged(void)
  126. {
  127. mte_enable_tco();
  128. if (uaccess_ttbr0_enable())
  129. return;
  130. __uaccess_disable_hw_pan();
  131. }
  132. /*
  133. * Sanitize a uaccess pointer such that it cannot reach any kernel address.
  134. *
  135. * Clearing bit 55 ensures the pointer cannot address any portion of the TTBR1
  136. * address range (i.e. any kernel address), and either the pointer falls within
  137. * the TTBR0 address range or must cause a fault.
  138. */
  139. #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
  140. static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
  141. {
  142. void __user *safe_ptr;
  143. asm volatile(
  144. " bic %0, %1, %2\n"
  145. : "=r" (safe_ptr)
  146. : "r" (ptr),
  147. "i" (BIT(55))
  148. );
  149. return safe_ptr;
  150. }
  151. /*
  152. * The "__xxx" versions of the user access functions do not verify the address
  153. * space - it must have been done previously with a separate "access_ok()"
  154. * call.
  155. *
  156. * The "__xxx_error" versions set the third argument to -EFAULT if an error
  157. * occurs, and leave it unchanged on success.
  158. */
  159. #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
  160. #define __get_mem_asm(load, reg, x, addr, label, type) \
  161. asm_goto_output( \
  162. "1: " load " " reg "0, [%1]\n" \
  163. _ASM_EXTABLE_##type##ACCESS(1b, %l2) \
  164. : "=r" (x) \
  165. : "r" (addr) : : label)
  166. #else
  167. #define __get_mem_asm(load, reg, x, addr, label, type) do { \
  168. int __gma_err = 0; \
  169. asm volatile( \
  170. "1: " load " " reg "1, [%2]\n" \
  171. "2:\n" \
  172. _ASM_EXTABLE_##type##ACCESS_ERR_ZERO(1b, 2b, %w0, %w1) \
  173. : "+r" (__gma_err), "=r" (x) \
  174. : "r" (addr)); \
  175. if (__gma_err) goto label; } while (0)
  176. #endif
  177. #define __raw_get_mem(ldr, x, ptr, label, type) \
  178. do { \
  179. unsigned long __gu_val; \
  180. switch (sizeof(*(ptr))) { \
  181. case 1: \
  182. __get_mem_asm(ldr "b", "%w", __gu_val, (ptr), label, type); \
  183. break; \
  184. case 2: \
  185. __get_mem_asm(ldr "h", "%w", __gu_val, (ptr), label, type); \
  186. break; \
  187. case 4: \
  188. __get_mem_asm(ldr, "%w", __gu_val, (ptr), label, type); \
  189. break; \
  190. case 8: \
  191. __get_mem_asm(ldr, "%x", __gu_val, (ptr), label, type); \
  192. break; \
  193. default: \
  194. BUILD_BUG(); \
  195. } \
  196. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  197. } while (0)
  198. /*
  199. * We must not call into the scheduler between uaccess_ttbr0_enable() and
  200. * uaccess_ttbr0_disable(). As `x` and `ptr` could contain blocking functions,
  201. * we must evaluate these outside of the critical section.
  202. */
  203. #define __raw_get_user(x, ptr, label) \
  204. do { \
  205. __typeof__(*(ptr)) __user *__rgu_ptr = (ptr); \
  206. __typeof__(x) __rgu_val; \
  207. __chk_user_ptr(ptr); \
  208. do { \
  209. __label__ __rgu_failed; \
  210. uaccess_ttbr0_enable(); \
  211. __raw_get_mem("ldtr", __rgu_val, __rgu_ptr, __rgu_failed, U); \
  212. uaccess_ttbr0_disable(); \
  213. (x) = __rgu_val; \
  214. break; \
  215. __rgu_failed: \
  216. uaccess_ttbr0_disable(); \
  217. goto label; \
  218. } while (0); \
  219. } while (0)
  220. #define __get_user_error(x, ptr, err) \
  221. do { \
  222. __label__ __gu_failed; \
  223. __typeof__(*(ptr)) __user *__p = (ptr); \
  224. might_fault(); \
  225. if (access_ok(__p, sizeof(*__p))) { \
  226. __p = uaccess_mask_ptr(__p); \
  227. __raw_get_user((x), __p, __gu_failed); \
  228. } else { \
  229. __gu_failed: \
  230. (x) = (__force __typeof__(x))0; (err) = -EFAULT; \
  231. } \
  232. } while (0)
  233. #define __get_user(x, ptr) \
  234. ({ \
  235. int __gu_err = 0; \
  236. __get_user_error((x), (ptr), __gu_err); \
  237. __gu_err; \
  238. })
  239. #define get_user __get_user
  240. /*
  241. * We must not call into the scheduler between __mte_enable_tco_async() and
  242. * __mte_disable_tco_async(). As `dst` and `src` may contain blocking
  243. * functions, we must evaluate these outside of the critical section.
  244. */
  245. #define __get_kernel_nofault(dst, src, type, err_label) \
  246. do { \
  247. __typeof__(dst) __gkn_dst = (dst); \
  248. __typeof__(src) __gkn_src = (src); \
  249. do { \
  250. __label__ __gkn_label; \
  251. \
  252. __mte_enable_tco_async(); \
  253. __raw_get_mem("ldr", *((type *)(__gkn_dst)), \
  254. (__force type *)(__gkn_src), __gkn_label, K); \
  255. __mte_disable_tco_async(); \
  256. break; \
  257. __gkn_label: \
  258. __mte_disable_tco_async(); \
  259. goto err_label; \
  260. } while (0); \
  261. } while (0)
  262. #define __put_mem_asm(store, reg, x, addr, label, type) \
  263. asm goto( \
  264. "1: " store " " reg "0, [%1]\n" \
  265. "2:\n" \
  266. _ASM_EXTABLE_##type##ACCESS(1b, %l2) \
  267. : : "rZ" (x), "r" (addr) : : label)
  268. #define __raw_put_mem(str, x, ptr, label, type) \
  269. do { \
  270. __typeof__(*(ptr)) __pu_val = (x); \
  271. switch (sizeof(*(ptr))) { \
  272. case 1: \
  273. __put_mem_asm(str "b", "%w", __pu_val, (ptr), label, type); \
  274. break; \
  275. case 2: \
  276. __put_mem_asm(str "h", "%w", __pu_val, (ptr), label, type); \
  277. break; \
  278. case 4: \
  279. __put_mem_asm(str, "%w", __pu_val, (ptr), label, type); \
  280. break; \
  281. case 8: \
  282. __put_mem_asm(str, "%x", __pu_val, (ptr), label, type); \
  283. break; \
  284. default: \
  285. BUILD_BUG(); \
  286. } \
  287. } while (0)
  288. /*
  289. * We must not call into the scheduler between uaccess_ttbr0_enable() and
  290. * uaccess_ttbr0_disable(). As `x` and `ptr` could contain blocking functions,
  291. * we must evaluate these outside of the critical section.
  292. */
  293. #define __raw_put_user(x, ptr, label) \
  294. do { \
  295. __label__ __rpu_failed; \
  296. __typeof__(*(ptr)) __user *__rpu_ptr = (ptr); \
  297. __typeof__(*(ptr)) __rpu_val = (x); \
  298. __chk_user_ptr(__rpu_ptr); \
  299. \
  300. do { \
  301. uaccess_ttbr0_enable(); \
  302. __raw_put_mem("sttr", __rpu_val, __rpu_ptr, __rpu_failed, U); \
  303. uaccess_ttbr0_disable(); \
  304. break; \
  305. __rpu_failed: \
  306. uaccess_ttbr0_disable(); \
  307. goto label; \
  308. } while (0); \
  309. } while (0)
  310. #define __put_user_error(x, ptr, err) \
  311. do { \
  312. __label__ __pu_failed; \
  313. __typeof__(*(ptr)) __user *__p = (ptr); \
  314. might_fault(); \
  315. if (access_ok(__p, sizeof(*__p))) { \
  316. __p = uaccess_mask_ptr(__p); \
  317. __raw_put_user((x), __p, __pu_failed); \
  318. } else { \
  319. __pu_failed: \
  320. (err) = -EFAULT; \
  321. } \
  322. } while (0)
  323. #define __put_user(x, ptr) \
  324. ({ \
  325. int __pu_err = 0; \
  326. __put_user_error((x), (ptr), __pu_err); \
  327. __pu_err; \
  328. })
  329. #define put_user __put_user
  330. /*
  331. * We must not call into the scheduler between __mte_enable_tco_async() and
  332. * __mte_disable_tco_async(). As `dst` and `src` may contain blocking
  333. * functions, we must evaluate these outside of the critical section.
  334. */
  335. #define __put_kernel_nofault(dst, src, type, err_label) \
  336. do { \
  337. __typeof__(dst) __pkn_dst = (dst); \
  338. __typeof__(src) __pkn_src = (src); \
  339. \
  340. do { \
  341. __label__ __pkn_err; \
  342. __mte_enable_tco_async(); \
  343. __raw_put_mem("str", *((type *)(__pkn_src)), \
  344. (__force type *)(__pkn_dst), __pkn_err, K); \
  345. __mte_disable_tco_async(); \
  346. break; \
  347. __pkn_err: \
  348. __mte_disable_tco_async(); \
  349. goto err_label; \
  350. } while (0); \
  351. } while(0)
  352. extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
  353. #define raw_copy_from_user(to, from, n) \
  354. ({ \
  355. unsigned long __acfu_ret; \
  356. uaccess_ttbr0_enable(); \
  357. __acfu_ret = __arch_copy_from_user((to), \
  358. __uaccess_mask_ptr(from), (n)); \
  359. uaccess_ttbr0_disable(); \
  360. __acfu_ret; \
  361. })
  362. extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
  363. #define raw_copy_to_user(to, from, n) \
  364. ({ \
  365. unsigned long __actu_ret; \
  366. uaccess_ttbr0_enable(); \
  367. __actu_ret = __arch_copy_to_user(__uaccess_mask_ptr(to), \
  368. (from), (n)); \
  369. uaccess_ttbr0_disable(); \
  370. __actu_ret; \
  371. })
  372. static __must_check __always_inline bool user_access_begin(const void __user *ptr, size_t len)
  373. {
  374. if (unlikely(!access_ok(ptr,len)))
  375. return 0;
  376. uaccess_ttbr0_enable();
  377. return 1;
  378. }
  379. #define user_access_begin(a,b) user_access_begin(a,b)
  380. #define user_access_end() uaccess_ttbr0_disable()
  381. #define unsafe_put_user(x, ptr, label) \
  382. __raw_put_mem("sttr", x, uaccess_mask_ptr(ptr), label, U)
  383. #define unsafe_get_user(x, ptr, label) \
  384. __raw_get_mem("ldtr", x, uaccess_mask_ptr(ptr), label, U)
  385. /*
  386. * KCSAN uses these to save and restore ttbr state.
  387. * We do not support KCSAN with ARM64_SW_TTBR0_PAN, so
  388. * they are no-ops.
  389. */
  390. static inline unsigned long user_access_save(void) { return 0; }
  391. static inline void user_access_restore(unsigned long enabled) { }
  392. /*
  393. * We want the unsafe accessors to always be inlined and use
  394. * the error labels - thus the macro games.
  395. */
  396. #define unsafe_copy_loop(dst, src, len, type, label) \
  397. while (len >= sizeof(type)) { \
  398. unsafe_put_user(*(type *)(src),(type __user *)(dst),label); \
  399. dst += sizeof(type); \
  400. src += sizeof(type); \
  401. len -= sizeof(type); \
  402. }
  403. #define unsafe_copy_to_user(_dst,_src,_len,label) \
  404. do { \
  405. char __user *__ucu_dst = (_dst); \
  406. const char *__ucu_src = (_src); \
  407. size_t __ucu_len = (_len); \
  408. unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, label); \
  409. unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, label); \
  410. unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, label); \
  411. unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label); \
  412. } while (0)
  413. #define INLINE_COPY_TO_USER
  414. #define INLINE_COPY_FROM_USER
  415. extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n);
  416. static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n)
  417. {
  418. if (access_ok(to, n)) {
  419. uaccess_ttbr0_enable();
  420. n = __arch_clear_user(__uaccess_mask_ptr(to), n);
  421. uaccess_ttbr0_disable();
  422. }
  423. return n;
  424. }
  425. #define clear_user __clear_user
  426. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  427. extern __must_check long strnlen_user(const char __user *str, long n);
  428. #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
  429. extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
  430. static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
  431. {
  432. kasan_check_write(dst, size);
  433. return __copy_user_flushcache(dst, __uaccess_mask_ptr(src), size);
  434. }
  435. #endif
  436. #ifdef CONFIG_ARCH_HAS_SUBPAGE_FAULTS
  437. /*
  438. * Return 0 on success, the number of bytes not probed otherwise.
  439. */
  440. static inline size_t probe_subpage_writeable(const char __user *uaddr,
  441. size_t size)
  442. {
  443. if (!system_supports_mte())
  444. return 0;
  445. return mte_probe_user_range(uaddr, size);
  446. }
  447. #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
  448. #endif /* __ASM_UACCESS_H */