process_vm_access.c 10 KB

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