strncpy_from_user.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <linux/compiler.h>
  2. #include <linux/export.h>
  3. #include <linux/uaccess.h>
  4. #include <linux/kernel.h>
  5. #include <linux/errno.h>
  6. #include <asm/byteorder.h>
  7. #include <asm/word-at-a-time.h>
  8. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  9. #define IS_UNALIGNED(src, dst) 0
  10. #else
  11. #define IS_UNALIGNED(src, dst) \
  12. (((long) dst | (long) src) & (sizeof(long) - 1))
  13. #endif
  14. /*
  15. * Do a strncpy, return length of string without final '\0'.
  16. * 'count' is the user-supplied count (return 'count' if we
  17. * hit it), 'max' is the address space maximum (and we return
  18. * -EFAULT if we hit it).
  19. */
  20. static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
  21. {
  22. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  23. long res = 0;
  24. /*
  25. * Truncate 'max' to the user-specified limit, so that
  26. * we only have one limit we need to check in the loop
  27. */
  28. if (max > count)
  29. max = count;
  30. if (IS_UNALIGNED(src, dst))
  31. goto byte_at_a_time;
  32. while (max >= sizeof(unsigned long)) {
  33. unsigned long c, data;
  34. /* Fall back to byte-at-a-time if we get a page fault */
  35. if (unlikely(__get_user(c,(unsigned long __user *)(src+res))))
  36. break;
  37. *(unsigned long *)(dst+res) = c;
  38. if (has_zero(c, &data, &constants)) {
  39. data = prep_zero_mask(c, data, &constants);
  40. data = create_zero_mask(data);
  41. return res + find_zero(data);
  42. }
  43. res += sizeof(unsigned long);
  44. max -= sizeof(unsigned long);
  45. }
  46. byte_at_a_time:
  47. while (max) {
  48. char c;
  49. if (unlikely(__get_user(c,src+res)))
  50. return -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. return -EFAULT;
  68. }
  69. /**
  70. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  71. * @dst: Destination address, in kernel space. This buffer must be at
  72. * least @count bytes long.
  73. * @src: Source address, in user space.
  74. * @count: Maximum number of bytes to copy, including the trailing NUL.
  75. *
  76. * Copies a NUL-terminated string from userspace to kernel space.
  77. *
  78. * On success, returns the length of the string (not including the trailing
  79. * NUL).
  80. *
  81. * If access to userspace fails, returns -EFAULT (some data may have been
  82. * copied).
  83. *
  84. * If @count is smaller than the length of the string, copies @count bytes
  85. * and returns @count.
  86. */
  87. long strncpy_from_user(char *dst, const char __user *src, long count)
  88. {
  89. unsigned long max_addr, src_addr;
  90. if (unlikely(count <= 0))
  91. return 0;
  92. max_addr = user_addr_max();
  93. src_addr = (unsigned long)src;
  94. if (likely(src_addr < max_addr)) {
  95. unsigned long max = max_addr - src_addr;
  96. return do_strncpy_from_user(dst, src, count, max);
  97. }
  98. return -EFAULT;
  99. }
  100. EXPORT_SYMBOL(strncpy_from_user);