usercopy.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * User address space access functions.
  3. *
  4. * For licencing details see kernel-base/COPYING
  5. */
  6. #include <linux/highmem.h>
  7. #include <linux/module.h>
  8. #include <asm/word-at-a-time.h>
  9. /*
  10. * best effort, GUP based copy_from_user() that is NMI-safe
  11. */
  12. unsigned long
  13. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  14. {
  15. unsigned long offset, addr = (unsigned long)from;
  16. unsigned long size, len = 0;
  17. struct page *page;
  18. void *map;
  19. int ret;
  20. do {
  21. ret = __get_user_pages_fast(addr, 1, 0, &page);
  22. if (!ret)
  23. break;
  24. offset = addr & (PAGE_SIZE - 1);
  25. size = min(PAGE_SIZE - offset, n - len);
  26. map = kmap_atomic(page);
  27. memcpy(to, map+offset, size);
  28. kunmap_atomic(map);
  29. put_page(page);
  30. len += size;
  31. to += size;
  32. addr += size;
  33. } while (len < n);
  34. return len;
  35. }
  36. EXPORT_SYMBOL_GPL(copy_from_user_nmi);
  37. /*
  38. * Do a strncpy, return length of string without final '\0'.
  39. * 'count' is the user-supplied count (return 'count' if we
  40. * hit it), 'max' is the address space maximum (and we return
  41. * -EFAULT if we hit it).
  42. */
  43. static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
  44. {
  45. long res = 0;
  46. /*
  47. * Truncate 'max' to the user-specified limit, so that
  48. * we only have one limit we need to check in the loop
  49. */
  50. if (max > count)
  51. max = count;
  52. while (max >= sizeof(unsigned long)) {
  53. unsigned long c, mask;
  54. /* Fall back to byte-at-a-time if we get a page fault */
  55. if (unlikely(__get_user(c,(unsigned long __user *)(src+res))))
  56. break;
  57. mask = has_zero(c);
  58. if (mask) {
  59. mask = (mask - 1) & ~mask;
  60. mask >>= 7;
  61. *(unsigned long *)(dst+res) = c & mask;
  62. return res + count_masked_bytes(mask);
  63. }
  64. *(unsigned long *)(dst+res) = c;
  65. res += sizeof(unsigned long);
  66. max -= sizeof(unsigned long);
  67. }
  68. while (max) {
  69. char c;
  70. if (unlikely(__get_user(c,src+res)))
  71. return -EFAULT;
  72. dst[res] = c;
  73. if (!c)
  74. return res;
  75. res++;
  76. max--;
  77. }
  78. /*
  79. * Uhhuh. We hit 'max'. But was that the user-specified maximum
  80. * too? If so, that's ok - we got as much as the user asked for.
  81. */
  82. if (res >= count)
  83. return res;
  84. /*
  85. * Nope: we hit the address space limit, and we still had more
  86. * characters the caller would have wanted. That's an EFAULT.
  87. */
  88. return -EFAULT;
  89. }
  90. /**
  91. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  92. * @dst: Destination address, in kernel space. This buffer must be at
  93. * least @count bytes long.
  94. * @src: Source address, in user space.
  95. * @count: Maximum number of bytes to copy, including the trailing NUL.
  96. *
  97. * Copies a NUL-terminated string from userspace to kernel space.
  98. *
  99. * On success, returns the length of the string (not including the trailing
  100. * NUL).
  101. *
  102. * If access to userspace fails, returns -EFAULT (some data may have been
  103. * copied).
  104. *
  105. * If @count is smaller than the length of the string, copies @count bytes
  106. * and returns @count.
  107. */
  108. long
  109. strncpy_from_user(char *dst, const char __user *src, long count)
  110. {
  111. unsigned long max_addr, src_addr;
  112. if (unlikely(count <= 0))
  113. return 0;
  114. max_addr = current_thread_info()->addr_limit.seg;
  115. src_addr = (unsigned long)src;
  116. if (likely(src_addr < max_addr)) {
  117. unsigned long max = max_addr - src_addr;
  118. return do_strncpy_from_user(dst, src, count, max);
  119. }
  120. return -EFAULT;
  121. }
  122. EXPORT_SYMBOL(strncpy_from_user);