uaccess.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #ifndef __ASSEMBLY__
  24. /*
  25. * User space memory access functions
  26. */
  27. #include <linux/bitops.h>
  28. #include <linux/kasan-checks.h>
  29. #include <linux/string.h>
  30. #include <linux/thread_info.h>
  31. #include <asm/cpufeature.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/errno.h>
  34. #include <asm/memory.h>
  35. #include <asm/compiler.h>
  36. #define VERIFY_READ 0
  37. #define VERIFY_WRITE 1
  38. /*
  39. * The exception table consists of pairs of relative offsets: the first
  40. * is the relative offset to an instruction that is allowed to fault,
  41. * and the second is the relative offset at which the program should
  42. * continue. No registers are modified, so it is entirely up to the
  43. * continuation code to figure out what to do.
  44. *
  45. * All the routines below use bits of fixup code that are out of line
  46. * with the main instruction path. This means when everything is well,
  47. * we don't even have to jump over them. Further, they do not intrude
  48. * on our cache or tlb entries.
  49. */
  50. struct exception_table_entry
  51. {
  52. int insn, fixup;
  53. };
  54. #define ARCH_HAS_RELATIVE_EXTABLE
  55. extern int fixup_exception(struct pt_regs *regs);
  56. #define KERNEL_DS (-1UL)
  57. #define get_ds() (KERNEL_DS)
  58. #define USER_DS TASK_SIZE_64
  59. #define get_fs() (current_thread_info()->addr_limit)
  60. static inline void set_fs(mm_segment_t fs)
  61. {
  62. current_thread_info()->addr_limit = fs;
  63. /*
  64. * Enable/disable UAO so that copy_to_user() etc can access
  65. * kernel memory with the unprivileged instructions.
  66. */
  67. if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
  68. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
  69. else
  70. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
  71. CONFIG_ARM64_UAO));
  72. }
  73. #define segment_eq(a, b) ((a) == (b))
  74. /*
  75. * Test whether a block of memory is a valid user space address.
  76. * Returns 1 if the range is valid, 0 otherwise.
  77. *
  78. * This is equivalent to the following test:
  79. * (u65)addr + (u65)size <= current->addr_limit
  80. *
  81. * This needs 65-bit arithmetic.
  82. */
  83. #define __range_ok(addr, size) \
  84. ({ \
  85. unsigned long __addr = (unsigned long __force)(addr); \
  86. unsigned long flag, roksum; \
  87. __chk_user_ptr(addr); \
  88. asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
  89. : "=&r" (flag), "=&r" (roksum) \
  90. : "1" (__addr), "Ir" (size), \
  91. "r" (current_thread_info()->addr_limit) \
  92. : "cc"); \
  93. flag; \
  94. })
  95. /*
  96. * When dealing with data aborts, watchpoints, or instruction traps we may end
  97. * up with a tagged userland pointer. Clear the tag to get a sane pointer to
  98. * pass on to access_ok(), for instance.
  99. */
  100. #define untagged_addr(addr) sign_extend64(addr, 55)
  101. #define access_ok(type, addr, size) __range_ok(addr, size)
  102. #define user_addr_max get_fs
  103. #define _ASM_EXTABLE(from, to) \
  104. " .pushsection __ex_table, \"a\"\n" \
  105. " .align 3\n" \
  106. " .long (" #from " - .), (" #to " - .)\n" \
  107. " .popsection\n"
  108. /*
  109. * User access enabling/disabling.
  110. */
  111. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  112. static inline void __uaccess_ttbr0_disable(void)
  113. {
  114. unsigned long ttbr;
  115. /* reserved_ttbr0 placed at the end of swapper_pg_dir */
  116. ttbr = read_sysreg(ttbr1_el1) + SWAPPER_DIR_SIZE;
  117. write_sysreg(ttbr, ttbr0_el1);
  118. isb();
  119. }
  120. static inline void __uaccess_ttbr0_enable(void)
  121. {
  122. unsigned long flags;
  123. /*
  124. * Disable interrupts to avoid preemption between reading the 'ttbr0'
  125. * variable and the MSR. A context switch could trigger an ASID
  126. * roll-over and an update of 'ttbr0'.
  127. */
  128. local_irq_save(flags);
  129. write_sysreg(current_thread_info()->ttbr0, ttbr0_el1);
  130. isb();
  131. local_irq_restore(flags);
  132. }
  133. static inline bool uaccess_ttbr0_disable(void)
  134. {
  135. if (!system_uses_ttbr0_pan())
  136. return false;
  137. __uaccess_ttbr0_disable();
  138. return true;
  139. }
  140. static inline bool uaccess_ttbr0_enable(void)
  141. {
  142. if (!system_uses_ttbr0_pan())
  143. return false;
  144. __uaccess_ttbr0_enable();
  145. return true;
  146. }
  147. #else
  148. static inline bool uaccess_ttbr0_disable(void)
  149. {
  150. return false;
  151. }
  152. static inline bool uaccess_ttbr0_enable(void)
  153. {
  154. return false;
  155. }
  156. #endif
  157. #define __uaccess_disable(alt) \
  158. do { \
  159. if (!uaccess_ttbr0_disable()) \
  160. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
  161. CONFIG_ARM64_PAN)); \
  162. } while (0)
  163. #define __uaccess_enable(alt) \
  164. do { \
  165. if (!uaccess_ttbr0_enable()) \
  166. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
  167. CONFIG_ARM64_PAN)); \
  168. } while (0)
  169. static inline void uaccess_disable(void)
  170. {
  171. __uaccess_disable(ARM64_HAS_PAN);
  172. }
  173. static inline void uaccess_enable(void)
  174. {
  175. __uaccess_enable(ARM64_HAS_PAN);
  176. }
  177. /*
  178. * These functions are no-ops when UAO is present.
  179. */
  180. static inline void uaccess_disable_not_uao(void)
  181. {
  182. __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
  183. }
  184. static inline void uaccess_enable_not_uao(void)
  185. {
  186. __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
  187. }
  188. /*
  189. * The "__xxx" versions of the user access functions do not verify the address
  190. * space - it must have been done previously with a separate "access_ok()"
  191. * call.
  192. *
  193. * The "__xxx_error" versions set the third argument to -EFAULT if an error
  194. * occurs, and leave it unchanged on success.
  195. */
  196. #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
  197. asm volatile( \
  198. "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
  199. alt_instr " " reg "1, [%2]\n", feature) \
  200. "2:\n" \
  201. " .section .fixup, \"ax\"\n" \
  202. " .align 2\n" \
  203. "3: mov %w0, %3\n" \
  204. " mov %1, #0\n" \
  205. " b 2b\n" \
  206. " .previous\n" \
  207. _ASM_EXTABLE(1b, 3b) \
  208. : "+r" (err), "=&r" (x) \
  209. : "r" (addr), "i" (-EFAULT))
  210. #define __get_user_err(x, ptr, err) \
  211. do { \
  212. unsigned long __gu_val; \
  213. __chk_user_ptr(ptr); \
  214. uaccess_enable_not_uao(); \
  215. switch (sizeof(*(ptr))) { \
  216. case 1: \
  217. __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
  218. (err), ARM64_HAS_UAO); \
  219. break; \
  220. case 2: \
  221. __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
  222. (err), ARM64_HAS_UAO); \
  223. break; \
  224. case 4: \
  225. __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
  226. (err), ARM64_HAS_UAO); \
  227. break; \
  228. case 8: \
  229. __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
  230. (err), ARM64_HAS_UAO); \
  231. break; \
  232. default: \
  233. BUILD_BUG(); \
  234. } \
  235. uaccess_disable_not_uao(); \
  236. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  237. } while (0)
  238. #define __get_user(x, ptr) \
  239. ({ \
  240. int __gu_err = 0; \
  241. __get_user_err((x), (ptr), __gu_err); \
  242. __gu_err; \
  243. })
  244. #define __get_user_error(x, ptr, err) \
  245. ({ \
  246. __get_user_err((x), (ptr), (err)); \
  247. (void)0; \
  248. })
  249. #define __get_user_unaligned __get_user
  250. #define get_user(x, ptr) \
  251. ({ \
  252. __typeof__(*(ptr)) __user *__p = (ptr); \
  253. might_fault(); \
  254. access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
  255. __get_user((x), __p) : \
  256. ((x) = 0, -EFAULT); \
  257. })
  258. #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
  259. asm volatile( \
  260. "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
  261. alt_instr " " reg "1, [%2]\n", feature) \
  262. "2:\n" \
  263. " .section .fixup,\"ax\"\n" \
  264. " .align 2\n" \
  265. "3: mov %w0, %3\n" \
  266. " b 2b\n" \
  267. " .previous\n" \
  268. _ASM_EXTABLE(1b, 3b) \
  269. : "+r" (err) \
  270. : "r" (x), "r" (addr), "i" (-EFAULT))
  271. #define __put_user_err(x, ptr, err) \
  272. do { \
  273. __typeof__(*(ptr)) __pu_val = (x); \
  274. __chk_user_ptr(ptr); \
  275. uaccess_enable_not_uao(); \
  276. switch (sizeof(*(ptr))) { \
  277. case 1: \
  278. __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
  279. (err), ARM64_HAS_UAO); \
  280. break; \
  281. case 2: \
  282. __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
  283. (err), ARM64_HAS_UAO); \
  284. break; \
  285. case 4: \
  286. __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
  287. (err), ARM64_HAS_UAO); \
  288. break; \
  289. case 8: \
  290. __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
  291. (err), ARM64_HAS_UAO); \
  292. break; \
  293. default: \
  294. BUILD_BUG(); \
  295. } \
  296. uaccess_disable_not_uao(); \
  297. } while (0)
  298. #define __put_user(x, ptr) \
  299. ({ \
  300. int __pu_err = 0; \
  301. __put_user_err((x), (ptr), __pu_err); \
  302. __pu_err; \
  303. })
  304. #define __put_user_error(x, ptr, err) \
  305. ({ \
  306. __put_user_err((x), (ptr), (err)); \
  307. (void)0; \
  308. })
  309. #define __put_user_unaligned __put_user
  310. #define put_user(x, ptr) \
  311. ({ \
  312. __typeof__(*(ptr)) __user *__p = (ptr); \
  313. might_fault(); \
  314. access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
  315. __put_user((x), __p) : \
  316. -EFAULT; \
  317. })
  318. extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
  319. extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
  320. extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
  321. extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
  322. static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
  323. {
  324. kasan_check_write(to, n);
  325. check_object_size(to, n, false);
  326. return __arch_copy_from_user(to, from, n);
  327. }
  328. static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
  329. {
  330. kasan_check_read(from, n);
  331. check_object_size(from, n, true);
  332. return __arch_copy_to_user(to, from, n);
  333. }
  334. static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
  335. {
  336. unsigned long res = n;
  337. kasan_check_write(to, n);
  338. if (access_ok(VERIFY_READ, from, n)) {
  339. check_object_size(to, n, false);
  340. res = __arch_copy_from_user(to, from, n);
  341. }
  342. if (unlikely(res))
  343. memset(to + (n - res), 0, res);
  344. return res;
  345. }
  346. static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
  347. {
  348. kasan_check_read(from, n);
  349. if (access_ok(VERIFY_WRITE, to, n)) {
  350. check_object_size(from, n, true);
  351. n = __arch_copy_to_user(to, from, n);
  352. }
  353. return n;
  354. }
  355. static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
  356. {
  357. if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
  358. n = __copy_in_user(to, from, n);
  359. return n;
  360. }
  361. #define __copy_to_user_inatomic __copy_to_user
  362. #define __copy_from_user_inatomic __copy_from_user
  363. static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
  364. {
  365. if (access_ok(VERIFY_WRITE, to, n))
  366. n = __clear_user(to, n);
  367. return n;
  368. }
  369. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  370. extern __must_check long strlen_user(const char __user *str);
  371. extern __must_check long strnlen_user(const char __user *str, long n);
  372. #else /* __ASSEMBLY__ */
  373. #include <asm/assembler.h>
  374. /*
  375. * User access enabling/disabling macros.
  376. */
  377. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  378. .macro __uaccess_ttbr0_disable, tmp1
  379. mrs \tmp1, ttbr1_el1 // swapper_pg_dir
  380. add \tmp1, \tmp1, #SWAPPER_DIR_SIZE // reserved_ttbr0 at the end of swapper_pg_dir
  381. msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1
  382. isb
  383. .endm
  384. .macro __uaccess_ttbr0_enable, tmp1
  385. get_thread_info \tmp1
  386. ldr \tmp1, [\tmp1, #TSK_TI_TTBR0] // load saved TTBR0_EL1
  387. msr ttbr0_el1, \tmp1 // set the non-PAN TTBR0_EL1
  388. isb
  389. .endm
  390. .macro uaccess_ttbr0_disable, tmp1
  391. alternative_if_not ARM64_HAS_PAN
  392. __uaccess_ttbr0_disable \tmp1
  393. alternative_else_nop_endif
  394. .endm
  395. .macro uaccess_ttbr0_enable, tmp1, tmp2
  396. alternative_if_not ARM64_HAS_PAN
  397. save_and_disable_irq \tmp2 // avoid preemption
  398. __uaccess_ttbr0_enable \tmp1
  399. restore_irq \tmp2
  400. alternative_else_nop_endif
  401. .endm
  402. #else
  403. .macro uaccess_ttbr0_disable, tmp1
  404. .endm
  405. .macro uaccess_ttbr0_enable, tmp1, tmp2
  406. .endm
  407. #endif
  408. /*
  409. * These macros are no-ops when UAO is present.
  410. */
  411. .macro uaccess_disable_not_uao, tmp1
  412. uaccess_ttbr0_disable \tmp1
  413. alternative_if ARM64_ALT_PAN_NOT_UAO
  414. SET_PSTATE_PAN(1)
  415. alternative_else_nop_endif
  416. .endm
  417. .macro uaccess_enable_not_uao, tmp1, tmp2
  418. uaccess_ttbr0_enable \tmp1, \tmp2
  419. alternative_if ARM64_ALT_PAN_NOT_UAO
  420. SET_PSTATE_PAN(0)
  421. alternative_else_nop_endif
  422. .endm
  423. #endif /* __ASSEMBLY__ */
  424. #endif /* __ASM_UACCESS_H */