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