strncpy_from_user.c 2.9 KB

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