process_vm_access.c 9.9 KB

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