maccess.c 5.9 KB

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