uaccess.h 11 KB

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