uaccess.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Based on arch/arm/include/asm/uaccess.h
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef __ASM_UACCESS_H
  19. #define __ASM_UACCESS_H
  20. #include <asm/alternative.h>
  21. #include <asm/kernel-pgtable.h>
  22. #include <asm/sysreg.h>
  23. /*
  24. * User space memory access functions
  25. */
  26. #include <linux/bitops.h>
  27. #include <linux/kasan-checks.h>
  28. #include <linux/string.h>
  29. #include <asm/cpufeature.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/memory.h>
  32. #include <asm/compiler.h>
  33. #include <asm/extable.h>
  34. #define get_ds() (KERNEL_DS)
  35. #define get_fs() (current_thread_info()->addr_limit)
  36. static inline void set_fs(mm_segment_t fs)
  37. {
  38. current_thread_info()->addr_limit = fs;
  39. /*
  40. * Prevent a mispredicted conditional call to set_fs from forwarding
  41. * the wrong address limit to access_ok under speculation.
  42. */
  43. dsb(nsh);
  44. isb();
  45. /* On user-mode return, check fs is correct */
  46. set_thread_flag(TIF_FSCHECK);
  47. /*
  48. * Enable/disable UAO so that copy_to_user() etc can access
  49. * kernel memory with the unprivileged instructions.
  50. */
  51. if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
  52. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
  53. else
  54. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
  55. CONFIG_ARM64_UAO));
  56. }
  57. #define segment_eq(a, b) ((a) == (b))
  58. /*
  59. * Test whether a block of memory is a valid user space address.
  60. * Returns 1 if the range is valid, 0 otherwise.
  61. *
  62. * This is equivalent to the following test:
  63. * (u65)addr + (u65)size <= (u65)current->addr_limit + 1
  64. */
  65. static inline unsigned long __range_ok(const void __user *addr, unsigned long size)
  66. {
  67. unsigned long ret, limit = current_thread_info()->addr_limit;
  68. __chk_user_ptr(addr);
  69. asm volatile(
  70. // A + B <= C + 1 for all A,B,C, in four easy steps:
  71. // 1: X = A + B; X' = X % 2^64
  72. " adds %0, %3, %2\n"
  73. // 2: Set C = 0 if X > 2^64, to guarantee X' > C in step 4
  74. " csel %1, xzr, %1, hi\n"
  75. // 3: Set X' = ~0 if X >= 2^64. For X == 2^64, this decrements X'
  76. // to compensate for the carry flag being set in step 4. For
  77. // X > 2^64, X' merely has to remain nonzero, which it does.
  78. " csinv %0, %0, xzr, cc\n"
  79. // 4: For X < 2^64, this gives us X' - C - 1 <= 0, where the -1
  80. // comes from the carry in being clear. Otherwise, we are
  81. // testing X' - C == 0, subject to the previous adjustments.
  82. " sbcs xzr, %0, %1\n"
  83. " cset %0, ls\n"
  84. : "=&r" (ret), "+r" (limit) : "Ir" (size), "0" (addr) : "cc");
  85. return ret;
  86. }
  87. /*
  88. * When dealing with data aborts, watchpoints, or instruction traps we may end
  89. * up with a tagged userland pointer. Clear the tag to get a sane pointer to
  90. * pass on to access_ok(), for instance.
  91. */
  92. #define untagged_addr(addr) sign_extend64(addr, 55)
  93. #define access_ok(type, addr, size) __range_ok(addr, size)
  94. #define user_addr_max get_fs
  95. #define _ASM_EXTABLE(from, to) \
  96. " .pushsection __ex_table, \"a\"\n" \
  97. " .align 3\n" \
  98. " .long (" #from " - .), (" #to " - .)\n" \
  99. " .popsection\n"
  100. /*
  101. * User access enabling/disabling.
  102. */
  103. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  104. static inline void __uaccess_ttbr0_disable(void)
  105. {
  106. unsigned long flags, ttbr;
  107. local_irq_save(flags);
  108. ttbr = read_sysreg(ttbr1_el1);
  109. ttbr &= ~TTBR_ASID_MASK;
  110. /* reserved_ttbr0 placed before swapper_pg_dir */
  111. write_sysreg(ttbr - RESERVED_TTBR0_SIZE, ttbr0_el1);
  112. isb();
  113. /* Set reserved ASID */
  114. write_sysreg(ttbr, ttbr1_el1);
  115. isb();
  116. local_irq_restore(flags);
  117. }
  118. static inline void __uaccess_ttbr0_enable(void)
  119. {
  120. unsigned long flags, ttbr0, ttbr1;
  121. /*
  122. * Disable interrupts to avoid preemption between reading the 'ttbr0'
  123. * variable and the MSR. A context switch could trigger an ASID
  124. * roll-over and an update of 'ttbr0'.
  125. */
  126. local_irq_save(flags);
  127. ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
  128. /* Restore active ASID */
  129. ttbr1 = read_sysreg(ttbr1_el1);
  130. ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */
  131. ttbr1 |= ttbr0 & TTBR_ASID_MASK;
  132. write_sysreg(ttbr1, ttbr1_el1);
  133. isb();
  134. /* Restore user page table */
  135. write_sysreg(ttbr0, ttbr0_el1);
  136. isb();
  137. local_irq_restore(flags);
  138. }
  139. static inline bool uaccess_ttbr0_disable(void)
  140. {
  141. if (!system_uses_ttbr0_pan())
  142. return false;
  143. __uaccess_ttbr0_disable();
  144. return true;
  145. }
  146. static inline bool uaccess_ttbr0_enable(void)
  147. {
  148. if (!system_uses_ttbr0_pan())
  149. return false;
  150. __uaccess_ttbr0_enable();
  151. return true;
  152. }
  153. #else
  154. static inline bool uaccess_ttbr0_disable(void)
  155. {
  156. return false;
  157. }
  158. static inline bool uaccess_ttbr0_enable(void)
  159. {
  160. return false;
  161. }
  162. #endif
  163. static inline void __uaccess_disable_hw_pan(void)
  164. {
  165. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,
  166. CONFIG_ARM64_PAN));
  167. }
  168. static inline void __uaccess_enable_hw_pan(void)
  169. {
  170. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
  171. CONFIG_ARM64_PAN));
  172. }
  173. #define __uaccess_disable(alt) \
  174. do { \
  175. if (!uaccess_ttbr0_disable()) \
  176. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
  177. CONFIG_ARM64_PAN)); \
  178. } while (0)
  179. #define __uaccess_enable(alt) \
  180. do { \
  181. if (!uaccess_ttbr0_enable()) \
  182. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
  183. CONFIG_ARM64_PAN)); \
  184. } while (0)
  185. static inline void uaccess_disable(void)
  186. {
  187. __uaccess_disable(ARM64_HAS_PAN);
  188. }
  189. static inline void uaccess_enable(void)
  190. {
  191. __uaccess_enable(ARM64_HAS_PAN);
  192. }
  193. /*
  194. * These functions are no-ops when UAO is present.
  195. */
  196. static inline void uaccess_disable_not_uao(void)
  197. {
  198. __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
  199. }
  200. static inline void uaccess_enable_not_uao(void)
  201. {
  202. __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
  203. }
  204. /*
  205. * Sanitise a uaccess pointer such that it becomes NULL if above the
  206. * current addr_limit.
  207. */
  208. #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
  209. static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
  210. {
  211. void __user *safe_ptr;
  212. asm volatile(
  213. " bics xzr, %1, %2\n"
  214. " csel %0, %1, xzr, eq\n"
  215. : "=&r" (safe_ptr)
  216. : "r" (ptr), "r" (current_thread_info()->addr_limit)
  217. : "cc");
  218. csdb();
  219. return safe_ptr;
  220. }
  221. /*
  222. * The "__xxx" versions of the user access functions do not verify the address
  223. * space - it must have been done previously with a separate "access_ok()"
  224. * call.
  225. *
  226. * The "__xxx_error" versions set the third argument to -EFAULT if an error
  227. * occurs, and leave it unchanged on success.
  228. */
  229. #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
  230. asm volatile( \
  231. "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
  232. alt_instr " " reg "1, [%2]\n", feature) \
  233. "2:\n" \
  234. " .section .fixup, \"ax\"\n" \
  235. " .align 2\n" \
  236. "3: mov %w0, %3\n" \
  237. " mov %1, #0\n" \
  238. " b 2b\n" \
  239. " .previous\n" \
  240. _ASM_EXTABLE(1b, 3b) \
  241. : "+r" (err), "=&r" (x) \
  242. : "r" (addr), "i" (-EFAULT))
  243. #define __get_user_err(x, ptr, err) \
  244. do { \
  245. unsigned long __gu_val; \
  246. __chk_user_ptr(ptr); \
  247. uaccess_enable_not_uao(); \
  248. switch (sizeof(*(ptr))) { \
  249. case 1: \
  250. __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
  251. (err), ARM64_HAS_UAO); \
  252. break; \
  253. case 2: \
  254. __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
  255. (err), ARM64_HAS_UAO); \
  256. break; \
  257. case 4: \
  258. __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
  259. (err), ARM64_HAS_UAO); \
  260. break; \
  261. case 8: \
  262. __get_user_asm("ldr", "ldtr", "%x", __gu_val, (ptr), \
  263. (err), ARM64_HAS_UAO); \
  264. break; \
  265. default: \
  266. BUILD_BUG(); \
  267. } \
  268. uaccess_disable_not_uao(); \
  269. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  270. } while (0)
  271. #define __get_user_check(x, ptr, err) \
  272. ({ \
  273. __typeof__(*(ptr)) __user *__p = (ptr); \
  274. might_fault(); \
  275. if (access_ok(VERIFY_READ, __p, sizeof(*__p))) { \
  276. __p = uaccess_mask_ptr(__p); \
  277. __get_user_err((x), __p, (err)); \
  278. } else { \
  279. (x) = 0; (err) = -EFAULT; \
  280. } \
  281. })
  282. #define __get_user_error(x, ptr, err) \
  283. ({ \
  284. __get_user_check((x), (ptr), (err)); \
  285. (void)0; \
  286. })
  287. #define __get_user(x, ptr) \
  288. ({ \
  289. int __gu_err = 0; \
  290. __get_user_check((x), (ptr), __gu_err); \
  291. __gu_err; \
  292. })
  293. #define get_user __get_user
  294. #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
  295. asm volatile( \
  296. "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
  297. alt_instr " " reg "1, [%2]\n", feature) \
  298. "2:\n" \
  299. " .section .fixup,\"ax\"\n" \
  300. " .align 2\n" \
  301. "3: mov %w0, %3\n" \
  302. " b 2b\n" \
  303. " .previous\n" \
  304. _ASM_EXTABLE(1b, 3b) \
  305. : "+r" (err) \
  306. : "r" (x), "r" (addr), "i" (-EFAULT))
  307. #define __put_user_err(x, ptr, err) \
  308. do { \
  309. __typeof__(*(ptr)) __pu_val = (x); \
  310. __chk_user_ptr(ptr); \
  311. uaccess_enable_not_uao(); \
  312. switch (sizeof(*(ptr))) { \
  313. case 1: \
  314. __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
  315. (err), ARM64_HAS_UAO); \
  316. break; \
  317. case 2: \
  318. __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
  319. (err), ARM64_HAS_UAO); \
  320. break; \
  321. case 4: \
  322. __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
  323. (err), ARM64_HAS_UAO); \
  324. break; \
  325. case 8: \
  326. __put_user_asm("str", "sttr", "%x", __pu_val, (ptr), \
  327. (err), ARM64_HAS_UAO); \
  328. break; \
  329. default: \
  330. BUILD_BUG(); \
  331. } \
  332. uaccess_disable_not_uao(); \
  333. } while (0)
  334. #define __put_user_check(x, ptr, err) \
  335. ({ \
  336. __typeof__(*(ptr)) __user *__p = (ptr); \
  337. might_fault(); \
  338. if (access_ok(VERIFY_WRITE, __p, sizeof(*__p))) { \
  339. __p = uaccess_mask_ptr(__p); \
  340. __put_user_err((x), __p, (err)); \
  341. } else { \
  342. (err) = -EFAULT; \
  343. } \
  344. })
  345. #define __put_user_error(x, ptr, err) \
  346. ({ \
  347. __put_user_check((x), (ptr), (err)); \
  348. (void)0; \
  349. })
  350. #define __put_user(x, ptr) \
  351. ({ \
  352. int __pu_err = 0; \
  353. __put_user_check((x), (ptr), __pu_err); \
  354. __pu_err; \
  355. })
  356. #define put_user __put_user
  357. extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
  358. #define raw_copy_from_user(to, from, n) \
  359. ({ \
  360. __arch_copy_from_user((to), __uaccess_mask_ptr(from), (n)); \
  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. __arch_copy_to_user(__uaccess_mask_ptr(to), (from), (n)); \
  366. })
  367. extern unsigned long __must_check __arch_copy_in_user(void __user *to, const void __user *from, unsigned long n);
  368. #define raw_copy_in_user(to, from, n) \
  369. ({ \
  370. __arch_copy_in_user(__uaccess_mask_ptr(to), \
  371. __uaccess_mask_ptr(from), (n)); \
  372. })
  373. #define INLINE_COPY_TO_USER
  374. #define INLINE_COPY_FROM_USER
  375. extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n);
  376. static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n)
  377. {
  378. if (access_ok(VERIFY_WRITE, to, n))
  379. n = __arch_clear_user(__uaccess_mask_ptr(to), n);
  380. return n;
  381. }
  382. #define clear_user __clear_user
  383. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  384. extern __must_check long strnlen_user(const char __user *str, long n);
  385. #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
  386. struct page;
  387. void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len);
  388. extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
  389. static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
  390. {
  391. kasan_check_write(dst, size);
  392. return __copy_user_flushcache(dst, __uaccess_mask_ptr(src), size);
  393. }
  394. #endif
  395. #endif /* __ASM_UACCESS_H */