maccess.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Access kernel or user memory without faulting.
  4. */
  5. #include <linux/export.h>
  6. #include <linux/mm.h>
  7. #include <linux/uaccess.h>
  8. #include <asm/tlb.h>
  9. bool __weak copy_from_kernel_nofault_allowed(const void *unsafe_src,
  10. size_t size)
  11. {
  12. return true;
  13. }
  14. /*
  15. * The below only uses kmsan_check_memory() to ensure uninitialized kernel
  16. * memory isn't leaked.
  17. */
  18. #define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
  19. while (len >= sizeof(type)) { \
  20. __get_kernel_nofault(dst, src, type, err_label); \
  21. kmsan_check_memory(src, sizeof(type)); \
  22. dst += sizeof(type); \
  23. src += sizeof(type); \
  24. len -= sizeof(type); \
  25. }
  26. long copy_from_kernel_nofault(void *dst, const void *src, size_t size)
  27. {
  28. unsigned long align = 0;
  29. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
  30. align = (unsigned long)dst | (unsigned long)src;
  31. if (!copy_from_kernel_nofault_allowed(src, size))
  32. return -ERANGE;
  33. pagefault_disable();
  34. if (!(align & 7))
  35. copy_from_kernel_nofault_loop(dst, src, size, u64, Efault);
  36. if (!(align & 3))
  37. copy_from_kernel_nofault_loop(dst, src, size, u32, Efault);
  38. if (!(align & 1))
  39. copy_from_kernel_nofault_loop(dst, src, size, u16, Efault);
  40. copy_from_kernel_nofault_loop(dst, src, size, u8, Efault);
  41. pagefault_enable();
  42. return 0;
  43. Efault:
  44. pagefault_enable();
  45. return -EFAULT;
  46. }
  47. EXPORT_SYMBOL_GPL(copy_from_kernel_nofault);
  48. #define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
  49. while (len >= sizeof(type)) { \
  50. __put_kernel_nofault(dst, src, type, err_label); \
  51. instrument_write(dst, sizeof(type)); \
  52. dst += sizeof(type); \
  53. src += sizeof(type); \
  54. len -= sizeof(type); \
  55. }
  56. long copy_to_kernel_nofault(void *dst, const void *src, size_t size)
  57. {
  58. unsigned long align = 0;
  59. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
  60. align = (unsigned long)dst | (unsigned long)src;
  61. pagefault_disable();
  62. if (!(align & 7))
  63. copy_to_kernel_nofault_loop(dst, src, size, u64, Efault);
  64. if (!(align & 3))
  65. copy_to_kernel_nofault_loop(dst, src, size, u32, Efault);
  66. if (!(align & 1))
  67. copy_to_kernel_nofault_loop(dst, src, size, u16, Efault);
  68. copy_to_kernel_nofault_loop(dst, src, size, u8, Efault);
  69. pagefault_enable();
  70. return 0;
  71. Efault:
  72. pagefault_enable();
  73. return -EFAULT;
  74. }
  75. long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
  76. {
  77. const void *src = unsafe_addr;
  78. if (unlikely(count <= 0))
  79. return 0;
  80. if (!copy_from_kernel_nofault_allowed(unsafe_addr, count))
  81. return -ERANGE;
  82. pagefault_disable();
  83. do {
  84. __get_kernel_nofault(dst, src, u8, Efault);
  85. dst++;
  86. src++;
  87. } while (dst[-1] && src - unsafe_addr < count);
  88. pagefault_enable();
  89. dst[-1] = '\0';
  90. return src - unsafe_addr;
  91. Efault:
  92. pagefault_enable();
  93. dst[0] = '\0';
  94. return -EFAULT;
  95. }
  96. /**
  97. * copy_from_user_nofault(): safely attempt to read from a user-space location
  98. * @dst: pointer to the buffer that shall take the data
  99. * @src: address to read from. This must be a user address.
  100. * @size: size of the data chunk
  101. *
  102. * Safely read from user address @src to the buffer at @dst. If a kernel fault
  103. * happens, handle that and return -EFAULT.
  104. */
  105. long copy_from_user_nofault(void *dst, const void __user *src, size_t size)
  106. {
  107. long ret = -EFAULT;
  108. if (!__access_ok(src, size))
  109. return ret;
  110. if (!nmi_uaccess_okay())
  111. return ret;
  112. pagefault_disable();
  113. ret = __copy_from_user_inatomic(dst, src, size);
  114. pagefault_enable();
  115. if (ret)
  116. return -EFAULT;
  117. return 0;
  118. }
  119. EXPORT_SYMBOL_GPL(copy_from_user_nofault);
  120. /**
  121. * copy_to_user_nofault(): safely attempt to write to a user-space location
  122. * @dst: address to write to
  123. * @src: pointer to the data that shall be written
  124. * @size: size of the data chunk
  125. *
  126. * Safely write to address @dst from the buffer at @src. If a kernel fault
  127. * happens, handle that and return -EFAULT.
  128. */
  129. long copy_to_user_nofault(void __user *dst, const void *src, size_t size)
  130. {
  131. long ret = -EFAULT;
  132. if (access_ok(dst, size)) {
  133. pagefault_disable();
  134. ret = __copy_to_user_inatomic(dst, src, size);
  135. pagefault_enable();
  136. }
  137. if (ret)
  138. return -EFAULT;
  139. return 0;
  140. }
  141. EXPORT_SYMBOL_GPL(copy_to_user_nofault);
  142. /**
  143. * strncpy_from_user_nofault: - Copy a NUL terminated string from unsafe user
  144. * address.
  145. * @dst: Destination address, in kernel space. This buffer must be at
  146. * least @count bytes long.
  147. * @unsafe_addr: Unsafe user address.
  148. * @count: Maximum number of bytes to copy, including the trailing NUL.
  149. *
  150. * Copies a NUL-terminated string from unsafe user address to kernel buffer.
  151. *
  152. * On success, returns the length of the string INCLUDING the trailing NUL.
  153. *
  154. * If access fails, returns -EFAULT (some data may have been copied
  155. * and the trailing NUL added).
  156. *
  157. * If @count is smaller than the length of the string, copies @count-1 bytes,
  158. * sets the last byte of @dst buffer to NUL and returns @count.
  159. */
  160. long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
  161. long count)
  162. {
  163. long ret;
  164. if (unlikely(count <= 0))
  165. return 0;
  166. pagefault_disable();
  167. ret = strncpy_from_user(dst, unsafe_addr, count);
  168. pagefault_enable();
  169. if (ret >= count) {
  170. ret = count;
  171. dst[ret - 1] = '\0';
  172. } else if (ret >= 0) {
  173. ret++;
  174. }
  175. return ret;
  176. }
  177. /**
  178. * strnlen_user_nofault: - Get the size of a user string INCLUDING final NUL.
  179. * @unsafe_addr: The string to measure.
  180. * @count: Maximum count (including NUL)
  181. *
  182. * Get the size of a NUL-terminated string in user space without pagefault.
  183. *
  184. * Returns the size of the string INCLUDING the terminating NUL.
  185. *
  186. * If the string is too long, returns a number larger than @count. User
  187. * has to check the return value against "> count".
  188. * On exception (or invalid count), returns 0.
  189. *
  190. * Unlike strnlen_user, this can be used from IRQ handler etc. because
  191. * it disables pagefaults.
  192. */
  193. long strnlen_user_nofault(const void __user *unsafe_addr, long count)
  194. {
  195. int ret;
  196. pagefault_disable();
  197. ret = strnlen_user(unsafe_addr, count);
  198. pagefault_enable();
  199. return ret;
  200. }
  201. void __copy_overflow(int size, unsigned long count)
  202. {
  203. WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
  204. }
  205. EXPORT_SYMBOL(__copy_overflow);