strncpy_from_user.c 3.1 KB

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