process_vm_access.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * @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. *
  135. * Returns the number of bytes read/written or error code. May
  136. * return less bytes than expected if an error occurs during the copying
  137. * process.
  138. */
  139. static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
  140. const struct iovec *rvec,
  141. unsigned long riovcnt,
  142. unsigned long flags, int vm_write)
  143. {
  144. struct task_struct *task;
  145. struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
  146. struct page **process_pages = pp_stack;
  147. struct mm_struct *mm;
  148. unsigned long i;
  149. ssize_t rc = 0;
  150. unsigned long nr_pages = 0;
  151. unsigned long nr_pages_iov;
  152. ssize_t iov_len;
  153. size_t total_len = iov_iter_count(iter);
  154. /*
  155. * Work out how many pages of struct pages we're going to need
  156. * when eventually calling get_user_pages
  157. */
  158. for (i = 0; i < riovcnt; i++) {
  159. iov_len = rvec[i].iov_len;
  160. if (iov_len > 0) {
  161. nr_pages_iov = ((unsigned long)rvec[i].iov_base
  162. + iov_len)
  163. / PAGE_SIZE - (unsigned long)rvec[i].iov_base
  164. / PAGE_SIZE + 1;
  165. nr_pages = max(nr_pages, nr_pages_iov);
  166. }
  167. }
  168. if (nr_pages == 0)
  169. return 0;
  170. if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
  171. /* For reliability don't try to kmalloc more than
  172. 2 pages worth */
  173. process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
  174. sizeof(struct pages *)*nr_pages),
  175. GFP_KERNEL);
  176. if (!process_pages)
  177. return -ENOMEM;
  178. }
  179. /* Get process information */
  180. task = find_get_task_by_vpid(pid);
  181. if (!task) {
  182. rc = -ESRCH;
  183. goto free_proc_pages;
  184. }
  185. mm = mm_access(task, PTRACE_MODE_ATTACH_REALCREDS);
  186. if (!mm || IS_ERR(mm)) {
  187. rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
  188. /*
  189. * Explicitly map EACCES to EPERM as EPERM is a more a
  190. * appropriate error code for process_vw_readv/writev
  191. */
  192. if (rc == -EACCES)
  193. rc = -EPERM;
  194. goto put_task_struct;
  195. }
  196. for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++)
  197. rc = process_vm_rw_single_vec(
  198. (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
  199. iter, process_pages, mm, task, vm_write);
  200. /* copied = space before - space after */
  201. total_len -= iov_iter_count(iter);
  202. /* If we have managed to copy any data at all then
  203. we return the number of bytes copied. Otherwise
  204. we return the error code */
  205. if (total_len)
  206. rc = total_len;
  207. mmput(mm);
  208. put_task_struct:
  209. put_task_struct(task);
  210. free_proc_pages:
  211. if (process_pages != pp_stack)
  212. kfree(process_pages);
  213. return rc;
  214. }
  215. /**
  216. * process_vm_rw - check iovecs before calling core routine
  217. * @pid: PID of process to read/write from/to
  218. * @lvec: iovec array specifying where to copy to/from locally
  219. * @liovcnt: size of lvec array
  220. * @rvec: iovec array specifying where to copy to/from in the other process
  221. * @riovcnt: size of rvec array
  222. * @flags: currently unused
  223. * @vm_write: 0 if reading from other process, 1 if writing to other process
  224. *
  225. * Returns the number of bytes read/written or error code. May
  226. * return less bytes than expected if an error occurs during the copying
  227. * process.
  228. */
  229. static ssize_t process_vm_rw(pid_t pid,
  230. const struct iovec __user *lvec,
  231. unsigned long liovcnt,
  232. const struct iovec __user *rvec,
  233. unsigned long riovcnt,
  234. unsigned long flags, int vm_write)
  235. {
  236. struct iovec iovstack_l[UIO_FASTIOV];
  237. struct iovec iovstack_r[UIO_FASTIOV];
  238. struct iovec *iov_l = iovstack_l;
  239. struct iovec *iov_r = iovstack_r;
  240. struct iov_iter iter;
  241. ssize_t rc;
  242. int dir = vm_write ? WRITE : READ;
  243. if (flags != 0)
  244. return -EINVAL;
  245. /* Check iovecs */
  246. rc = import_iovec(dir, lvec, liovcnt, UIO_FASTIOV, &iov_l, &iter);
  247. if (rc < 0)
  248. return rc;
  249. if (!iov_iter_count(&iter))
  250. goto free_iovecs;
  251. rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
  252. iovstack_r, &iov_r);
  253. if (rc <= 0)
  254. goto free_iovecs;
  255. rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
  256. free_iovecs:
  257. if (iov_r != iovstack_r)
  258. kfree(iov_r);
  259. kfree(iov_l);
  260. return rc;
  261. }
  262. SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
  263. unsigned long, liovcnt, const struct iovec __user *, rvec,
  264. unsigned long, riovcnt, unsigned long, flags)
  265. {
  266. return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
  267. }
  268. SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
  269. const struct iovec __user *, lvec,
  270. unsigned long, liovcnt, const struct iovec __user *, rvec,
  271. unsigned long, riovcnt, unsigned long, flags)
  272. {
  273. return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
  274. }
  275. #ifdef CONFIG_COMPAT
  276. static ssize_t
  277. compat_process_vm_rw(compat_pid_t pid,
  278. const struct compat_iovec __user *lvec,
  279. unsigned long liovcnt,
  280. const struct compat_iovec __user *rvec,
  281. unsigned long riovcnt,
  282. unsigned long flags, int vm_write)
  283. {
  284. struct iovec iovstack_l[UIO_FASTIOV];
  285. struct iovec iovstack_r[UIO_FASTIOV];
  286. struct iovec *iov_l = iovstack_l;
  287. struct iovec *iov_r = iovstack_r;
  288. struct iov_iter iter;
  289. ssize_t rc = -EFAULT;
  290. int dir = vm_write ? WRITE : READ;
  291. if (flags != 0)
  292. return -EINVAL;
  293. rc = compat_import_iovec(dir, lvec, liovcnt, UIO_FASTIOV, &iov_l, &iter);
  294. if (rc < 0)
  295. return rc;
  296. if (!iov_iter_count(&iter))
  297. goto free_iovecs;
  298. rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
  299. UIO_FASTIOV, iovstack_r,
  300. &iov_r);
  301. if (rc <= 0)
  302. goto free_iovecs;
  303. rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
  304. free_iovecs:
  305. if (iov_r != iovstack_r)
  306. kfree(iov_r);
  307. kfree(iov_l);
  308. return rc;
  309. }
  310. COMPAT_SYSCALL_DEFINE6(process_vm_readv, compat_pid_t, pid,
  311. const struct compat_iovec __user *, lvec,
  312. compat_ulong_t, liovcnt,
  313. const struct compat_iovec __user *, rvec,
  314. compat_ulong_t, riovcnt,
  315. compat_ulong_t, flags)
  316. {
  317. return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
  318. riovcnt, flags, 0);
  319. }
  320. COMPAT_SYSCALL_DEFINE6(process_vm_writev, compat_pid_t, pid,
  321. const struct compat_iovec __user *, lvec,
  322. compat_ulong_t, liovcnt,
  323. const struct compat_iovec __user *, rvec,
  324. compat_ulong_t, riovcnt,
  325. compat_ulong_t, flags)
  326. {
  327. return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
  328. riovcnt, flags, 1);
  329. }
  330. #endif