process_vm_access.c 12 KB

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