process_vm_access.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/mm/process_vm_access.c
  4. *
  5. * Copyright (C) 2010-2011 Christopher Yeoh <[email protected]>, IBM Corp.
  6. */
  7. #include <linux/compat.h>
  8. #include <linux/mm.h>
  9. #include <linux/uio.h>
  10. #include <linux/sched.h>
  11. #include <linux/sched/mm.h>
  12. #include <linux/highmem.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/slab.h>
  15. #include <linux/syscalls.h>
  16. /**
  17. * process_vm_rw_pages - read/write pages from task specified
  18. * @pages: array of pointers to pages we want to copy
  19. * @offset: offset in page to start copying from/to
  20. * @len: number of bytes to copy
  21. * @iter: where to copy to/from locally
  22. * @vm_write: 0 means copy from, 1 means copy to
  23. * Returns 0 on success, error code otherwise
  24. */
  25. static int process_vm_rw_pages(struct page **pages,
  26. unsigned offset,
  27. size_t len,
  28. struct iov_iter *iter,
  29. int vm_write)
  30. {
  31. /* Do the copy for each page */
  32. while (len && iov_iter_count(iter)) {
  33. struct page *page = *pages++;
  34. size_t copy = PAGE_SIZE - offset;
  35. size_t copied;
  36. if (copy > len)
  37. copy = len;
  38. if (vm_write)
  39. copied = copy_page_from_iter(page, offset, copy, iter);
  40. else
  41. copied = copy_page_to_iter(page, offset, copy, iter);
  42. len -= copied;
  43. if (copied < copy && iov_iter_count(iter))
  44. return -EFAULT;
  45. offset = 0;
  46. }
  47. return 0;
  48. }
  49. /* Maximum number of pages kmalloc'd to hold struct page's during copy */
  50. #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
  51. /**
  52. * process_vm_rw_single_vec - read/write pages from task specified
  53. * @addr: start memory address of target process
  54. * @len: size of area to copy to/from
  55. * @iter: where to copy to/from locally
  56. * @process_pages: struct pages area that can store at least
  57. * nr_pages_to_copy struct page pointers
  58. * @mm: mm for task
  59. * @task: task to read/write from
  60. * @vm_write: 0 means copy from, 1 means copy to
  61. * Returns 0 on success or on failure error code
  62. */
  63. static int process_vm_rw_single_vec(unsigned long addr,
  64. unsigned long len,
  65. struct iov_iter *iter,
  66. struct page **process_pages,
  67. struct mm_struct *mm,
  68. struct task_struct *task,
  69. int vm_write)
  70. {
  71. unsigned long pa = addr & PAGE_MASK;
  72. unsigned long start_offset = addr - pa;
  73. unsigned long nr_pages;
  74. ssize_t rc = 0;
  75. unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
  76. / sizeof(struct pages *);
  77. unsigned int flags = 0;
  78. /* Work out address and page range required */
  79. if (len == 0)
  80. return 0;
  81. nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
  82. if (vm_write)
  83. flags |= FOLL_WRITE;
  84. while (!rc && nr_pages && iov_iter_count(iter)) {
  85. int pinned_pages = min(nr_pages, max_pages_per_loop);
  86. int locked = 1;
  87. size_t bytes;
  88. /*
  89. * Get the pages we're interested in. We must
  90. * access remotely because task/mm might not
  91. * current/current->mm
  92. */
  93. mmap_read_lock(mm);
  94. pinned_pages = pin_user_pages_remote(mm, pa, pinned_pages,
  95. flags, process_pages,
  96. NULL, &locked);
  97. if (locked)
  98. mmap_read_unlock(mm);
  99. if (pinned_pages <= 0)
  100. return -EFAULT;
  101. bytes = pinned_pages * PAGE_SIZE - start_offset;
  102. if (bytes > len)
  103. bytes = len;
  104. rc = process_vm_rw_pages(process_pages,
  105. start_offset, bytes, iter,
  106. vm_write);
  107. len -= bytes;
  108. start_offset = 0;
  109. nr_pages -= pinned_pages;
  110. pa += pinned_pages * PAGE_SIZE;
  111. /* If vm_write is set, the pages need to be made dirty: */
  112. unpin_user_pages_dirty_lock(process_pages, pinned_pages,
  113. vm_write);
  114. }
  115. return rc;
  116. }
  117. /* Maximum number of entries for process pages array
  118. which lives on stack */
  119. #define PVM_MAX_PP_ARRAY_COUNT 16
  120. /**
  121. * process_vm_rw_core - core of reading/writing pages from task specified
  122. * @pid: PID of process to read/write from/to
  123. * @iter: where to copy to/from locally
  124. * @rvec: iovec array specifying where to copy to/from in the other process
  125. * @riovcnt: size of rvec array
  126. * @flags: currently unused
  127. * @vm_write: 0 if reading from other process, 1 if writing to other process
  128. *
  129. * Returns the number of bytes read/written or error code. May
  130. * return less bytes than expected if an error occurs during the copying
  131. * process.
  132. */
  133. static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
  134. const struct iovec *rvec,
  135. unsigned long riovcnt,
  136. unsigned long flags, int vm_write)
  137. {
  138. struct task_struct *task;
  139. struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
  140. struct page **process_pages = pp_stack;
  141. struct mm_struct *mm;
  142. unsigned long i;
  143. ssize_t rc = 0;
  144. unsigned long nr_pages = 0;
  145. unsigned long nr_pages_iov;
  146. ssize_t iov_len;
  147. size_t total_len = iov_iter_count(iter);
  148. /*
  149. * Work out how many pages of struct pages we're going to need
  150. * when eventually calling get_user_pages
  151. */
  152. for (i = 0; i < riovcnt; i++) {
  153. iov_len = rvec[i].iov_len;
  154. if (iov_len > 0) {
  155. nr_pages_iov = ((unsigned long)rvec[i].iov_base
  156. + iov_len)
  157. / PAGE_SIZE - (unsigned long)rvec[i].iov_base
  158. / PAGE_SIZE + 1;
  159. nr_pages = max(nr_pages, nr_pages_iov);
  160. }
  161. }
  162. if (nr_pages == 0)
  163. return 0;
  164. if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
  165. /* For reliability don't try to kmalloc more than
  166. 2 pages worth */
  167. process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
  168. sizeof(struct pages *)*nr_pages),
  169. GFP_KERNEL);
  170. if (!process_pages)
  171. return -ENOMEM;
  172. }
  173. /* Get process information */
  174. task = find_get_task_by_vpid(pid);
  175. if (!task) {
  176. rc = -ESRCH;
  177. goto free_proc_pages;
  178. }
  179. mm = mm_access(task, PTRACE_MODE_ATTACH_REALCREDS);
  180. if (!mm || IS_ERR(mm)) {
  181. rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
  182. /*
  183. * Explicitly map EACCES to EPERM as EPERM is a more
  184. * appropriate error code for process_vw_readv/writev
  185. */
  186. if (rc == -EACCES)
  187. rc = -EPERM;
  188. goto put_task_struct;
  189. }
  190. for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++)
  191. rc = process_vm_rw_single_vec(
  192. (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
  193. iter, process_pages, mm, task, vm_write);
  194. /* copied = space before - space after */
  195. total_len -= iov_iter_count(iter);
  196. /* If we have managed to copy any data at all then
  197. we return the number of bytes copied. Otherwise
  198. we return the error code */
  199. if (total_len)
  200. rc = total_len;
  201. mmput(mm);
  202. put_task_struct:
  203. put_task_struct(task);
  204. free_proc_pages:
  205. if (process_pages != pp_stack)
  206. kfree(process_pages);
  207. return rc;
  208. }
  209. /**
  210. * process_vm_rw - check iovecs before calling core routine
  211. * @pid: PID of process to read/write from/to
  212. * @lvec: iovec array specifying where to copy to/from locally
  213. * @liovcnt: size of lvec array
  214. * @rvec: iovec array specifying where to copy to/from in the other process
  215. * @riovcnt: size of rvec array
  216. * @flags: currently unused
  217. * @vm_write: 0 if reading from other process, 1 if writing to other process
  218. *
  219. * Returns the number of bytes read/written or error code. May
  220. * return less bytes than expected if an error occurs during the copying
  221. * process.
  222. */
  223. static ssize_t process_vm_rw(pid_t pid,
  224. const struct iovec __user *lvec,
  225. unsigned long liovcnt,
  226. const struct iovec __user *rvec,
  227. unsigned long riovcnt,
  228. unsigned long flags, int vm_write)
  229. {
  230. struct iovec iovstack_l[UIO_FASTIOV];
  231. struct iovec iovstack_r[UIO_FASTIOV];
  232. struct iovec *iov_l = iovstack_l;
  233. struct iovec *iov_r;
  234. struct iov_iter iter;
  235. ssize_t rc;
  236. int dir = vm_write ? ITER_SOURCE : ITER_DEST;
  237. if (flags != 0)
  238. return -EINVAL;
  239. /* Check iovecs */
  240. rc = import_iovec(dir, lvec, liovcnt, UIO_FASTIOV, &iov_l, &iter);
  241. if (rc < 0)
  242. return rc;
  243. if (!iov_iter_count(&iter))
  244. goto free_iov_l;
  245. iov_r = iovec_from_user(rvec, riovcnt, UIO_FASTIOV, iovstack_r,
  246. in_compat_syscall());
  247. if (IS_ERR(iov_r)) {
  248. rc = PTR_ERR(iov_r);
  249. goto free_iov_l;
  250. }
  251. rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
  252. if (iov_r != iovstack_r)
  253. kfree(iov_r);
  254. free_iov_l:
  255. kfree(iov_l);
  256. return rc;
  257. }
  258. SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
  259. unsigned long, liovcnt, const struct iovec __user *, rvec,
  260. unsigned long, riovcnt, unsigned long, flags)
  261. {
  262. return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
  263. }
  264. SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
  265. const struct iovec __user *, lvec,
  266. unsigned long, liovcnt, const struct iovec __user *, rvec,
  267. unsigned long, riovcnt, unsigned long, flags)
  268. {
  269. return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
  270. }