usercopy.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitops.h>
  3. #include <linux/fault-inject-usercopy.h>
  4. #include <linux/instrumented.h>
  5. #include <linux/uaccess.h>
  6. #include <linux/nospec.h>
  7. /* out-of-line parts */
  8. #ifndef INLINE_COPY_FROM_USER
  9. unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
  10. {
  11. unsigned long res = n;
  12. might_fault();
  13. if (!should_fail_usercopy() && likely(access_ok(from, n))) {
  14. /*
  15. * Ensure that bad access_ok() speculation will not
  16. * lead to nasty side effects *after* the copy is
  17. * finished:
  18. */
  19. barrier_nospec();
  20. instrument_copy_from_user_before(to, from, n);
  21. res = raw_copy_from_user(to, from, n);
  22. instrument_copy_from_user_after(to, from, n, res);
  23. }
  24. if (unlikely(res))
  25. memset(to + (n - res), 0, res);
  26. return res;
  27. }
  28. EXPORT_SYMBOL(_copy_from_user);
  29. #endif
  30. #ifndef INLINE_COPY_TO_USER
  31. unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
  32. {
  33. might_fault();
  34. if (should_fail_usercopy())
  35. return n;
  36. if (likely(access_ok(to, n))) {
  37. instrument_copy_to_user(to, from, n);
  38. n = raw_copy_to_user(to, from, n);
  39. }
  40. return n;
  41. }
  42. EXPORT_SYMBOL(_copy_to_user);
  43. #endif
  44. /**
  45. * check_zeroed_user: check if a userspace buffer only contains zero bytes
  46. * @from: Source address, in userspace.
  47. * @size: Size of buffer.
  48. *
  49. * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
  50. * userspace addresses (and is more efficient because we don't care where the
  51. * first non-zero byte is).
  52. *
  53. * Returns:
  54. * * 0: There were non-zero bytes present in the buffer.
  55. * * 1: The buffer was full of zero bytes.
  56. * * -EFAULT: access to userspace failed.
  57. */
  58. int check_zeroed_user(const void __user *from, size_t size)
  59. {
  60. unsigned long val;
  61. uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
  62. if (unlikely(size == 0))
  63. return 1;
  64. from -= align;
  65. size += align;
  66. if (!user_read_access_begin(from, size))
  67. return -EFAULT;
  68. unsafe_get_user(val, (unsigned long __user *) from, err_fault);
  69. if (align)
  70. val &= ~aligned_byte_mask(align);
  71. while (size > sizeof(unsigned long)) {
  72. if (unlikely(val))
  73. goto done;
  74. from += sizeof(unsigned long);
  75. size -= sizeof(unsigned long);
  76. unsafe_get_user(val, (unsigned long __user *) from, err_fault);
  77. }
  78. if (size < sizeof(unsigned long))
  79. val &= aligned_byte_mask(size);
  80. done:
  81. user_read_access_end();
  82. return (val == 0);
  83. err_fault:
  84. user_read_access_end();
  85. return -EFAULT;
  86. }
  87. EXPORT_SYMBOL(check_zeroed_user);