usercopy.c 3.2 KB

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