uaccess.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_GENERIC_UACCESS_H
  3. #define __ASM_GENERIC_UACCESS_H
  4. /*
  5. * User space memory access functions, these should work
  6. * on any machine that has kernel and user data in the same
  7. * address space, e.g. all NOMMU machines.
  8. */
  9. #include <linux/string.h>
  10. #include <asm/segment.h>
  11. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  12. #ifndef KERNEL_DS
  13. #define KERNEL_DS MAKE_MM_SEG(~0UL)
  14. #endif
  15. #ifndef USER_DS
  16. #define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
  17. #endif
  18. #ifndef get_fs
  19. #define get_ds() (KERNEL_DS)
  20. #define get_fs() (current_thread_info()->addr_limit)
  21. static inline void set_fs(mm_segment_t fs)
  22. {
  23. current_thread_info()->addr_limit = fs;
  24. }
  25. #endif
  26. #ifndef segment_eq
  27. #define segment_eq(a, b) ((a).seg == (b).seg)
  28. #endif
  29. #define access_ok(type, addr, size) __access_ok((unsigned long)(addr),(size))
  30. /*
  31. * The architecture should really override this if possible, at least
  32. * doing a check on the get_fs()
  33. */
  34. #ifndef __access_ok
  35. static inline int __access_ok(unsigned long addr, unsigned long size)
  36. {
  37. return 1;
  38. }
  39. #endif
  40. /*
  41. * These are the main single-value transfer routines. They automatically
  42. * use the right size if we just have the right pointer type.
  43. * This version just falls back to copy_{from,to}_user, which should
  44. * provide a fast-path for small values.
  45. */
  46. #define __put_user(x, ptr) \
  47. ({ \
  48. __typeof__(*(ptr)) __x = (x); \
  49. int __pu_err = -EFAULT; \
  50. __chk_user_ptr(ptr); \
  51. switch (sizeof (*(ptr))) { \
  52. case 1: \
  53. case 2: \
  54. case 4: \
  55. case 8: \
  56. __pu_err = __put_user_fn(sizeof (*(ptr)), \
  57. ptr, &__x); \
  58. break; \
  59. default: \
  60. __put_user_bad(); \
  61. break; \
  62. } \
  63. __pu_err; \
  64. })
  65. #define put_user(x, ptr) \
  66. ({ \
  67. void __user *__p = (ptr); \
  68. might_fault(); \
  69. access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ? \
  70. __put_user((x), ((__typeof__(*(ptr)) __user *)__p)) : \
  71. -EFAULT; \
  72. })
  73. #ifndef __put_user_fn
  74. static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
  75. {
  76. return unlikely(raw_copy_to_user(ptr, x, size)) ? -EFAULT : 0;
  77. }
  78. #define __put_user_fn(sz, u, k) __put_user_fn(sz, u, k)
  79. #endif
  80. extern int __put_user_bad(void) __attribute__((noreturn));
  81. #define __get_user(x, ptr) \
  82. ({ \
  83. int __gu_err = -EFAULT; \
  84. __chk_user_ptr(ptr); \
  85. switch (sizeof(*(ptr))) { \
  86. case 1: { \
  87. unsigned char __x = 0; \
  88. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  89. ptr, &__x); \
  90. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  91. break; \
  92. }; \
  93. case 2: { \
  94. unsigned short __x = 0; \
  95. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  96. ptr, &__x); \
  97. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  98. break; \
  99. }; \
  100. case 4: { \
  101. unsigned int __x = 0; \
  102. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  103. ptr, &__x); \
  104. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  105. break; \
  106. }; \
  107. case 8: { \
  108. unsigned long long __x = 0; \
  109. __gu_err = __get_user_fn(sizeof (*(ptr)), \
  110. ptr, &__x); \
  111. (x) = *(__force __typeof__(*(ptr)) *) &__x; \
  112. break; \
  113. }; \
  114. default: \
  115. __get_user_bad(); \
  116. break; \
  117. } \
  118. __gu_err; \
  119. })
  120. #define get_user(x, ptr) \
  121. ({ \
  122. const void __user *__p = (ptr); \
  123. might_fault(); \
  124. access_ok(VERIFY_READ, __p, sizeof(*ptr)) ? \
  125. __get_user((x), (__typeof__(*(ptr)) __user *)__p) :\
  126. ((x) = (__typeof__(*(ptr)))0,-EFAULT); \
  127. })
  128. #ifndef __get_user_fn
  129. static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
  130. {
  131. return unlikely(raw_copy_from_user(x, ptr, size)) ? -EFAULT : 0;
  132. }
  133. #define __get_user_fn(sz, u, k) __get_user_fn(sz, u, k)
  134. #endif
  135. extern int __get_user_bad(void) __attribute__((noreturn));
  136. /*
  137. * Copy a null terminated string from userspace.
  138. */
  139. #ifndef __strncpy_from_user
  140. static inline long
  141. __strncpy_from_user(char *dst, const char __user *src, long count)
  142. {
  143. char *tmp;
  144. strncpy(dst, (const char __force *)src, count);
  145. for (tmp = dst; *tmp && count > 0; tmp++, count--)
  146. ;
  147. return (tmp - dst);
  148. }
  149. #endif
  150. static inline long
  151. strncpy_from_user(char *dst, const char __user *src, long count)
  152. {
  153. if (!access_ok(VERIFY_READ, src, 1))
  154. return -EFAULT;
  155. return __strncpy_from_user(dst, src, count);
  156. }
  157. /*
  158. * Return the size of a string (including the ending 0)
  159. *
  160. * Return 0 on exception, a value greater than N if too long
  161. */
  162. #ifndef __strnlen_user
  163. #define __strnlen_user(s, n) (strnlen((s), (n)) + 1)
  164. #endif
  165. /*
  166. * Unlike strnlen, strnlen_user includes the nul terminator in
  167. * its returned count. Callers should check for a returned value
  168. * greater than N as an indication the string is too long.
  169. */
  170. static inline long strnlen_user(const char __user *src, long n)
  171. {
  172. if (!access_ok(VERIFY_READ, src, 1))
  173. return 0;
  174. return __strnlen_user(src, n);
  175. }
  176. /*
  177. * Zero Userspace
  178. */
  179. #ifndef __clear_user
  180. static inline __must_check unsigned long
  181. __clear_user(void __user *to, unsigned long n)
  182. {
  183. memset((void __force *)to, 0, n);
  184. return 0;
  185. }
  186. #endif
  187. static inline __must_check unsigned long
  188. clear_user(void __user *to, unsigned long n)
  189. {
  190. might_fault();
  191. if (!access_ok(VERIFY_WRITE, to, n))
  192. return n;
  193. return __clear_user(to, n);
  194. }
  195. #include <asm/extable.h>
  196. #endif /* __ASM_GENERIC_UACCESS_H */