process_vm_access.c 10 KB

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