usercopy.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <linux/module.h>
  2. #include <linux/uaccess.h>
  3. #include <linux/kernel.h>
  4. #include <linux/errno.h>
  5. #include <linux/bug.h>
  6. #include <asm/byteorder.h>
  7. void copy_from_user_overflow(void)
  8. {
  9. WARN(1, "Buffer overflow detected!\n");
  10. }
  11. EXPORT_SYMBOL(copy_from_user_overflow);
  12. static inline long find_zero(unsigned long mask)
  13. {
  14. long byte = 0;
  15. #ifdef __BIG_ENDIAN
  16. #ifdef CONFIG_64BIT
  17. if (mask >> 32)
  18. mask >>= 32;
  19. else
  20. byte = 4;
  21. #endif
  22. if (mask >> 16)
  23. mask >>= 16;
  24. else
  25. byte += 2;
  26. return (mask >> 8) ? byte : byte + 1;
  27. #else
  28. #ifdef CONFIG_64BIT
  29. if (!((unsigned int) mask)) {
  30. mask >>= 32;
  31. byte = 4;
  32. }
  33. #endif
  34. if (!(mask & 0xffff)) {
  35. mask >>= 16;
  36. byte += 2;
  37. }
  38. return (mask & 0xff) ? byte : byte + 1;
  39. #endif
  40. }
  41. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  42. #define IS_UNALIGNED(src, dst) 0
  43. #else
  44. #define IS_UNALIGNED(src, dst) \
  45. (((long) dst | (long) src) & (sizeof(long) - 1))
  46. #endif
  47. /*
  48. * Do a strncpy, return length of string without final '\0'.
  49. * 'count' is the user-supplied count (return 'count' if we
  50. * hit it), 'max' is the address space maximum (and we return
  51. * -EFAULT if we hit it).
  52. */
  53. static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
  54. {
  55. const unsigned long high_bits = REPEAT_BYTE(0xfe) + 1;
  56. const unsigned long low_bits = REPEAT_BYTE(0x7f);
  57. long res = 0;
  58. /*
  59. * Truncate 'max' to the user-specified limit, so that
  60. * we only have one limit we need to check in the loop
  61. */
  62. if (max > count)
  63. max = count;
  64. if (IS_UNALIGNED(src, dst))
  65. goto byte_at_a_time;
  66. while (max >= sizeof(unsigned long)) {
  67. unsigned long c, v, rhs;
  68. /* Fall back to byte-at-a-time if we get a page fault */
  69. if (unlikely(__get_user(c,(unsigned long __user *)(src+res))))
  70. break;
  71. rhs = c | low_bits;
  72. v = (c + high_bits) & ~rhs;
  73. *(unsigned long *)(dst+res) = c;
  74. if (v) {
  75. v = (c & low_bits) + low_bits;
  76. v = ~(v | rhs);
  77. return res + find_zero(v);
  78. }
  79. res += sizeof(unsigned long);
  80. max -= sizeof(unsigned long);
  81. }
  82. byte_at_a_time:
  83. while (max) {
  84. char c;
  85. if (unlikely(__get_user(c,src+res)))
  86. return -EFAULT;
  87. dst[res] = c;
  88. if (!c)
  89. return res;
  90. res++;
  91. max--;
  92. }
  93. /*
  94. * Uhhuh. We hit 'max'. But was that the user-specified maximum
  95. * too? If so, that's ok - we got as much as the user asked for.
  96. */
  97. if (res >= count)
  98. return res;
  99. /*
  100. * Nope: we hit the address space limit, and we still had more
  101. * characters the caller would have wanted. That's an EFAULT.
  102. */
  103. return -EFAULT;
  104. }
  105. /**
  106. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  107. * @dst: Destination address, in kernel space. This buffer must be at
  108. * least @count bytes long.
  109. * @src: Source address, in user space.
  110. * @count: Maximum number of bytes to copy, including the trailing NUL.
  111. *
  112. * Copies a NUL-terminated string from userspace to kernel space.
  113. *
  114. * On success, returns the length of the string (not including the trailing
  115. * NUL).
  116. *
  117. * If access to userspace fails, returns -EFAULT (some data may have been
  118. * copied).
  119. *
  120. * If @count is smaller than the length of the string, copies @count bytes
  121. * and returns @count.
  122. */
  123. long strncpy_from_user(char *dst, const char __user *src, long count)
  124. {
  125. unsigned long max_addr, src_addr;
  126. if (unlikely(count <= 0))
  127. return 0;
  128. max_addr = user_addr_max();
  129. src_addr = (unsigned long)src;
  130. if (likely(src_addr < max_addr)) {
  131. unsigned long max = max_addr - src_addr;
  132. return do_strncpy_from_user(dst, src, count, max);
  133. }
  134. return -EFAULT;
  135. }
  136. EXPORT_SYMBOL(strncpy_from_user);