uaccess.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #ifndef __SCORE_UACCESS_H
  2. #define __SCORE_UACCESS_H
  3. #include <linux/kernel.h>
  4. #include <linux/errno.h>
  5. #include <linux/thread_info.h>
  6. #include <asm/extable.h>
  7. #define VERIFY_READ 0
  8. #define VERIFY_WRITE 1
  9. #define get_ds() (KERNEL_DS)
  10. #define get_fs() (current_thread_info()->addr_limit)
  11. #define segment_eq(a, b) ((a).seg == (b).seg)
  12. /*
  13. * Is a address valid? This does a straighforward calculation rather
  14. * than tests.
  15. *
  16. * Address valid if:
  17. * - "addr" doesn't have any high-bits set
  18. * - AND "size" doesn't have any high-bits set
  19. * - AND "addr+size" doesn't have any high-bits set
  20. * - OR we are in kernel mode.
  21. *
  22. * __ua_size() is a trick to avoid runtime checking of positive constant
  23. * sizes; for those we already know at compile time that the size is ok.
  24. */
  25. #define __ua_size(size) \
  26. ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
  27. /*
  28. * access_ok: - Checks if a user space pointer is valid
  29. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  30. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  31. * to write to a block, it is always safe to read from it.
  32. * @addr: User space pointer to start of block to check
  33. * @size: Size of block to check
  34. *
  35. * Context: User context only. This function may sleep if pagefaults are
  36. * enabled.
  37. *
  38. * Checks if a pointer to a block of memory in user space is valid.
  39. *
  40. * Returns true (nonzero) if the memory block may be valid, false (zero)
  41. * if it is definitely invalid.
  42. *
  43. * Note that, depending on architecture, this function probably just
  44. * checks that the pointer is in the user space range - after calling
  45. * this function, memory access functions may still return -EFAULT.
  46. */
  47. #define __access_ok(addr, size) \
  48. (((long)((get_fs().seg) & \
  49. ((addr) | ((addr) + (size)) | \
  50. __ua_size(size)))) == 0)
  51. #define access_ok(type, addr, size) \
  52. likely(__access_ok((unsigned long)(addr), (size)))
  53. /*
  54. * put_user: - Write a simple value into user space.
  55. * @x: Value to copy to user space.
  56. * @ptr: Destination address, in user space.
  57. *
  58. * Context: User context only. This function may sleep if pagefaults are
  59. * enabled.
  60. *
  61. * This macro copies a single simple value from kernel space to user
  62. * space. It supports simple types like char and int, but not larger
  63. * data types like structures or arrays.
  64. *
  65. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  66. * to the result of dereferencing @ptr.
  67. *
  68. * Returns zero on success, or -EFAULT on error.
  69. */
  70. #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
  71. /*
  72. * get_user: - Get a simple variable from user space.
  73. * @x: Variable to store result.
  74. * @ptr: Source address, in user space.
  75. *
  76. * Context: User context only. This function may sleep if pagefaults are
  77. * enabled.
  78. *
  79. * This macro copies a single simple variable from user space to kernel
  80. * space. It supports simple types like char and int, but not larger
  81. * data types like structures or arrays.
  82. *
  83. * @ptr must have pointer-to-simple-variable type, and the result of
  84. * dereferencing @ptr must be assignable to @x without a cast.
  85. *
  86. * Returns zero on success, or -EFAULT on error.
  87. * On error, the variable @x is set to zero.
  88. */
  89. #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
  90. /*
  91. * __put_user: - Write a simple value into user space, with less checking.
  92. * @x: Value to copy to user space.
  93. * @ptr: Destination address, in user space.
  94. *
  95. * Context: User context only. This function may sleep if pagefaults are
  96. * enabled.
  97. *
  98. * This macro copies a single simple value from kernel space to user
  99. * space. It supports simple types like char and int, but not larger
  100. * data types like structures or arrays.
  101. *
  102. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  103. * to the result of dereferencing @ptr.
  104. *
  105. * Caller must check the pointer with access_ok() before calling this
  106. * function.
  107. *
  108. * Returns zero on success, or -EFAULT on error.
  109. */
  110. #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  111. /*
  112. * __get_user: - Get a simple variable from user space, with less checking.
  113. * @x: Variable to store result.
  114. * @ptr: Source address, in user space.
  115. *
  116. * Context: User context only. This function may sleep if pagefaults are
  117. * enabled.
  118. *
  119. * This macro copies a single simple variable from user space to kernel
  120. * space. It supports simple types like char and int, but not larger
  121. * data types like structures or arrays.
  122. *
  123. * @ptr must have pointer-to-simple-variable type, and the result of
  124. * dereferencing @ptr must be assignable to @x without a cast.
  125. *
  126. * Caller must check the pointer with access_ok() before calling this
  127. * function.
  128. *
  129. * Returns zero on success, or -EFAULT on error.
  130. * On error, the variable @x is set to zero.
  131. */
  132. #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  133. struct __large_struct { unsigned long buf[100]; };
  134. #define __m(x) (*(struct __large_struct __user *)(x))
  135. /*
  136. * Yuck. We need two variants, one for 64bit operation and one
  137. * for 32 bit mode and old iron.
  138. */
  139. extern void __get_user_unknown(void);
  140. #define __get_user_common(val, size, ptr) \
  141. do { \
  142. switch (size) { \
  143. case 1: \
  144. __get_user_asm(val, "lb", ptr); \
  145. break; \
  146. case 2: \
  147. __get_user_asm(val, "lh", ptr); \
  148. break; \
  149. case 4: \
  150. __get_user_asm(val, "lw", ptr); \
  151. break; \
  152. case 8: \
  153. if (__copy_from_user((void *)&val, ptr, 8) == 0) \
  154. __gu_err = 0; \
  155. else \
  156. __gu_err = -EFAULT; \
  157. break; \
  158. default: \
  159. __get_user_unknown(); \
  160. break; \
  161. } \
  162. } while (0)
  163. #define __get_user_nocheck(x, ptr, size) \
  164. ({ \
  165. long __gu_err = 0; \
  166. __get_user_common((x), size, ptr); \
  167. __gu_err; \
  168. })
  169. #define __get_user_check(x, ptr, size) \
  170. ({ \
  171. long __gu_err = -EFAULT; \
  172. const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
  173. \
  174. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
  175. __get_user_common((x), size, __gu_ptr); \
  176. else \
  177. (x) = 0; \
  178. \
  179. __gu_err; \
  180. })
  181. #define __get_user_asm(val, insn, addr) \
  182. { \
  183. long __gu_tmp; \
  184. \
  185. __asm__ __volatile__( \
  186. "1:" insn " %1, %3\n" \
  187. "2:\n" \
  188. ".section .fixup,\"ax\"\n" \
  189. "3:li %0, %4\n" \
  190. "li %1, 0\n" \
  191. "j 2b\n" \
  192. ".previous\n" \
  193. ".section __ex_table,\"a\"\n" \
  194. ".word 1b, 3b\n" \
  195. ".previous\n" \
  196. : "=r" (__gu_err), "=r" (__gu_tmp) \
  197. : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
  198. \
  199. (val) = (__typeof__(*(addr))) __gu_tmp; \
  200. }
  201. /*
  202. * Yuck. We need two variants, one for 64bit operation and one
  203. * for 32 bit mode and old iron.
  204. */
  205. #define __put_user_nocheck(val, ptr, size) \
  206. ({ \
  207. __typeof__(*(ptr)) __pu_val; \
  208. long __pu_err = 0; \
  209. \
  210. __pu_val = (val); \
  211. switch (size) { \
  212. case 1: \
  213. __put_user_asm("sb", ptr); \
  214. break; \
  215. case 2: \
  216. __put_user_asm("sh", ptr); \
  217. break; \
  218. case 4: \
  219. __put_user_asm("sw", ptr); \
  220. break; \
  221. case 8: \
  222. if ((__copy_to_user((void *)ptr, &__pu_val, 8)) == 0) \
  223. __pu_err = 0; \
  224. else \
  225. __pu_err = -EFAULT; \
  226. break; \
  227. default: \
  228. __put_user_unknown(); \
  229. break; \
  230. } \
  231. __pu_err; \
  232. })
  233. #define __put_user_check(val, ptr, size) \
  234. ({ \
  235. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  236. __typeof__(*(ptr)) __pu_val = (val); \
  237. long __pu_err = -EFAULT; \
  238. \
  239. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
  240. switch (size) { \
  241. case 1: \
  242. __put_user_asm("sb", __pu_addr); \
  243. break; \
  244. case 2: \
  245. __put_user_asm("sh", __pu_addr); \
  246. break; \
  247. case 4: \
  248. __put_user_asm("sw", __pu_addr); \
  249. break; \
  250. case 8: \
  251. if ((__copy_to_user((void *)__pu_addr, &__pu_val, 8)) == 0)\
  252. __pu_err = 0; \
  253. else \
  254. __pu_err = -EFAULT; \
  255. break; \
  256. default: \
  257. __put_user_unknown(); \
  258. break; \
  259. } \
  260. } \
  261. __pu_err; \
  262. })
  263. #define __put_user_asm(insn, ptr) \
  264. __asm__ __volatile__( \
  265. "1:" insn " %2, %3\n" \
  266. "2:\n" \
  267. ".section .fixup,\"ax\"\n" \
  268. "3:li %0, %4\n" \
  269. "j 2b\n" \
  270. ".previous\n" \
  271. ".section __ex_table,\"a\"\n" \
  272. ".word 1b, 3b\n" \
  273. ".previous\n" \
  274. : "=r" (__pu_err) \
  275. : "0" (0), "r" (__pu_val), "o" (__m(ptr)), \
  276. "i" (-EFAULT));
  277. extern void __put_user_unknown(void);
  278. extern int __copy_tofrom_user(void *to, const void *from, unsigned long len);
  279. static inline unsigned long
  280. copy_from_user(void *to, const void *from, unsigned long len)
  281. {
  282. unsigned long res = len;
  283. if (likely(access_ok(VERIFY_READ, from, len)))
  284. res = __copy_tofrom_user(to, from, len);
  285. if (unlikely(res))
  286. memset(to + (len - res), 0, res);
  287. return res;
  288. }
  289. static inline unsigned long
  290. copy_to_user(void *to, const void *from, unsigned long len)
  291. {
  292. if (likely(access_ok(VERIFY_WRITE, to, len)))
  293. len = __copy_tofrom_user(to, from, len);
  294. return len;
  295. }
  296. static inline unsigned long
  297. __copy_from_user(void *to, const void *from, unsigned long len)
  298. {
  299. unsigned long left = __copy_tofrom_user(to, from, len);
  300. if (unlikely(left))
  301. memset(to + (len - left), 0, left);
  302. return left;
  303. }
  304. #define __copy_to_user(to, from, len) \
  305. __copy_tofrom_user((to), (from), (len))
  306. static inline unsigned long
  307. __copy_to_user_inatomic(void *to, const void *from, unsigned long len)
  308. {
  309. return __copy_to_user(to, from, len);
  310. }
  311. static inline unsigned long
  312. __copy_from_user_inatomic(void *to, const void *from, unsigned long len)
  313. {
  314. return __copy_tofrom_user(to, from, len);
  315. }
  316. #define __copy_in_user(to, from, len) __copy_tofrom_user(to, from, len)
  317. static inline unsigned long
  318. copy_in_user(void *to, const void *from, unsigned long len)
  319. {
  320. if (access_ok(VERIFY_READ, from, len) &&
  321. access_ok(VERFITY_WRITE, to, len))
  322. return __copy_tofrom_user(to, from, len);
  323. }
  324. /*
  325. * __clear_user: - Zero a block of memory in user space, with less checking.
  326. * @to: Destination address, in user space.
  327. * @n: Number of bytes to zero.
  328. *
  329. * Zero a block of memory in user space. Caller must check
  330. * the specified block with access_ok() before calling this function.
  331. *
  332. * Returns number of bytes that could not be cleared.
  333. * On success, this will be zero.
  334. */
  335. extern unsigned long __clear_user(void __user *src, unsigned long size);
  336. static inline unsigned long clear_user(char *src, unsigned long size)
  337. {
  338. if (access_ok(VERIFY_WRITE, src, size))
  339. return __clear_user(src, size);
  340. return -EFAULT;
  341. }
  342. /*
  343. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  344. * @dst: Destination address, in kernel space. This buffer must be at
  345. * least @count bytes long.
  346. * @src: Source address, in user space.
  347. * @count: Maximum number of bytes to copy, including the trailing NUL.
  348. *
  349. * Copies a NUL-terminated string from userspace to kernel space.
  350. * Caller must check the specified block with access_ok() before calling
  351. * this function.
  352. *
  353. * On success, returns the length of the string (not including the trailing
  354. * NUL).
  355. *
  356. * If access to userspace fails, returns -EFAULT (some data may have been
  357. * copied).
  358. *
  359. * If @count is smaller than the length of the string, copies @count bytes
  360. * and returns @count.
  361. */
  362. extern int __strncpy_from_user(char *dst, const char *src, long len);
  363. static inline int strncpy_from_user(char *dst, const char *src, long len)
  364. {
  365. if (access_ok(VERIFY_READ, src, 1))
  366. return __strncpy_from_user(dst, src, len);
  367. return -EFAULT;
  368. }
  369. extern int __strlen_user(const char *src);
  370. static inline long strlen_user(const char __user *src)
  371. {
  372. return __strlen_user(src);
  373. }
  374. extern int __strnlen_user(const char *str, long len);
  375. static inline long strnlen_user(const char __user *str, long len)
  376. {
  377. if (!access_ok(VERIFY_READ, str, 0))
  378. return 0;
  379. else
  380. return __strnlen_user(str, len);
  381. }
  382. #endif /* __SCORE_UACCESS_H */