usercopy.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. static inline unsigned long count_bytes(unsigned long mask)
  38. {
  39. mask = (mask - 1) & ~mask;
  40. mask >>= 7;
  41. return count_masked_bytes(mask);
  42. }
  43. /*
  44. * Do a strncpy, return length of string without final '\0'.
  45. * 'count' is the user-supplied count (return 'count' if we
  46. * hit it), 'max' is the address space maximum (and we return
  47. * -EFAULT if we hit it).
  48. */
  49. static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
  50. {
  51. long res = 0;
  52. /*
  53. * Truncate 'max' to the user-specified limit, so that
  54. * we only have one limit we need to check in the loop
  55. */
  56. if (max > count)
  57. max = count;
  58. while (max >= sizeof(unsigned long)) {
  59. unsigned long c;
  60. /* Fall back to byte-at-a-time if we get a page fault */
  61. if (unlikely(__get_user(c,(unsigned long __user *)(src+res))))
  62. break;
  63. /* This can write a few bytes past the NUL character, but that's ok */
  64. *(unsigned long *)(dst+res) = c;
  65. c = has_zero(c);
  66. if (c)
  67. return res + count_bytes(c);
  68. res += sizeof(unsigned long);
  69. max -= sizeof(unsigned long);
  70. }
  71. while (max) {
  72. char c;
  73. if (unlikely(__get_user(c,src+res)))
  74. return -EFAULT;
  75. dst[res] = c;
  76. if (!c)
  77. return res;
  78. res++;
  79. max--;
  80. }
  81. /*
  82. * Uhhuh. We hit 'max'. But was that the user-specified maximum
  83. * too? If so, that's ok - we got as much as the user asked for.
  84. */
  85. if (res >= count)
  86. return res;
  87. /*
  88. * Nope: we hit the address space limit, and we still had more
  89. * characters the caller would have wanted. That's an EFAULT.
  90. */
  91. return -EFAULT;
  92. }
  93. /**
  94. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  95. * @dst: Destination address, in kernel space. This buffer must be at
  96. * least @count bytes long.
  97. * @src: Source address, in user space.
  98. * @count: Maximum number of bytes to copy, including the trailing NUL.
  99. *
  100. * Copies a NUL-terminated string from userspace to kernel space.
  101. *
  102. * On success, returns the length of the string (not including the trailing
  103. * NUL).
  104. *
  105. * If access to userspace fails, returns -EFAULT (some data may have been
  106. * copied).
  107. *
  108. * If @count is smaller than the length of the string, copies @count bytes
  109. * and returns @count.
  110. */
  111. long
  112. strncpy_from_user(char *dst, const char __user *src, long count)
  113. {
  114. unsigned long max_addr, src_addr;
  115. if (unlikely(count <= 0))
  116. return 0;
  117. max_addr = current_thread_info()->addr_limit.seg;
  118. src_addr = (unsigned long)src;
  119. if (likely(src_addr < max_addr)) {
  120. unsigned long max = max_addr - src_addr;
  121. return do_strncpy_from_user(dst, src, count, max);
  122. }
  123. return -EFAULT;
  124. }
  125. EXPORT_SYMBOL(strncpy_from_user);