process_vm_access.c 11 KB

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