usercopy.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /* out-of-line parts */
  7. #ifndef INLINE_COPY_FROM_USER
  8. unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
  9. {
  10. unsigned long res = n;
  11. might_fault();
  12. if (!should_fail_usercopy() && likely(access_ok(from, n))) {
  13. instrument_copy_from_user(to, from, n);
  14. res = raw_copy_from_user(to, from, n);
  15. }
  16. if (unlikely(res))
  17. memset(to + (n - res), 0, res);
  18. return res;
  19. }
  20. EXPORT_SYMBOL(_copy_from_user);
  21. #endif
  22. #ifndef INLINE_COPY_TO_USER
  23. unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
  24. {
  25. might_fault();
  26. if (should_fail_usercopy())
  27. return n;
  28. if (likely(access_ok(to, n))) {
  29. instrument_copy_to_user(to, from, n);
  30. n = raw_copy_to_user(to, from, n);
  31. }
  32. return n;
  33. }
  34. EXPORT_SYMBOL(_copy_to_user);
  35. #endif
  36. /**
  37. * check_zeroed_user: check if a userspace buffer only contains zero bytes
  38. * @from: Source address, in userspace.
  39. * @size: Size of buffer.
  40. *
  41. * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
  42. * userspace addresses (and is more efficient because we don't care where the
  43. * first non-zero byte is).
  44. *
  45. * Returns:
  46. * * 0: There were non-zero bytes present in the buffer.
  47. * * 1: The buffer was full of zero bytes.
  48. * * -EFAULT: access to userspace failed.
  49. */
  50. int check_zeroed_user(const void __user *from, size_t size)
  51. {
  52. unsigned long val;
  53. uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
  54. if (unlikely(size == 0))
  55. return 1;
  56. from -= align;
  57. size += align;
  58. if (!user_read_access_begin(from, size))
  59. return -EFAULT;
  60. unsafe_get_user(val, (unsigned long __user *) from, err_fault);
  61. if (align)
  62. val &= ~aligned_byte_mask(align);
  63. while (size > sizeof(unsigned long)) {
  64. if (unlikely(val))
  65. goto done;
  66. from += sizeof(unsigned long);
  67. size -= sizeof(unsigned long);
  68. unsafe_get_user(val, (unsigned long __user *) from, err_fault);
  69. }
  70. if (size < sizeof(unsigned long))
  71. val &= aligned_byte_mask(size);
  72. done:
  73. user_read_access_end();
  74. return (val == 0);
  75. err_fault:
  76. user_read_access_end();
  77. return -EFAULT;
  78. }
  79. EXPORT_SYMBOL(check_zeroed_user);