strncpy_from_user.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <linux/export.h>
  4. #include <linux/kasan-checks.h>
  5. #include <linux/thread_info.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <asm/byteorder.h>
  10. #include <asm/word-at-a-time.h>
  11. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  12. #define IS_UNALIGNED(src, dst) 0
  13. #else
  14. #define IS_UNALIGNED(src, dst) \
  15. (((long) dst | (long) src) & (sizeof(long) - 1))
  16. #endif
  17. /*
  18. * Do a strncpy, return length of string without final '\0'.
  19. * 'count' is the user-supplied count (return 'count' if we
  20. * hit it), 'max' is the address space maximum (and we return
  21. * -EFAULT if we hit it).
  22. */
  23. static inline long do_strncpy_from_user(char *dst, const char __user *src,
  24. unsigned long count, unsigned long max)
  25. {
  26. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  27. unsigned long res = 0;
  28. if (IS_UNALIGNED(src, dst))
  29. goto byte_at_a_time;
  30. while (max >= sizeof(unsigned long)) {
  31. unsigned long c, data;
  32. /* Fall back to byte-at-a-time if we get a page fault */
  33. unsafe_get_user(c, (unsigned long __user *)(src+res), byte_at_a_time);
  34. *(unsigned long *)(dst+res) = c;
  35. if (has_zero(c, &data, &constants)) {
  36. data = prep_zero_mask(c, data, &constants);
  37. data = create_zero_mask(data);
  38. return res + find_zero(data);
  39. }
  40. res += sizeof(unsigned long);
  41. max -= sizeof(unsigned long);
  42. }
  43. byte_at_a_time:
  44. while (max) {
  45. char c;
  46. unsafe_get_user(c,src+res, efault);
  47. dst[res] = c;
  48. if (!c)
  49. return res;
  50. res++;
  51. max--;
  52. }
  53. /*
  54. * Uhhuh. We hit 'max'. But was that the user-specified maximum
  55. * too? If so, that's ok - we got as much as the user asked for.
  56. */
  57. if (res >= count)
  58. return res;
  59. /*
  60. * Nope: we hit the address space limit, and we still had more
  61. * characters the caller would have wanted. That's an EFAULT.
  62. */
  63. efault:
  64. return -EFAULT;
  65. }
  66. /**
  67. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  68. * @dst: Destination address, in kernel space. This buffer must be at
  69. * least @count bytes long.
  70. * @src: Source address, in user space.
  71. * @count: Maximum number of bytes to copy, including the trailing NUL.
  72. *
  73. * Copies a NUL-terminated string from userspace to kernel space.
  74. *
  75. * On success, returns the length of the string (not including the trailing
  76. * NUL).
  77. *
  78. * If access to userspace fails, returns -EFAULT (some data may have been
  79. * copied).
  80. *
  81. * If @count is smaller than the length of the string, copies @count bytes
  82. * and returns @count.
  83. */
  84. long strncpy_from_user(char *dst, const char __user *src, long count)
  85. {
  86. unsigned long max_addr, src_addr;
  87. if (unlikely(count <= 0))
  88. return 0;
  89. max_addr = user_addr_max();
  90. src_addr = (unsigned long)src;
  91. if (likely(src_addr < max_addr)) {
  92. unsigned long max = max_addr - src_addr;
  93. long retval;
  94. /*
  95. * Truncate 'max' to the user-specified limit, so that
  96. * we only have one limit we need to check in the loop
  97. */
  98. if (max > count)
  99. max = count;
  100. kasan_check_write(dst, count);
  101. check_object_size(dst, count, false);
  102. if (user_access_begin(VERIFY_READ, src, max)) {
  103. retval = do_strncpy_from_user(dst, src, count, max);
  104. user_access_end();
  105. return retval;
  106. }
  107. }
  108. return -EFAULT;
  109. }
  110. EXPORT_SYMBOL(strncpy_from_user);