uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. /*
  21. * User space memory access functions
  22. */
  23. #include <linux/bitops.h>
  24. #include <linux/kasan-checks.h>
  25. #include <linux/string.h>
  26. #include <linux/thread_info.h>
  27. #include <asm/alternative.h>
  28. #include <asm/cpufeature.h>
  29. #include <asm/processor.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/sysreg.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 get_ds() (KERNEL_DS)
  56. #define get_fs() (current_thread_info()->addr_limit)
  57. static inline void set_fs(mm_segment_t fs)
  58. {
  59. current_thread_info()->addr_limit = fs;
  60. /*
  61. * Prevent a mispredicted conditional call to set_fs from forwarding
  62. * the wrong address limit to access_ok under speculation.
  63. */
  64. dsb(nsh);
  65. isb();
  66. /*
  67. * Enable/disable UAO so that copy_to_user() etc can access
  68. * kernel memory with the unprivileged instructions.
  69. */
  70. if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
  71. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
  72. else
  73. asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
  74. CONFIG_ARM64_UAO));
  75. }
  76. #define segment_eq(a, b) ((a) == (b))
  77. /*
  78. * Test whether a block of memory is a valid user space address.
  79. * Returns 1 if the range is valid, 0 otherwise.
  80. *
  81. * This is equivalent to the following test:
  82. * (u65)addr + (u65)size <= (u65)current->addr_limit + 1
  83. */
  84. static inline unsigned long __range_ok(unsigned long addr, unsigned long size)
  85. {
  86. unsigned long limit = current_thread_info()->addr_limit;
  87. __chk_user_ptr(addr);
  88. asm volatile(
  89. // A + B <= C + 1 for all A,B,C, in four easy steps:
  90. // 1: X = A + B; X' = X % 2^64
  91. " adds %0, %0, %2\n"
  92. // 2: Set C = 0 if X > 2^64, to guarantee X' > C in step 4
  93. " csel %1, xzr, %1, hi\n"
  94. // 3: Set X' = ~0 if X >= 2^64. For X == 2^64, this decrements X'
  95. // to compensate for the carry flag being set in step 4. For
  96. // X > 2^64, X' merely has to remain nonzero, which it does.
  97. " csinv %0, %0, xzr, cc\n"
  98. // 4: For X < 2^64, this gives us X' - C - 1 <= 0, where the -1
  99. // comes from the carry in being clear. Otherwise, we are
  100. // testing X' - C == 0, subject to the previous adjustments.
  101. " sbcs xzr, %0, %1\n"
  102. " cset %0, ls\n"
  103. : "+r" (addr), "+r" (limit) : "Ir" (size) : "cc");
  104. return addr;
  105. }
  106. /*
  107. * When dealing with data aborts, watchpoints, or instruction traps we may end
  108. * up with a tagged userland pointer. Clear the tag to get a sane pointer to
  109. * pass on to access_ok(), for instance.
  110. */
  111. #define untagged_addr(addr) sign_extend64(addr, 55)
  112. #define access_ok(type, addr, size) __range_ok((unsigned long)(addr), size)
  113. #define user_addr_max get_fs
  114. #define _ASM_EXTABLE(from, to) \
  115. " .pushsection __ex_table, \"a\"\n" \
  116. " .align 3\n" \
  117. " .long (" #from " - .), (" #to " - .)\n" \
  118. " .popsection\n"
  119. /*
  120. * Sanitise a uaccess pointer such that it becomes NULL if above the
  121. * current addr_limit.
  122. */
  123. #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
  124. static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
  125. {
  126. void __user *safe_ptr;
  127. asm volatile(
  128. " bics xzr, %1, %2\n"
  129. " csel %0, %1, xzr, eq\n"
  130. : "=&r" (safe_ptr)
  131. : "r" (ptr), "r" (current_thread_info()->addr_limit)
  132. : "cc");
  133. csdb();
  134. return safe_ptr;
  135. }
  136. /*
  137. * The "__xxx" versions of the user access functions do not verify the address
  138. * space - it must have been done previously with a separate "access_ok()"
  139. * call.
  140. *
  141. * The "__xxx_error" versions set the third argument to -EFAULT if an error
  142. * occurs, and leave it unchanged on success.
  143. */
  144. #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
  145. asm volatile( \
  146. "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
  147. alt_instr " " reg "1, [%2]\n", feature) \
  148. "2:\n" \
  149. " .section .fixup, \"ax\"\n" \
  150. " .align 2\n" \
  151. "3: mov %w0, %3\n" \
  152. " mov %1, #0\n" \
  153. " b 2b\n" \
  154. " .previous\n" \
  155. _ASM_EXTABLE(1b, 3b) \
  156. : "+r" (err), "=&r" (x) \
  157. : "r" (addr), "i" (-EFAULT))
  158. #define __get_user_err(x, ptr, err) \
  159. do { \
  160. unsigned long __gu_val; \
  161. __chk_user_ptr(ptr); \
  162. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
  163. CONFIG_ARM64_PAN)); \
  164. switch (sizeof(*(ptr))) { \
  165. case 1: \
  166. __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
  167. (err), ARM64_HAS_UAO); \
  168. break; \
  169. case 2: \
  170. __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
  171. (err), ARM64_HAS_UAO); \
  172. break; \
  173. case 4: \
  174. __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
  175. (err), ARM64_HAS_UAO); \
  176. break; \
  177. case 8: \
  178. __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
  179. (err), ARM64_HAS_UAO); \
  180. break; \
  181. default: \
  182. BUILD_BUG(); \
  183. } \
  184. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  185. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
  186. CONFIG_ARM64_PAN)); \
  187. } while (0)
  188. #define __get_user_check(x, ptr, err) \
  189. ({ \
  190. __typeof__(*(ptr)) __user *__p = (ptr); \
  191. might_fault(); \
  192. if (access_ok(VERIFY_READ, __p, sizeof(*__p))) { \
  193. __p = uaccess_mask_ptr(__p); \
  194. __get_user_err((x), __p, (err)); \
  195. } else { \
  196. (x) = 0; (err) = -EFAULT; \
  197. } \
  198. })
  199. #define __get_user_error(x, ptr, err) \
  200. ({ \
  201. __get_user_check((x), (ptr), (err)); \
  202. (void)0; \
  203. })
  204. #define __get_user(x, ptr) \
  205. ({ \
  206. int __gu_err = 0; \
  207. __get_user_check((x), (ptr), __gu_err); \
  208. __gu_err; \
  209. })
  210. #define __get_user_unaligned __get_user
  211. #define get_user __get_user
  212. #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
  213. asm volatile( \
  214. "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
  215. alt_instr " " reg "1, [%2]\n", feature) \
  216. "2:\n" \
  217. " .section .fixup,\"ax\"\n" \
  218. " .align 2\n" \
  219. "3: mov %w0, %3\n" \
  220. " b 2b\n" \
  221. " .previous\n" \
  222. _ASM_EXTABLE(1b, 3b) \
  223. : "+r" (err) \
  224. : "r" (x), "r" (addr), "i" (-EFAULT))
  225. #define __put_user_err(x, ptr, err) \
  226. do { \
  227. __typeof__(*(ptr)) __pu_val = (x); \
  228. __chk_user_ptr(ptr); \
  229. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
  230. CONFIG_ARM64_PAN)); \
  231. switch (sizeof(*(ptr))) { \
  232. case 1: \
  233. __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
  234. (err), ARM64_HAS_UAO); \
  235. break; \
  236. case 2: \
  237. __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
  238. (err), ARM64_HAS_UAO); \
  239. break; \
  240. case 4: \
  241. __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
  242. (err), ARM64_HAS_UAO); \
  243. break; \
  244. case 8: \
  245. __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
  246. (err), ARM64_HAS_UAO); \
  247. break; \
  248. default: \
  249. BUILD_BUG(); \
  250. } \
  251. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
  252. CONFIG_ARM64_PAN)); \
  253. } while (0)
  254. #define __put_user_check(x, ptr, err) \
  255. ({ \
  256. __typeof__(*(ptr)) __user *__p = (ptr); \
  257. might_fault(); \
  258. if (access_ok(VERIFY_WRITE, __p, sizeof(*__p))) { \
  259. __p = uaccess_mask_ptr(__p); \
  260. __put_user_err((x), __p, (err)); \
  261. } else { \
  262. (err) = -EFAULT; \
  263. } \
  264. })
  265. #define __put_user_error(x, ptr, err) \
  266. ({ \
  267. __put_user_check((x), (ptr), (err)); \
  268. (void)0; \
  269. })
  270. #define __put_user(x, ptr) \
  271. ({ \
  272. int __pu_err = 0; \
  273. __put_user_check((x), (ptr), __pu_err); \
  274. __pu_err; \
  275. })
  276. #define __put_user_unaligned __put_user
  277. #define put_user __put_user
  278. extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
  279. extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
  280. extern unsigned long __must_check __arch_copy_in_user(void __user *to, const void __user *from, unsigned long n);
  281. static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
  282. {
  283. kasan_check_write(to, n);
  284. check_object_size(to, n, false);
  285. return __arch_copy_from_user(to, __uaccess_mask_ptr(from), n);
  286. }
  287. static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
  288. {
  289. kasan_check_read(from, n);
  290. check_object_size(from, n, true);
  291. return __arch_copy_to_user(__uaccess_mask_ptr(to), from, n);
  292. }
  293. static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
  294. {
  295. unsigned long res = n;
  296. kasan_check_write(to, n);
  297. if (access_ok(VERIFY_READ, from, n)) {
  298. check_object_size(to, n, false);
  299. res = __arch_copy_from_user(to, from, n);
  300. }
  301. if (unlikely(res))
  302. memset(to + (n - res), 0, res);
  303. return res;
  304. }
  305. static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
  306. {
  307. kasan_check_read(from, n);
  308. if (access_ok(VERIFY_WRITE, to, n)) {
  309. check_object_size(from, n, true);
  310. n = __arch_copy_to_user(to, from, n);
  311. }
  312. return n;
  313. }
  314. static inline unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n)
  315. {
  316. if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
  317. n = __arch_copy_in_user(__uaccess_mask_ptr(to), __uaccess_mask_ptr(from), n);
  318. return n;
  319. }
  320. #define copy_in_user __copy_in_user
  321. #define __copy_to_user_inatomic __copy_to_user
  322. #define __copy_from_user_inatomic __copy_from_user
  323. extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n);
  324. static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n)
  325. {
  326. if (access_ok(VERIFY_WRITE, to, n))
  327. n = __arch_clear_user(__uaccess_mask_ptr(to), n);
  328. return n;
  329. }
  330. #define clear_user __clear_user
  331. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  332. extern __must_check long strlen_user(const char __user *str);
  333. extern __must_check long strnlen_user(const char __user *str, long n);
  334. #endif /* __ASM_UACCESS_H */