strncpy_from_user.c 3.2 KB

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