process_vm_access.c 9.8 KB

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