uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. isb();
  60. /* Set reserved ASID */
  61. write_sysreg(ttbr, ttbr1_el1);
  62. isb();
  63. local_irq_restore(flags);
  64. }
  65. static inline void __uaccess_ttbr0_enable(void)
  66. {
  67. unsigned long flags, ttbr0, ttbr1;
  68. /*
  69. * Disable interrupts to avoid preemption between reading the 'ttbr0'
  70. * variable and the MSR. A context switch could trigger an ASID
  71. * roll-over and an update of 'ttbr0'.
  72. */
  73. local_irq_save(flags);
  74. ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
  75. /* Restore active ASID */
  76. ttbr1 = read_sysreg(ttbr1_el1);
  77. ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */
  78. ttbr1 |= ttbr0 & TTBR_ASID_MASK;
  79. write_sysreg(ttbr1, ttbr1_el1);
  80. isb();
  81. /* Restore user page table */
  82. write_sysreg(ttbr0, ttbr0_el1);
  83. isb();
  84. local_irq_restore(flags);
  85. }
  86. static inline bool uaccess_ttbr0_disable(void)
  87. {
  88. if (!system_uses_ttbr0_pan())
  89. return false;
  90. __uaccess_ttbr0_disable();
  91. return true;
  92. }
  93. static inline bool uaccess_ttbr0_enable(void)
  94. {
  95. if (!system_uses_ttbr0_pan())
  96. return false;
  97. __uaccess_ttbr0_enable();
  98. return true;
  99. }
  100. #else
  101. static inline bool uaccess_ttbr0_disable(void)
  102. {
  103. return false;
  104. }
  105. static inline bool uaccess_ttbr0_enable(void)
  106. {
  107. return false;
  108. }
  109. #endif
  110. static inline void __uaccess_disable_hw_pan(void)
  111. {
  112. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,
  113. CONFIG_ARM64_PAN));
  114. }
  115. static inline void __uaccess_enable_hw_pan(void)
  116. {
  117. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
  118. CONFIG_ARM64_PAN));
  119. }
  120. static inline void uaccess_disable_privileged(void)
  121. {
  122. mte_disable_tco();
  123. if (uaccess_ttbr0_disable())
  124. return;
  125. __uaccess_enable_hw_pan();
  126. }
  127. static inline void uaccess_enable_privileged(void)
  128. {
  129. mte_enable_tco();
  130. if (uaccess_ttbr0_enable())
  131. return;
  132. __uaccess_disable_hw_pan();
  133. }
  134. /*
  135. * Sanitize a uaccess pointer such that it cannot reach any kernel address.
  136. *
  137. * Clearing bit 55 ensures the pointer cannot address any portion of the TTBR1
  138. * address range (i.e. any kernel address), and either the pointer falls within
  139. * the TTBR0 address range or must cause a fault.
  140. */
  141. #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
  142. static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
  143. {
  144. void __user *safe_ptr;
  145. asm volatile(
  146. " bic %0, %1, %2\n"
  147. : "=r" (safe_ptr)
  148. : "r" (ptr),
  149. "i" (BIT(55))
  150. );
  151. return safe_ptr;
  152. }
  153. /*
  154. * The "__xxx" versions of the user access functions do not verify the address
  155. * space - it must have been done previously with a separate "access_ok()"
  156. * call.
  157. *
  158. * The "__xxx_error" versions set the third argument to -EFAULT if an error
  159. * occurs, and leave it unchanged on success.
  160. */
  161. #define __get_mem_asm(load, reg, x, addr, err, type) \
  162. asm volatile( \
  163. "1: " load " " reg "1, [%2]\n" \
  164. "2:\n" \
  165. _ASM_EXTABLE_##type##ACCESS_ERR_ZERO(1b, 2b, %w0, %w1) \
  166. : "+r" (err), "=&r" (x) \
  167. : "r" (addr))
  168. #define __raw_get_mem(ldr, x, ptr, err, type) \
  169. do { \
  170. unsigned long __gu_val; \
  171. switch (sizeof(*(ptr))) { \
  172. case 1: \
  173. __get_mem_asm(ldr "b", "%w", __gu_val, (ptr), (err), type); \
  174. break; \
  175. case 2: \
  176. __get_mem_asm(ldr "h", "%w", __gu_val, (ptr), (err), type); \
  177. break; \
  178. case 4: \
  179. __get_mem_asm(ldr, "%w", __gu_val, (ptr), (err), type); \
  180. break; \
  181. case 8: \
  182. __get_mem_asm(ldr, "%x", __gu_val, (ptr), (err), type); \
  183. break; \
  184. default: \
  185. BUILD_BUG(); \
  186. } \
  187. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  188. } while (0)
  189. /*
  190. * We must not call into the scheduler between uaccess_ttbr0_enable() and
  191. * uaccess_ttbr0_disable(). As `x` and `ptr` could contain blocking functions,
  192. * we must evaluate these outside of the critical section.
  193. */
  194. #define __raw_get_user(x, ptr, err) \
  195. do { \
  196. __typeof__(*(ptr)) __user *__rgu_ptr = (ptr); \
  197. __typeof__(x) __rgu_val; \
  198. __chk_user_ptr(ptr); \
  199. \
  200. uaccess_ttbr0_enable(); \
  201. __raw_get_mem("ldtr", __rgu_val, __rgu_ptr, err, U); \
  202. uaccess_ttbr0_disable(); \
  203. \
  204. (x) = __rgu_val; \
  205. } while (0)
  206. #define __get_user_error(x, ptr, err) \
  207. do { \
  208. __typeof__(*(ptr)) __user *__p = (ptr); \
  209. might_fault(); \
  210. if (access_ok(__p, sizeof(*__p))) { \
  211. __p = uaccess_mask_ptr(__p); \
  212. __raw_get_user((x), __p, (err)); \
  213. } else { \
  214. (x) = (__force __typeof__(x))0; (err) = -EFAULT; \
  215. } \
  216. } while (0)
  217. #define __get_user(x, ptr) \
  218. ({ \
  219. int __gu_err = 0; \
  220. __get_user_error((x), (ptr), __gu_err); \
  221. __gu_err; \
  222. })
  223. #define get_user __get_user
  224. /*
  225. * We must not call into the scheduler between __mte_enable_tco_async() and
  226. * __mte_disable_tco_async(). As `dst` and `src` may contain blocking
  227. * functions, we must evaluate these outside of the critical section.
  228. */
  229. #define __get_kernel_nofault(dst, src, type, err_label) \
  230. do { \
  231. __typeof__(dst) __gkn_dst = (dst); \
  232. __typeof__(src) __gkn_src = (src); \
  233. int __gkn_err = 0; \
  234. \
  235. __mte_enable_tco_async(); \
  236. __raw_get_mem("ldr", *((type *)(__gkn_dst)), \
  237. (__force type *)(__gkn_src), __gkn_err, K); \
  238. __mte_disable_tco_async(); \
  239. \
  240. if (unlikely(__gkn_err)) \
  241. goto err_label; \
  242. } while (0)
  243. #define __put_mem_asm(store, reg, x, addr, err, type) \
  244. asm volatile( \
  245. "1: " store " " reg "1, [%2]\n" \
  246. "2:\n" \
  247. _ASM_EXTABLE_##type##ACCESS_ERR(1b, 2b, %w0) \
  248. : "+r" (err) \
  249. : "r" (x), "r" (addr))
  250. #define __raw_put_mem(str, x, ptr, err, type) \
  251. do { \
  252. __typeof__(*(ptr)) __pu_val = (x); \
  253. switch (sizeof(*(ptr))) { \
  254. case 1: \
  255. __put_mem_asm(str "b", "%w", __pu_val, (ptr), (err), type); \
  256. break; \
  257. case 2: \
  258. __put_mem_asm(str "h", "%w", __pu_val, (ptr), (err), type); \
  259. break; \
  260. case 4: \
  261. __put_mem_asm(str, "%w", __pu_val, (ptr), (err), type); \
  262. break; \
  263. case 8: \
  264. __put_mem_asm(str, "%x", __pu_val, (ptr), (err), type); \
  265. break; \
  266. default: \
  267. BUILD_BUG(); \
  268. } \
  269. } while (0)
  270. /*
  271. * We must not call into the scheduler between uaccess_ttbr0_enable() and
  272. * uaccess_ttbr0_disable(). As `x` and `ptr` could contain blocking functions,
  273. * we must evaluate these outside of the critical section.
  274. */
  275. #define __raw_put_user(x, ptr, err) \
  276. do { \
  277. __typeof__(*(ptr)) __user *__rpu_ptr = (ptr); \
  278. __typeof__(*(ptr)) __rpu_val = (x); \
  279. __chk_user_ptr(__rpu_ptr); \
  280. \
  281. uaccess_ttbr0_enable(); \
  282. __raw_put_mem("sttr", __rpu_val, __rpu_ptr, err, U); \
  283. uaccess_ttbr0_disable(); \
  284. } while (0)
  285. #define __put_user_error(x, ptr, err) \
  286. do { \
  287. __typeof__(*(ptr)) __user *__p = (ptr); \
  288. might_fault(); \
  289. if (access_ok(__p, sizeof(*__p))) { \
  290. __p = uaccess_mask_ptr(__p); \
  291. __raw_put_user((x), __p, (err)); \
  292. } else { \
  293. (err) = -EFAULT; \
  294. } \
  295. } while (0)
  296. #define __put_user(x, ptr) \
  297. ({ \
  298. int __pu_err = 0; \
  299. __put_user_error((x), (ptr), __pu_err); \
  300. __pu_err; \
  301. })
  302. #define put_user __put_user
  303. /*
  304. * We must not call into the scheduler between __mte_enable_tco_async() and
  305. * __mte_disable_tco_async(). As `dst` and `src` may contain blocking
  306. * functions, we must evaluate these outside of the critical section.
  307. */
  308. #define __put_kernel_nofault(dst, src, type, err_label) \
  309. do { \
  310. __typeof__(dst) __pkn_dst = (dst); \
  311. __typeof__(src) __pkn_src = (src); \
  312. int __pkn_err = 0; \
  313. \
  314. __mte_enable_tco_async(); \
  315. __raw_put_mem("str", *((type *)(__pkn_src)), \
  316. (__force type *)(__pkn_dst), __pkn_err, K); \
  317. __mte_disable_tco_async(); \
  318. \
  319. if (unlikely(__pkn_err)) \
  320. goto err_label; \
  321. } while(0)
  322. extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
  323. #define raw_copy_from_user(to, from, n) \
  324. ({ \
  325. unsigned long __acfu_ret; \
  326. uaccess_ttbr0_enable(); \
  327. __acfu_ret = __arch_copy_from_user((to), \
  328. __uaccess_mask_ptr(from), (n)); \
  329. uaccess_ttbr0_disable(); \
  330. __acfu_ret; \
  331. })
  332. extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
  333. #define raw_copy_to_user(to, from, n) \
  334. ({ \
  335. unsigned long __actu_ret; \
  336. uaccess_ttbr0_enable(); \
  337. __actu_ret = __arch_copy_to_user(__uaccess_mask_ptr(to), \
  338. (from), (n)); \
  339. uaccess_ttbr0_disable(); \
  340. __actu_ret; \
  341. })
  342. #define INLINE_COPY_TO_USER
  343. #define INLINE_COPY_FROM_USER
  344. extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n);
  345. static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n)
  346. {
  347. if (access_ok(to, n)) {
  348. uaccess_ttbr0_enable();
  349. n = __arch_clear_user(__uaccess_mask_ptr(to), n);
  350. uaccess_ttbr0_disable();
  351. }
  352. return n;
  353. }
  354. #define clear_user __clear_user
  355. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  356. extern __must_check long strnlen_user(const char __user *str, long n);
  357. #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
  358. struct page;
  359. void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len);
  360. extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
  361. static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
  362. {
  363. kasan_check_write(dst, size);
  364. return __copy_user_flushcache(dst, __uaccess_mask_ptr(src), size);
  365. }
  366. #endif
  367. #ifdef CONFIG_ARCH_HAS_SUBPAGE_FAULTS
  368. /*
  369. * Return 0 on success, the number of bytes not probed otherwise.
  370. */
  371. static inline size_t probe_subpage_writeable(const char __user *uaddr,
  372. size_t size)
  373. {
  374. if (!system_supports_mte())
  375. return 0;
  376. return mte_probe_user_range(uaddr, size);
  377. }
  378. #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
  379. #endif /* __ASM_UACCESS_H */