strncpy_from_user.c 3.2 KB

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