uaccess.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * arch/arm/include/asm/uaccess.h
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef _ASMARM_UACCESS_H
  9. #define _ASMARM_UACCESS_H
  10. /*
  11. * User space memory access functions
  12. */
  13. #include <linux/string.h>
  14. #include <linux/thread_info.h>
  15. #include <asm/errno.h>
  16. #include <asm/memory.h>
  17. #include <asm/domain.h>
  18. #include <asm/unified.h>
  19. #include <asm/compiler.h>
  20. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  21. #include <asm-generic/uaccess-unaligned.h>
  22. #else
  23. #define __get_user_unaligned __get_user
  24. #define __put_user_unaligned __put_user
  25. #endif
  26. #define VERIFY_READ 0
  27. #define VERIFY_WRITE 1
  28. /*
  29. * The exception table consists of pairs of addresses: the first is the
  30. * address of an instruction that is allowed to fault, and the second is
  31. * the address at which the program should continue. No registers are
  32. * modified, so it is entirely up to the continuation code to figure out
  33. * what to do.
  34. *
  35. * All the routines below use bits of fixup code that are out of line
  36. * with the main instruction path. This means when everything is well,
  37. * we don't even have to jump over them. Further, they do not intrude
  38. * on our cache or tlb entries.
  39. */
  40. struct exception_table_entry
  41. {
  42. unsigned long insn, fixup;
  43. };
  44. extern int fixup_exception(struct pt_regs *regs);
  45. /*
  46. * These two functions allow hooking accesses to userspace to increase
  47. * system integrity by ensuring that the kernel can not inadvertantly
  48. * perform such accesses (eg, via list poison values) which could then
  49. * be exploited for priviledge escalation.
  50. */
  51. static inline unsigned int uaccess_save_and_enable(void)
  52. {
  53. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  54. unsigned int old_domain = get_domain();
  55. /* Set the current domain access to permit user accesses */
  56. set_domain((old_domain & ~domain_mask(DOMAIN_USER)) |
  57. domain_val(DOMAIN_USER, DOMAIN_CLIENT));
  58. return old_domain;
  59. #else
  60. return 0;
  61. #endif
  62. }
  63. static inline void uaccess_restore(unsigned int flags)
  64. {
  65. #ifdef CONFIG_CPU_SW_DOMAIN_PAN
  66. /* Restore the user access mask */
  67. set_domain(flags);
  68. #endif
  69. }
  70. /*
  71. * These two are intentionally not defined anywhere - if the kernel
  72. * code generates any references to them, that's a bug.
  73. */
  74. extern int __get_user_bad(void);
  75. extern int __put_user_bad(void);
  76. /*
  77. * Note that this is actually 0x1,0000,0000
  78. */
  79. #define KERNEL_DS 0x00000000
  80. #define get_ds() (KERNEL_DS)
  81. #ifdef CONFIG_MMU
  82. #define USER_DS TASK_SIZE
  83. #define get_fs() (current_thread_info()->addr_limit)
  84. static inline void set_fs(mm_segment_t fs)
  85. {
  86. current_thread_info()->addr_limit = fs;
  87. modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
  88. }
  89. #define segment_eq(a, b) ((a) == (b))
  90. /* We use 33-bit arithmetic here... */
  91. #define __range_ok(addr, size) ({ \
  92. unsigned long flag, roksum; \
  93. __chk_user_ptr(addr); \
  94. __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
  95. : "=&r" (flag), "=&r" (roksum) \
  96. : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
  97. : "cc"); \
  98. flag; })
  99. /*
  100. * Single-value transfer routines. They automatically use the right
  101. * size if we just have the right pointer type. Note that the functions
  102. * which read from user space (*get_*) need to take care not to leak
  103. * kernel data even if the calling code is buggy and fails to check
  104. * the return value. This means zeroing out the destination variable
  105. * or buffer on error. Normally this is done out of line by the
  106. * fixup code, but there are a few places where it intrudes on the
  107. * main code path. When we only write to user space, there is no
  108. * problem.
  109. */
  110. extern int __get_user_1(void *);
  111. extern int __get_user_2(void *);
  112. extern int __get_user_4(void *);
  113. extern int __get_user_32t_8(void *);
  114. extern int __get_user_8(void *);
  115. extern int __get_user_64t_1(void *);
  116. extern int __get_user_64t_2(void *);
  117. extern int __get_user_64t_4(void *);
  118. #define __GUP_CLOBBER_1 "lr", "cc"
  119. #ifdef CONFIG_CPU_USE_DOMAINS
  120. #define __GUP_CLOBBER_2 "ip", "lr", "cc"
  121. #else
  122. #define __GUP_CLOBBER_2 "lr", "cc"
  123. #endif
  124. #define __GUP_CLOBBER_4 "lr", "cc"
  125. #define __GUP_CLOBBER_32t_8 "lr", "cc"
  126. #define __GUP_CLOBBER_8 "lr", "cc"
  127. #define __get_user_x(__r2, __p, __e, __l, __s) \
  128. __asm__ __volatile__ ( \
  129. __asmeq("%0", "r0") __asmeq("%1", "r2") \
  130. __asmeq("%3", "r1") \
  131. "bl __get_user_" #__s \
  132. : "=&r" (__e), "=r" (__r2) \
  133. : "0" (__p), "r" (__l) \
  134. : __GUP_CLOBBER_##__s)
  135. /* narrowing a double-word get into a single 32bit word register: */
  136. #ifdef __ARMEB__
  137. #define __get_user_x_32t(__r2, __p, __e, __l, __s) \
  138. __get_user_x(__r2, __p, __e, __l, 32t_8)
  139. #else
  140. #define __get_user_x_32t __get_user_x
  141. #endif
  142. /*
  143. * storing result into proper least significant word of 64bit target var,
  144. * different only for big endian case where 64 bit __r2 lsw is r3:
  145. */
  146. #ifdef __ARMEB__
  147. #define __get_user_x_64t(__r2, __p, __e, __l, __s) \
  148. __asm__ __volatile__ ( \
  149. __asmeq("%0", "r0") __asmeq("%1", "r2") \
  150. __asmeq("%3", "r1") \
  151. "bl __get_user_64t_" #__s \
  152. : "=&r" (__e), "=r" (__r2) \
  153. : "0" (__p), "r" (__l) \
  154. : __GUP_CLOBBER_##__s)
  155. #else
  156. #define __get_user_x_64t __get_user_x
  157. #endif
  158. #define __get_user_check(x, p) \
  159. ({ \
  160. unsigned long __limit = current_thread_info()->addr_limit - 1; \
  161. register const typeof(*(p)) __user *__p asm("r0") = (p);\
  162. register typeof(x) __r2 asm("r2"); \
  163. register unsigned long __l asm("r1") = __limit; \
  164. register int __e asm("r0"); \
  165. unsigned int __ua_flags = uaccess_save_and_enable(); \
  166. switch (sizeof(*(__p))) { \
  167. case 1: \
  168. if (sizeof((x)) >= 8) \
  169. __get_user_x_64t(__r2, __p, __e, __l, 1); \
  170. else \
  171. __get_user_x(__r2, __p, __e, __l, 1); \
  172. break; \
  173. case 2: \
  174. if (sizeof((x)) >= 8) \
  175. __get_user_x_64t(__r2, __p, __e, __l, 2); \
  176. else \
  177. __get_user_x(__r2, __p, __e, __l, 2); \
  178. break; \
  179. case 4: \
  180. if (sizeof((x)) >= 8) \
  181. __get_user_x_64t(__r2, __p, __e, __l, 4); \
  182. else \
  183. __get_user_x(__r2, __p, __e, __l, 4); \
  184. break; \
  185. case 8: \
  186. if (sizeof((x)) < 8) \
  187. __get_user_x_32t(__r2, __p, __e, __l, 4); \
  188. else \
  189. __get_user_x(__r2, __p, __e, __l, 8); \
  190. break; \
  191. default: __e = __get_user_bad(); break; \
  192. } \
  193. uaccess_restore(__ua_flags); \
  194. x = (typeof(*(p))) __r2; \
  195. __e; \
  196. })
  197. #define get_user(x, p) \
  198. ({ \
  199. might_fault(); \
  200. __get_user_check(x, p); \
  201. })
  202. extern int __put_user_1(void *, unsigned int);
  203. extern int __put_user_2(void *, unsigned int);
  204. extern int __put_user_4(void *, unsigned int);
  205. extern int __put_user_8(void *, unsigned long long);
  206. #define __put_user_check(__pu_val, __ptr, __err, __s) \
  207. ({ \
  208. unsigned long __limit = current_thread_info()->addr_limit - 1; \
  209. register typeof(__pu_val) __r2 asm("r2") = __pu_val; \
  210. register const void __user *__p asm("r0") = __ptr; \
  211. register unsigned long __l asm("r1") = __limit; \
  212. register int __e asm("r0"); \
  213. __asm__ __volatile__ ( \
  214. __asmeq("%0", "r0") __asmeq("%2", "r2") \
  215. __asmeq("%3", "r1") \
  216. "bl __put_user_" #__s \
  217. : "=&r" (__e) \
  218. : "0" (__p), "r" (__r2), "r" (__l) \
  219. : "ip", "lr", "cc"); \
  220. __err = __e; \
  221. })
  222. #else /* CONFIG_MMU */
  223. /*
  224. * uClinux has only one addr space, so has simplified address limits.
  225. */
  226. #define USER_DS KERNEL_DS
  227. #define segment_eq(a, b) (1)
  228. #define __addr_ok(addr) ((void)(addr), 1)
  229. #define __range_ok(addr, size) ((void)(addr), 0)
  230. #define get_fs() (KERNEL_DS)
  231. static inline void set_fs(mm_segment_t fs)
  232. {
  233. }
  234. #define get_user(x, p) __get_user(x, p)
  235. #define __put_user_check __put_user_nocheck
  236. #endif /* CONFIG_MMU */
  237. #define access_ok(type, addr, size) (__range_ok(addr, size) == 0)
  238. #define user_addr_max() \
  239. (segment_eq(get_fs(), KERNEL_DS) ? ~0UL : get_fs())
  240. /*
  241. * The "__xxx" versions of the user access functions do not verify the
  242. * address space - it must have been done previously with a separate
  243. * "access_ok()" call.
  244. *
  245. * The "xxx_error" versions set the third argument to EFAULT if an
  246. * error occurs, and leave it unchanged on success. Note that these
  247. * versions are void (ie, don't return a value as such).
  248. */
  249. #define __get_user(x, ptr) \
  250. ({ \
  251. long __gu_err = 0; \
  252. __get_user_err((x), (ptr), __gu_err); \
  253. __gu_err; \
  254. })
  255. #define __get_user_error(x, ptr, err) \
  256. ({ \
  257. __get_user_err((x), (ptr), err); \
  258. (void) 0; \
  259. })
  260. #define __get_user_err(x, ptr, err) \
  261. do { \
  262. unsigned long __gu_addr = (unsigned long)(ptr); \
  263. unsigned long __gu_val; \
  264. unsigned int __ua_flags; \
  265. __chk_user_ptr(ptr); \
  266. might_fault(); \
  267. __ua_flags = uaccess_save_and_enable(); \
  268. switch (sizeof(*(ptr))) { \
  269. case 1: __get_user_asm_byte(__gu_val, __gu_addr, err); break; \
  270. case 2: __get_user_asm_half(__gu_val, __gu_addr, err); break; \
  271. case 4: __get_user_asm_word(__gu_val, __gu_addr, err); break; \
  272. default: (__gu_val) = __get_user_bad(); \
  273. } \
  274. uaccess_restore(__ua_flags); \
  275. (x) = (__typeof__(*(ptr)))__gu_val; \
  276. } while (0)
  277. #define __get_user_asm(x, addr, err, instr) \
  278. __asm__ __volatile__( \
  279. "1: " TUSER(instr) " %1, [%2], #0\n" \
  280. "2:\n" \
  281. " .pushsection .text.fixup,\"ax\"\n" \
  282. " .align 2\n" \
  283. "3: mov %0, %3\n" \
  284. " mov %1, #0\n" \
  285. " b 2b\n" \
  286. " .popsection\n" \
  287. " .pushsection __ex_table,\"a\"\n" \
  288. " .align 3\n" \
  289. " .long 1b, 3b\n" \
  290. " .popsection" \
  291. : "+r" (err), "=&r" (x) \
  292. : "r" (addr), "i" (-EFAULT) \
  293. : "cc")
  294. #define __get_user_asm_byte(x, addr, err) \
  295. __get_user_asm(x, addr, err, ldrb)
  296. #ifndef __ARMEB__
  297. #define __get_user_asm_half(x, __gu_addr, err) \
  298. ({ \
  299. unsigned long __b1, __b2; \
  300. __get_user_asm_byte(__b1, __gu_addr, err); \
  301. __get_user_asm_byte(__b2, __gu_addr + 1, err); \
  302. (x) = __b1 | (__b2 << 8); \
  303. })
  304. #else
  305. #define __get_user_asm_half(x, __gu_addr, err) \
  306. ({ \
  307. unsigned long __b1, __b2; \
  308. __get_user_asm_byte(__b1, __gu_addr, err); \
  309. __get_user_asm_byte(__b2, __gu_addr + 1, err); \
  310. (x) = (__b1 << 8) | __b2; \
  311. })
  312. #endif
  313. #define __get_user_asm_word(x, addr, err) \
  314. __get_user_asm(x, addr, err, ldr)
  315. #define __put_user_switch(x, ptr, __err, __fn) \
  316. do { \
  317. const __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
  318. __typeof__(*(ptr)) __pu_val = (x); \
  319. unsigned int __ua_flags; \
  320. might_fault(); \
  321. __ua_flags = uaccess_save_and_enable(); \
  322. switch (sizeof(*(ptr))) { \
  323. case 1: __fn(__pu_val, __pu_ptr, __err, 1); break; \
  324. case 2: __fn(__pu_val, __pu_ptr, __err, 2); break; \
  325. case 4: __fn(__pu_val, __pu_ptr, __err, 4); break; \
  326. case 8: __fn(__pu_val, __pu_ptr, __err, 8); break; \
  327. default: __err = __put_user_bad(); break; \
  328. } \
  329. uaccess_restore(__ua_flags); \
  330. } while (0)
  331. #define put_user(x, ptr) \
  332. ({ \
  333. int __pu_err = 0; \
  334. __put_user_switch((x), (ptr), __pu_err, __put_user_check); \
  335. __pu_err; \
  336. })
  337. #define __put_user(x, ptr) \
  338. ({ \
  339. long __pu_err = 0; \
  340. __put_user_switch((x), (ptr), __pu_err, __put_user_nocheck); \
  341. __pu_err; \
  342. })
  343. #define __put_user_error(x, ptr, err) \
  344. ({ \
  345. __put_user_switch((x), (ptr), (err), __put_user_nocheck); \
  346. (void) 0; \
  347. })
  348. #define __put_user_nocheck(x, __pu_ptr, __err, __size) \
  349. do { \
  350. unsigned long __pu_addr = (unsigned long)__pu_ptr; \
  351. __put_user_nocheck_##__size(x, __pu_addr, __err); \
  352. } while (0)
  353. #define __put_user_nocheck_1 __put_user_asm_byte
  354. #define __put_user_nocheck_2 __put_user_asm_half
  355. #define __put_user_nocheck_4 __put_user_asm_word
  356. #define __put_user_nocheck_8 __put_user_asm_dword
  357. #define __put_user_asm(x, __pu_addr, err, instr) \
  358. __asm__ __volatile__( \
  359. "1: " TUSER(instr) " %1, [%2], #0\n" \
  360. "2:\n" \
  361. " .pushsection .text.fixup,\"ax\"\n" \
  362. " .align 2\n" \
  363. "3: mov %0, %3\n" \
  364. " b 2b\n" \
  365. " .popsection\n" \
  366. " .pushsection __ex_table,\"a\"\n" \
  367. " .align 3\n" \
  368. " .long 1b, 3b\n" \
  369. " .popsection" \
  370. : "+r" (err) \
  371. : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
  372. : "cc")
  373. #define __put_user_asm_byte(x, __pu_addr, err) \
  374. __put_user_asm(x, __pu_addr, err, strb)
  375. #ifndef __ARMEB__
  376. #define __put_user_asm_half(x, __pu_addr, err) \
  377. ({ \
  378. unsigned long __temp = (__force unsigned long)(x); \
  379. __put_user_asm_byte(__temp, __pu_addr, err); \
  380. __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
  381. })
  382. #else
  383. #define __put_user_asm_half(x, __pu_addr, err) \
  384. ({ \
  385. unsigned long __temp = (__force unsigned long)(x); \
  386. __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
  387. __put_user_asm_byte(__temp, __pu_addr + 1, err); \
  388. })
  389. #endif
  390. #define __put_user_asm_word(x, __pu_addr, err) \
  391. __put_user_asm(x, __pu_addr, err, str)
  392. #ifndef __ARMEB__
  393. #define __reg_oper0 "%R2"
  394. #define __reg_oper1 "%Q2"
  395. #else
  396. #define __reg_oper0 "%Q2"
  397. #define __reg_oper1 "%R2"
  398. #endif
  399. #define __put_user_asm_dword(x, __pu_addr, err) \
  400. __asm__ __volatile__( \
  401. ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \
  402. ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \
  403. THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \
  404. THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \
  405. "3:\n" \
  406. " .pushsection .text.fixup,\"ax\"\n" \
  407. " .align 2\n" \
  408. "4: mov %0, %3\n" \
  409. " b 3b\n" \
  410. " .popsection\n" \
  411. " .pushsection __ex_table,\"a\"\n" \
  412. " .align 3\n" \
  413. " .long 1b, 4b\n" \
  414. " .long 2b, 4b\n" \
  415. " .popsection" \
  416. : "+r" (err), "+r" (__pu_addr) \
  417. : "r" (x), "i" (-EFAULT) \
  418. : "cc")
  419. #ifdef CONFIG_MMU
  420. extern unsigned long __must_check
  421. arm_copy_from_user(void *to, const void __user *from, unsigned long n);
  422. static inline unsigned long __must_check
  423. __arch_copy_from_user(void *to, const void __user *from, unsigned long n)
  424. {
  425. unsigned int __ua_flags;
  426. __ua_flags = uaccess_save_and_enable();
  427. n = arm_copy_from_user(to, from, n);
  428. uaccess_restore(__ua_flags);
  429. return n;
  430. }
  431. extern unsigned long __must_check
  432. arm_copy_to_user(void __user *to, const void *from, unsigned long n);
  433. extern unsigned long __must_check
  434. __copy_to_user_std(void __user *to, const void *from, unsigned long n);
  435. static inline unsigned long __must_check
  436. __arch_copy_to_user(void __user *to, const void *from, unsigned long n)
  437. {
  438. #ifndef CONFIG_UACCESS_WITH_MEMCPY
  439. unsigned int __ua_flags;
  440. __ua_flags = uaccess_save_and_enable();
  441. n = arm_copy_to_user(to, from, n);
  442. uaccess_restore(__ua_flags);
  443. return n;
  444. #else
  445. return arm_copy_to_user(to, from, n);
  446. #endif
  447. }
  448. extern unsigned long __must_check
  449. arm_clear_user(void __user *addr, unsigned long n);
  450. extern unsigned long __must_check
  451. __clear_user_std(void __user *addr, unsigned long n);
  452. static inline unsigned long __must_check
  453. __clear_user(void __user *addr, unsigned long n)
  454. {
  455. unsigned int __ua_flags = uaccess_save_and_enable();
  456. n = arm_clear_user(addr, n);
  457. uaccess_restore(__ua_flags);
  458. return n;
  459. }
  460. #else
  461. #define __arch_copy_from_user(to, from, n) \
  462. (memcpy(to, (void __force *)from, n), 0)
  463. #define __arch_copy_to_user(to, from, n) \
  464. (memcpy((void __force *)to, from, n), 0)
  465. #define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0)
  466. #endif
  467. static inline unsigned long __must_check
  468. __copy_from_user(void *to, const void __user *from, unsigned long n)
  469. {
  470. check_object_size(to, n, false);
  471. return __arch_copy_from_user(to, from, n);
  472. }
  473. static inline unsigned long __must_check
  474. copy_from_user(void *to, const void __user *from, unsigned long n)
  475. {
  476. unsigned long res = n;
  477. check_object_size(to, n, false);
  478. if (likely(access_ok(VERIFY_READ, from, n)))
  479. res = __arch_copy_from_user(to, from, n);
  480. if (unlikely(res))
  481. memset(to + (n - res), 0, res);
  482. return res;
  483. }
  484. static inline unsigned long __must_check
  485. __copy_to_user(void __user *to, const void *from, unsigned long n)
  486. {
  487. check_object_size(from, n, true);
  488. return __arch_copy_to_user(to, from, n);
  489. }
  490. static inline unsigned long __must_check
  491. copy_to_user(void __user *to, const void *from, unsigned long n)
  492. {
  493. check_object_size(from, n, true);
  494. if (access_ok(VERIFY_WRITE, to, n))
  495. n = __arch_copy_to_user(to, from, n);
  496. return n;
  497. }
  498. #define __copy_to_user_inatomic __copy_to_user
  499. #define __copy_from_user_inatomic __copy_from_user
  500. static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
  501. {
  502. if (access_ok(VERIFY_WRITE, to, n))
  503. n = __clear_user(to, n);
  504. return n;
  505. }
  506. /* These are from lib/ code, and use __get_user() and friends */
  507. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  508. extern __must_check long strlen_user(const char __user *str);
  509. extern __must_check long strnlen_user(const char __user *str, long n);
  510. #endif /* _ASMARM_UACCESS_H */