process_vm_access.c 8.2 KB

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