strncpy_from_user.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <linux/export.h>
  4. #include <linux/fault-inject-usercopy.h>
  5. #include <linux/kasan-checks.h>
  6. #include <linux/thread_info.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/mm.h>
  11. #include <asm/byteorder.h>
  12. #include <asm/word-at-a-time.h>
  13. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  14. #define IS_UNALIGNED(src, dst) 0
  15. #else
  16. #define IS_UNALIGNED(src, dst) \
  17. (((long) dst | (long) src) & (sizeof(long) - 1))
  18. #endif
  19. /*
  20. * Do a strncpy, return length of string without final '\0'.
  21. * 'count' is the user-supplied count (return 'count' if we
  22. * hit it), 'max' is the address space maximum (and we return
  23. * -EFAULT if we hit it).
  24. */
  25. static inline long do_strncpy_from_user(char *dst, const char __user *src,
  26. unsigned long count, unsigned long max)
  27. {
  28. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  29. unsigned long res = 0;
  30. if (IS_UNALIGNED(src, dst))
  31. goto byte_at_a_time;
  32. while (max >= sizeof(unsigned long)) {
  33. unsigned long c, data, mask;
  34. /* Fall back to byte-at-a-time if we get a page fault */
  35. unsafe_get_user(c, (unsigned long __user *)(src+res), byte_at_a_time);
  36. /*
  37. * Note that we mask out the bytes following the NUL. This is
  38. * important to do because string oblivious code may read past
  39. * the NUL. For those routines, we don't want to give them
  40. * potentially random bytes after the NUL in `src`.
  41. *
  42. * One example of such code is BPF map keys. BPF treats map keys
  43. * as an opaque set of bytes. Without the post-NUL mask, any BPF
  44. * maps keyed by strings returned from strncpy_from_user() may
  45. * have multiple entries for semantically identical strings.
  46. */
  47. if (has_zero(c, &data, &constants)) {
  48. data = prep_zero_mask(c, data, &constants);
  49. data = create_zero_mask(data);
  50. mask = zero_bytemask(data);
  51. *(unsigned long *)(dst+res) = c & mask;
  52. return res + find_zero(data);
  53. }
  54. *(unsigned long *)(dst+res) = c;
  55. res += sizeof(unsigned long);
  56. max -= sizeof(unsigned long);
  57. }
  58. byte_at_a_time:
  59. while (max) {
  60. char c;
  61. unsafe_get_user(c,src+res, efault);
  62. dst[res] = c;
  63. if (!c)
  64. return res;
  65. res++;
  66. max--;
  67. }
  68. /*
  69. * Uhhuh. We hit 'max'. But was that the user-specified maximum
  70. * too? If so, that's ok - we got as much as the user asked for.
  71. */
  72. if (res >= count)
  73. return res;
  74. /*
  75. * Nope: we hit the address space limit, and we still had more
  76. * characters the caller would have wanted. That's an EFAULT.
  77. */
  78. efault:
  79. return -EFAULT;
  80. }
  81. /**
  82. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  83. * @dst: Destination address, in kernel space. This buffer must be at
  84. * least @count bytes long.
  85. * @src: Source address, in user space.
  86. * @count: Maximum number of bytes to copy, including the trailing NUL.
  87. *
  88. * Copies a NUL-terminated string from userspace to kernel space.
  89. *
  90. * On success, returns the length of the string (not including the trailing
  91. * NUL).
  92. *
  93. * If access to userspace fails, returns -EFAULT (some data may have been
  94. * copied).
  95. *
  96. * If @count is smaller than the length of the string, copies @count bytes
  97. * and returns @count.
  98. */
  99. long strncpy_from_user(char *dst, const char __user *src, long count)
  100. {
  101. unsigned long max_addr, src_addr;
  102. might_fault();
  103. if (should_fail_usercopy())
  104. return -EFAULT;
  105. if (unlikely(count <= 0))
  106. return 0;
  107. max_addr = user_addr_max();
  108. src_addr = (unsigned long)untagged_addr(src);
  109. if (likely(src_addr < max_addr)) {
  110. unsigned long max = max_addr - src_addr;
  111. long retval;
  112. /*
  113. * Truncate 'max' to the user-specified limit, so that
  114. * we only have one limit we need to check in the loop
  115. */
  116. if (max > count)
  117. max = count;
  118. kasan_check_write(dst, count);
  119. check_object_size(dst, count, false);
  120. if (user_read_access_begin(src, max)) {
  121. retval = do_strncpy_from_user(dst, src, count, max);
  122. user_read_access_end();
  123. return retval;
  124. }
  125. }
  126. return -EFAULT;
  127. }
  128. EXPORT_SYMBOL(strncpy_from_user);