process_vm_access.c 10.0 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/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 = FOLL_REMOTE;
  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. size_t bytes;
  94. /*
  95. * Get the pages we're interested in. We must
  96. * add FOLL_REMOTE because task/mm might not
  97. * current/current->mm
  98. */
  99. pages = __get_user_pages_unlocked(task, mm, pa, pages,
  100. process_pages, flags);
  101. if (pages <= 0)
  102. return -EFAULT;
  103. bytes = pages * PAGE_SIZE - start_offset;
  104. if (bytes > len)
  105. bytes = len;
  106. rc = process_vm_rw_pages(process_pages,
  107. start_offset, bytes, iter,
  108. vm_write);
  109. len -= bytes;
  110. start_offset = 0;
  111. nr_pages -= pages;
  112. pa += pages * PAGE_SIZE;
  113. while (pages)
  114. put_page(process_pages[--pages]);
  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. * 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. rcu_read_lock();
  175. task = find_task_by_vpid(pid);
  176. if (task)
  177. get_task_struct(task);
  178. rcu_read_unlock();
  179. if (!task) {
  180. rc = -ESRCH;
  181. goto free_proc_pages;
  182. }
  183. mm = mm_access(task, PTRACE_MODE_ATTACH_REALCREDS);
  184. if (!mm || IS_ERR(mm)) {
  185. rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
  186. /*
  187. * Explicitly map EACCES to EPERM as EPERM is a more a
  188. * appropriate error code for process_vw_readv/writev
  189. */
  190. if (rc == -EACCES)
  191. rc = -EPERM;
  192. goto put_task_struct;
  193. }
  194. for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++)
  195. rc = process_vm_rw_single_vec(
  196. (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
  197. iter, process_pages, mm, task, vm_write);
  198. /* copied = space before - space after */
  199. total_len -= iov_iter_count(iter);
  200. /* If we have managed to copy any data at all then
  201. we return the number of bytes copied. Otherwise
  202. we return the error code */
  203. if (total_len)
  204. rc = total_len;
  205. mmput(mm);
  206. put_task_struct:
  207. put_task_struct(task);
  208. free_proc_pages:
  209. if (process_pages != pp_stack)
  210. kfree(process_pages);
  211. return rc;
  212. }
  213. /**
  214. * process_vm_rw - check iovecs before calling core routine
  215. * @pid: PID of process to read/write from/to
  216. * @lvec: iovec array specifying where to copy to/from locally
  217. * @liovcnt: size of lvec array
  218. * @rvec: iovec array specifying where to copy to/from in the other process
  219. * @riovcnt: size of rvec array
  220. * @flags: currently unused
  221. * @vm_write: 0 if reading from other process, 1 if writing to other process
  222. * Returns the number of bytes read/written or error code. May
  223. * return less bytes than expected if an error occurs during the copying
  224. * process.
  225. */
  226. static ssize_t process_vm_rw(pid_t pid,
  227. const struct iovec __user *lvec,
  228. unsigned long liovcnt,
  229. const struct iovec __user *rvec,
  230. unsigned long riovcnt,
  231. unsigned long flags, int vm_write)
  232. {
  233. struct iovec iovstack_l[UIO_FASTIOV];
  234. struct iovec iovstack_r[UIO_FASTIOV];
  235. struct iovec *iov_l = iovstack_l;
  236. struct iovec *iov_r = iovstack_r;
  237. struct iov_iter iter;
  238. ssize_t rc;
  239. int dir = vm_write ? WRITE : READ;
  240. if (flags != 0)
  241. return -EINVAL;
  242. /* Check iovecs */
  243. rc = import_iovec(dir, lvec, liovcnt, UIO_FASTIOV, &iov_l, &iter);
  244. if (rc < 0)
  245. return rc;
  246. if (!iov_iter_count(&iter))
  247. goto free_iovecs;
  248. rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
  249. iovstack_r, &iov_r);
  250. if (rc <= 0)
  251. goto free_iovecs;
  252. rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
  253. free_iovecs:
  254. if (iov_r != iovstack_r)
  255. kfree(iov_r);
  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. }
  272. #ifdef CONFIG_COMPAT
  273. static ssize_t
  274. compat_process_vm_rw(compat_pid_t pid,
  275. const struct compat_iovec __user *lvec,
  276. unsigned long liovcnt,
  277. const struct compat_iovec __user *rvec,
  278. unsigned long riovcnt,
  279. unsigned long flags, int vm_write)
  280. {
  281. struct iovec iovstack_l[UIO_FASTIOV];
  282. struct iovec iovstack_r[UIO_FASTIOV];
  283. struct iovec *iov_l = iovstack_l;
  284. struct iovec *iov_r = iovstack_r;
  285. struct iov_iter iter;
  286. ssize_t rc = -EFAULT;
  287. int dir = vm_write ? WRITE : READ;
  288. if (flags != 0)
  289. return -EINVAL;
  290. rc = compat_import_iovec(dir, lvec, liovcnt, UIO_FASTIOV, &iov_l, &iter);
  291. if (rc < 0)
  292. return rc;
  293. if (!iov_iter_count(&iter))
  294. goto free_iovecs;
  295. rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
  296. UIO_FASTIOV, iovstack_r,
  297. &iov_r);
  298. if (rc <= 0)
  299. goto free_iovecs;
  300. rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
  301. free_iovecs:
  302. if (iov_r != iovstack_r)
  303. kfree(iov_r);
  304. kfree(iov_l);
  305. return rc;
  306. }
  307. COMPAT_SYSCALL_DEFINE6(process_vm_readv, compat_pid_t, pid,
  308. const struct compat_iovec __user *, lvec,
  309. compat_ulong_t, liovcnt,
  310. const struct compat_iovec __user *, rvec,
  311. compat_ulong_t, riovcnt,
  312. compat_ulong_t, flags)
  313. {
  314. return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
  315. riovcnt, flags, 0);
  316. }
  317. COMPAT_SYSCALL_DEFINE6(process_vm_writev, compat_pid_t, pid,
  318. const struct compat_iovec __user *, lvec,
  319. compat_ulong_t, liovcnt,
  320. const struct compat_iovec __user *, rvec,
  321. compat_ulong_t, riovcnt,
  322. compat_ulong_t, flags)
  323. {
  324. return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
  325. riovcnt, flags, 1);
  326. }
  327. #endif