maccess.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Access kernel memory without faulting.
  3. */
  4. #include <linux/export.h>
  5. #include <linux/mm.h>
  6. #include <linux/uaccess.h>
  7. static __always_inline long
  8. probe_read_common(void *dst, const void __user *src, size_t size)
  9. {
  10. long ret;
  11. pagefault_disable();
  12. ret = __copy_from_user_inatomic(dst, src, size);
  13. pagefault_enable();
  14. return ret ? -EFAULT : 0;
  15. }
  16. static __always_inline long
  17. probe_write_common(void __user *dst, const void *src, size_t size)
  18. {
  19. long ret;
  20. pagefault_disable();
  21. ret = __copy_to_user_inatomic(dst, src, size);
  22. pagefault_enable();
  23. return ret ? -EFAULT : 0;
  24. }
  25. /**
  26. * probe_kernel_read(): safely attempt to read from a kernel-space location
  27. * @dst: pointer to the buffer that shall take the data
  28. * @src: address to read from
  29. * @size: size of the data chunk
  30. *
  31. * Safely read from address @src to the buffer at @dst. If a kernel fault
  32. * happens, handle that and return -EFAULT.
  33. *
  34. * We ensure that the copy_from_user is executed in atomic context so that
  35. * do_page_fault() doesn't attempt to take mmap_sem. This makes
  36. * probe_kernel_read() suitable for use within regions where the caller
  37. * already holds mmap_sem, or other locks which nest inside mmap_sem.
  38. */
  39. long __weak probe_kernel_read(void *dst, const void *src, size_t size)
  40. __attribute__((alias("__probe_kernel_read")));
  41. long __probe_kernel_read(void *dst, const void *src, size_t size)
  42. {
  43. long ret;
  44. mm_segment_t old_fs = get_fs();
  45. set_fs(KERNEL_DS);
  46. ret = probe_read_common(dst, (__force const void __user *)src, size);
  47. set_fs(old_fs);
  48. return ret;
  49. }
  50. EXPORT_SYMBOL_GPL(probe_kernel_read);
  51. /**
  52. * probe_user_read(): safely attempt to read from a user-space location
  53. * @dst: pointer to the buffer that shall take the data
  54. * @src: address to read from. This must be a user address.
  55. * @size: size of the data chunk
  56. *
  57. * Safely read from user address @src to the buffer at @dst. If a kernel fault
  58. * happens, handle that and return -EFAULT.
  59. */
  60. long __weak probe_user_read(void *dst, const void __user *src, size_t size)
  61. __attribute__((alias("__probe_user_read")));
  62. long __probe_user_read(void *dst, const void __user *src, size_t size)
  63. {
  64. long ret = -EFAULT;
  65. mm_segment_t old_fs = get_fs();
  66. set_fs(USER_DS);
  67. if (access_ok(VERIFY_READ, src, size))
  68. ret = probe_read_common(dst, src, size);
  69. set_fs(old_fs);
  70. return ret;
  71. }
  72. EXPORT_SYMBOL_GPL(probe_user_read);
  73. /**
  74. * probe_kernel_write(): safely attempt to write to a location
  75. * @dst: address to write to
  76. * @src: pointer to the data that shall be written
  77. * @size: size of the data chunk
  78. *
  79. * Safely write to address @dst from the buffer at @src. If a kernel fault
  80. * happens, handle that and return -EFAULT.
  81. */
  82. long __weak probe_kernel_write(void *dst, const void *src, size_t size)
  83. __attribute__((alias("__probe_kernel_write")));
  84. long __probe_kernel_write(void *dst, const void *src, size_t size)
  85. {
  86. long ret;
  87. mm_segment_t old_fs = get_fs();
  88. set_fs(KERNEL_DS);
  89. ret = probe_write_common((__force void __user *)dst, src, size);
  90. set_fs(old_fs);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL_GPL(probe_kernel_write);
  94. /**
  95. * probe_user_write(): safely attempt to write to a user-space location
  96. * @dst: address to write to
  97. * @src: pointer to the data that shall be written
  98. * @size: size of the data chunk
  99. *
  100. * Safely write to address @dst from the buffer at @src. If a kernel fault
  101. * happens, handle that and return -EFAULT.
  102. */
  103. long __weak probe_user_write(void __user *dst, const void *src, size_t size)
  104. __attribute__((alias("__probe_user_write")));
  105. long __probe_user_write(void __user *dst, const void *src, size_t size)
  106. {
  107. long ret = -EFAULT;
  108. mm_segment_t old_fs = get_fs();
  109. set_fs(USER_DS);
  110. if (access_ok(VERIFY_WRITE, dst, size))
  111. ret = probe_write_common(dst, src, size);
  112. set_fs(old_fs);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL_GPL(probe_user_write);
  116. /**
  117. * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
  118. * @dst: Destination address, in kernel space. This buffer must be at
  119. * least @count bytes long.
  120. * @unsafe_addr: Unsafe address.
  121. * @count: Maximum number of bytes to copy, including the trailing NUL.
  122. *
  123. * Copies a NUL-terminated string from unsafe address to kernel buffer.
  124. *
  125. * On success, returns the length of the string INCLUDING the trailing NUL.
  126. *
  127. * If access fails, returns -EFAULT (some data may have been copied
  128. * and the trailing NUL added).
  129. *
  130. * If @count is smaller than the length of the string, copies @count-1 bytes,
  131. * sets the last byte of @dst buffer to NUL and returns @count.
  132. */
  133. long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
  134. {
  135. mm_segment_t old_fs = get_fs();
  136. const void *src = unsafe_addr;
  137. long ret;
  138. if (unlikely(count <= 0))
  139. return 0;
  140. set_fs(KERNEL_DS);
  141. pagefault_disable();
  142. do {
  143. ret = __get_user(*dst++, (const char __user __force *)src++);
  144. } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
  145. dst[-1] = '\0';
  146. pagefault_enable();
  147. set_fs(old_fs);
  148. return ret ? -EFAULT : src - unsafe_addr;
  149. }
  150. /**
  151. * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user
  152. * address.
  153. * @dst: Destination address, in kernel space. This buffer must be at
  154. * least @count bytes long.
  155. * @unsafe_addr: Unsafe user address.
  156. * @count: Maximum number of bytes to copy, including the trailing NUL.
  157. *
  158. * Copies a NUL-terminated string from unsafe user address to kernel buffer.
  159. *
  160. * On success, returns the length of the string INCLUDING the trailing NUL.
  161. *
  162. * If access fails, returns -EFAULT (some data may have been copied
  163. * and the trailing NUL added).
  164. *
  165. * If @count is smaller than the length of the string, copies @count-1 bytes,
  166. * sets the last byte of @dst buffer to NUL and returns @count.
  167. */
  168. long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
  169. long count)
  170. {
  171. mm_segment_t old_fs = get_fs();
  172. long ret;
  173. if (unlikely(count <= 0))
  174. return 0;
  175. set_fs(USER_DS);
  176. pagefault_disable();
  177. ret = strncpy_from_user(dst, unsafe_addr, count);
  178. pagefault_enable();
  179. set_fs(old_fs);
  180. if (ret >= count) {
  181. ret = count;
  182. dst[ret - 1] = '\0';
  183. } else if (ret > 0) {
  184. ret++;
  185. }
  186. return ret;
  187. }
  188. /**
  189. * strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL.
  190. * @unsafe_addr: The string to measure.
  191. * @count: Maximum count (including NUL)
  192. *
  193. * Get the size of a NUL-terminated string in user space without pagefault.
  194. *
  195. * Returns the size of the string INCLUDING the terminating NUL.
  196. *
  197. * If the string is too long, returns a number larger than @count. User
  198. * has to check the return value against "> count".
  199. * On exception (or invalid count), returns 0.
  200. *
  201. * Unlike strnlen_user, this can be used from IRQ handler etc. because
  202. * it disables pagefaults.
  203. */
  204. long strnlen_unsafe_user(const void __user *unsafe_addr, long count)
  205. {
  206. mm_segment_t old_fs = get_fs();
  207. int ret;
  208. set_fs(USER_DS);
  209. pagefault_disable();
  210. ret = strnlen_user(unsafe_addr, count);
  211. pagefault_enable();
  212. set_fs(old_fs);
  213. return ret;
  214. }