process_vm_access.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /* Work out address and page range required */
  85. if (len == 0)
  86. return 0;
  87. nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
  88. while (!rc && nr_pages && iov_iter_count(iter)) {
  89. int pages = min(nr_pages, max_pages_per_loop);
  90. size_t bytes;
  91. /* Get the pages we're interested in */
  92. down_read(&mm->mmap_sem);
  93. pages = get_user_pages(task, mm, pa, pages,
  94. vm_write, 0, process_pages, NULL);
  95. up_read(&mm->mmap_sem);
  96. if (pages <= 0)
  97. return -EFAULT;
  98. bytes = pages * PAGE_SIZE - start_offset;
  99. if (bytes > len)
  100. bytes = len;
  101. rc = process_vm_rw_pages(process_pages,
  102. start_offset, bytes, iter,
  103. vm_write);
  104. len -= bytes;
  105. start_offset = 0;
  106. nr_pages -= pages;
  107. pa += pages * PAGE_SIZE;
  108. while (pages)
  109. put_page(process_pages[--pages]);
  110. }
  111. return rc;
  112. }
  113. /* Maximum number of entries for process pages array
  114. which lives on stack */
  115. #define PVM_MAX_PP_ARRAY_COUNT 16
  116. /**
  117. * process_vm_rw_core - core of reading/writing pages from task specified
  118. * @pid: PID of process to read/write from/to
  119. * @iter: where to copy to/from locally
  120. * @rvec: iovec array specifying where to copy to/from in the other process
  121. * @riovcnt: size of rvec array
  122. * @flags: currently unused
  123. * @vm_write: 0 if reading from other process, 1 if writing to other process
  124. * Returns the number of bytes read/written or error code. May
  125. * return less bytes than expected if an error occurs during the copying
  126. * process.
  127. */
  128. static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
  129. const struct iovec *rvec,
  130. unsigned long riovcnt,
  131. unsigned long flags, int vm_write)
  132. {
  133. struct task_struct *task;
  134. struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
  135. struct page **process_pages = pp_stack;
  136. struct mm_struct *mm;
  137. unsigned long i;
  138. ssize_t rc = 0;
  139. unsigned long nr_pages = 0;
  140. unsigned long nr_pages_iov;
  141. ssize_t iov_len;
  142. size_t total_len = iov_iter_count(iter);
  143. /*
  144. * Work out how many pages of struct pages we're going to need
  145. * when eventually calling get_user_pages
  146. */
  147. for (i = 0; i < riovcnt; i++) {
  148. iov_len = rvec[i].iov_len;
  149. if (iov_len > 0) {
  150. nr_pages_iov = ((unsigned long)rvec[i].iov_base
  151. + iov_len)
  152. / PAGE_SIZE - (unsigned long)rvec[i].iov_base
  153. / PAGE_SIZE + 1;
  154. nr_pages = max(nr_pages, nr_pages_iov);
  155. }
  156. }
  157. if (nr_pages == 0)
  158. return 0;
  159. if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
  160. /* For reliability don't try to kmalloc more than
  161. 2 pages worth */
  162. process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
  163. sizeof(struct pages *)*nr_pages),
  164. GFP_KERNEL);
  165. if (!process_pages)
  166. return -ENOMEM;
  167. }
  168. /* Get process information */
  169. rcu_read_lock();
  170. task = find_task_by_vpid(pid);
  171. if (task)
  172. get_task_struct(task);
  173. rcu_read_unlock();
  174. if (!task) {
  175. rc = -ESRCH;
  176. goto free_proc_pages;
  177. }
  178. mm = mm_access(task, PTRACE_MODE_ATTACH);
  179. if (!mm || IS_ERR(mm)) {
  180. rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
  181. /*
  182. * Explicitly map EACCES to EPERM as EPERM is a more a
  183. * appropriate error code for process_vw_readv/writev
  184. */
  185. if (rc == -EACCES)
  186. rc = -EPERM;
  187. goto put_task_struct;
  188. }
  189. for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++)
  190. rc = process_vm_rw_single_vec(
  191. (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
  192. iter, process_pages, mm, task, vm_write);
  193. /* copied = space before - space after */
  194. total_len -= iov_iter_count(iter);
  195. /* If we have managed to copy any data at all then
  196. we return the number of bytes copied. Otherwise
  197. we return the error code */
  198. if (total_len)
  199. rc = total_len;
  200. mmput(mm);
  201. put_task_struct:
  202. put_task_struct(task);
  203. free_proc_pages:
  204. if (process_pages != pp_stack)
  205. kfree(process_pages);
  206. return rc;
  207. }
  208. /**
  209. * process_vm_rw - check iovecs before calling core routine
  210. * @pid: PID of process to read/write from/to
  211. * @lvec: iovec array specifying where to copy to/from locally
  212. * @liovcnt: size of lvec array
  213. * @rvec: iovec array specifying where to copy to/from in the other process
  214. * @riovcnt: size of rvec array
  215. * @flags: currently unused
  216. * @vm_write: 0 if reading from other process, 1 if writing to other process
  217. * Returns the number of bytes read/written or error code. May
  218. * return less bytes than expected if an error occurs during the copying
  219. * process.
  220. */
  221. static ssize_t process_vm_rw(pid_t pid,
  222. const struct iovec __user *lvec,
  223. unsigned long liovcnt,
  224. const struct iovec __user *rvec,
  225. unsigned long riovcnt,
  226. unsigned long flags, int vm_write)
  227. {
  228. struct iovec iovstack_l[UIO_FASTIOV];
  229. struct iovec iovstack_r[UIO_FASTIOV];
  230. struct iovec *iov_l = iovstack_l;
  231. struct iovec *iov_r = iovstack_r;
  232. struct iov_iter iter;
  233. ssize_t rc;
  234. if (flags != 0)
  235. return -EINVAL;
  236. /* Check iovecs */
  237. if (vm_write)
  238. rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
  239. iovstack_l, &iov_l);
  240. else
  241. rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
  242. iovstack_l, &iov_l);
  243. if (rc <= 0)
  244. goto free_iovecs;
  245. iov_iter_init(&iter, vm_write ? WRITE : READ, iov_l, liovcnt, rc);
  246. rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
  247. iovstack_r, &iov_r);
  248. if (rc <= 0)
  249. goto free_iovecs;
  250. rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
  251. free_iovecs:
  252. if (iov_r != iovstack_r)
  253. kfree(iov_r);
  254. if (iov_l != iovstack_l)
  255. kfree(iov_l);
  256. return rc;
  257. }
  258. SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
  259. unsigned long, liovcnt, const struct iovec __user *, rvec,
  260. unsigned long, riovcnt, unsigned long, flags)
  261. {
  262. return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
  263. }
  264. SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
  265. const struct iovec __user *, lvec,
  266. unsigned long, liovcnt, const struct iovec __user *, rvec,
  267. unsigned long, riovcnt, unsigned long, flags)
  268. {
  269. return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
  270. }
  271. #ifdef CONFIG_COMPAT
  272. static ssize_t
  273. compat_process_vm_rw(compat_pid_t pid,
  274. const struct compat_iovec __user *lvec,
  275. unsigned long liovcnt,
  276. const struct compat_iovec __user *rvec,
  277. unsigned long riovcnt,
  278. unsigned long flags, int vm_write)
  279. {
  280. struct iovec iovstack_l[UIO_FASTIOV];
  281. struct iovec iovstack_r[UIO_FASTIOV];
  282. struct iovec *iov_l = iovstack_l;
  283. struct iovec *iov_r = iovstack_r;
  284. struct iov_iter iter;
  285. ssize_t rc = -EFAULT;
  286. if (flags != 0)
  287. return -EINVAL;
  288. if (vm_write)
  289. rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
  290. UIO_FASTIOV, iovstack_l,
  291. &iov_l);
  292. else
  293. rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
  294. UIO_FASTIOV, iovstack_l,
  295. &iov_l);
  296. if (rc <= 0)
  297. goto free_iovecs;
  298. iov_iter_init(&iter, vm_write ? WRITE : READ, iov_l, liovcnt, rc);
  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. if (iov_l != iovstack_l)
  309. kfree(iov_l);
  310. return rc;
  311. }
  312. COMPAT_SYSCALL_DEFINE6(process_vm_readv, compat_pid_t, pid,
  313. const struct compat_iovec __user *, lvec,
  314. compat_ulong_t, liovcnt,
  315. const struct compat_iovec __user *, rvec,
  316. compat_ulong_t, riovcnt,
  317. compat_ulong_t, flags)
  318. {
  319. return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
  320. riovcnt, flags, 0);
  321. }
  322. COMPAT_SYSCALL_DEFINE6(process_vm_writev, compat_pid_t, pid,
  323. const struct compat_iovec __user *, lvec,
  324. compat_ulong_t, liovcnt,
  325. const struct compat_iovec __user *, rvec,
  326. compat_ulong_t, riovcnt,
  327. compat_ulong_t, flags)
  328. {
  329. return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
  330. riovcnt, flags, 1);
  331. }
  332. #endif