blob: 83252d783482b4a758c016ddb2cef799903ae763 [file] [log] [blame]
Christopher Yeohfcf63402011-10-31 17:06:39 -07001/*
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
12#include <linux/mm.h>
13#include <linux/uio.h>
14#include <linux/sched.h>
15#include <linux/highmem.h>
16#include <linux/ptrace.h>
17#include <linux/slab.h>
18#include <linux/syscalls.h>
19
20#ifdef CONFIG_COMPAT
21#include <linux/compat.h>
22#endif
23
24/**
25 * process_vm_rw_pages - read/write pages from task specified
26 * @task: task to read/write from
27 * @mm: mm for task
28 * @process_pages: struct pages area that can store at least
29 * nr_pages_to_copy struct page pointers
30 * @pa: address of page in task to start copying from/to
31 * @start_offset: offset in page to start copying from/to
32 * @len: number of bytes to copy
33 * @lvec: iovec array specifying where to copy to/from
34 * @lvec_cnt: number of elements in iovec array
35 * @lvec_current: index in iovec array we are up to
36 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
37 * @vm_write: 0 means copy from, 1 means copy to
38 * @nr_pages_to_copy: number of pages to copy
39 * @bytes_copied: returns number of bytes successfully copied
40 * Returns 0 on success, error code otherwise
41 */
Al Viro70eca122014-02-05 12:44:24 -050042static int process_vm_rw_pages(struct page **pages,
43 unsigned offset,
Al Viroe21345f2014-02-05 12:55:11 -050044 size_t len,
Al Viro9f78bdf2014-02-05 11:51:53 -050045 struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -070046 int vm_write,
Christopher Yeohfcf63402011-10-31 17:06:39 -070047 ssize_t *bytes_copied)
48{
Christopher Yeohfcf63402011-10-31 17:06:39 -070049 *bytes_copied = 0;
50
Christopher Yeohfcf63402011-10-31 17:06:39 -070051 /* Do the copy for each page */
Al Viroe21345f2014-02-05 12:55:11 -050052 while (iov_iter_count(iter) && len) {
Al Viro70eca122014-02-05 12:44:24 -050053 struct page *page = *pages++;
Al Viroe21345f2014-02-05 12:55:11 -050054 size_t copy = PAGE_SIZE - offset;
Al Viro70eca122014-02-05 12:44:24 -050055 size_t copied;
Christopher Yeohfcf63402011-10-31 17:06:39 -070056
Al Viroe21345f2014-02-05 12:55:11 -050057 if (copy > len)
58 copy = len;
59
Al Viro240f3902014-02-05 12:14:11 -050060 if (vm_write) {
Al Viro70eca122014-02-05 12:44:24 -050061 if (copy > iov_iter_count(iter))
62 copy = iov_iter_count(iter);
63 copied = iov_iter_copy_from_user(page, iter,
64 offset, copy);
65 iov_iter_advance(iter, copied);
Al Viro240f3902014-02-05 12:14:11 -050066 set_page_dirty_lock(page);
Christopher Yeohfcf63402011-10-31 17:06:39 -070067 } else {
Al Viro70eca122014-02-05 12:44:24 -050068 copied = copy_page_to_iter(page, offset, copy, iter);
Christopher Yeohfcf63402011-10-31 17:06:39 -070069 }
Al Viro70eca122014-02-05 12:44:24 -050070 *bytes_copied += copied;
71 len -= copied;
72 if (copied < copy && iov_iter_count(iter))
73 return -EFAULT;
74 offset = 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -070075 }
Al Viro70eca122014-02-05 12:44:24 -050076 return 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -070077}
78
79/* Maximum number of pages kmalloc'd to hold struct page's during copy */
80#define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
81
82/**
83 * process_vm_rw_single_vec - read/write pages from task specified
84 * @addr: start memory address of target process
85 * @len: size of area to copy to/from
86 * @lvec: iovec array specifying where to copy to/from locally
87 * @lvec_cnt: number of elements in iovec array
88 * @lvec_current: index in iovec array we are up to
89 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
90 * @process_pages: struct pages area that can store at least
91 * nr_pages_to_copy struct page pointers
92 * @mm: mm for task
93 * @task: task to read/write from
94 * @vm_write: 0 means copy from, 1 means copy to
95 * @bytes_copied: returns number of bytes successfully copied
96 * Returns 0 on success or on failure error code
97 */
98static int process_vm_rw_single_vec(unsigned long addr,
99 unsigned long len,
Al Viro9f78bdf2014-02-05 11:51:53 -0500100 struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700101 struct page **process_pages,
102 struct mm_struct *mm,
103 struct task_struct *task,
104 int vm_write,
105 ssize_t *bytes_copied)
106{
107 unsigned long pa = addr & PAGE_MASK;
108 unsigned long start_offset = addr - pa;
109 unsigned long nr_pages;
110 ssize_t bytes_copied_loop;
111 ssize_t rc = 0;
112 unsigned long nr_pages_copied = 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700113 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
114 / sizeof(struct pages *);
115
116 *bytes_copied = 0;
117
118 /* Work out address and page range required */
119 if (len == 0)
120 return 0;
121 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
122
Al Viro240f3902014-02-05 12:14:11 -0500123 while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) {
Al Viro70eca122014-02-05 12:44:24 -0500124 int nr_pages_to_copy;
125 int pages_pinned;
Al Viroe21345f2014-02-05 12:55:11 -0500126 size_t n;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700127 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
128 max_pages_per_loop);
129
Al Viro70eca122014-02-05 12:44:24 -0500130 /* Get the pages we're interested in */
131 down_read(&mm->mmap_sem);
132 pages_pinned = get_user_pages(task, mm, pa,
133 nr_pages_to_copy,
134 vm_write, 0, process_pages, NULL);
135 up_read(&mm->mmap_sem);
136
137 if (pages_pinned <= 0)
138 return -EFAULT;
139
Al Viroe21345f2014-02-05 12:55:11 -0500140 n = pages_pinned * PAGE_SIZE - start_offset;
141 if (n > len)
142 n = len;
143
Al Viro70eca122014-02-05 12:44:24 -0500144 rc = process_vm_rw_pages(process_pages,
Al Viroe21345f2014-02-05 12:55:11 -0500145 start_offset, n, iter,
146 vm_write,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700147 &bytes_copied_loop);
Al Viroe21345f2014-02-05 12:55:11 -0500148 len -= n;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700149 start_offset = 0;
150 *bytes_copied += bytes_copied_loop;
Al Viro70eca122014-02-05 12:44:24 -0500151 nr_pages_copied += pages_pinned;
152 pa += pages_pinned * PAGE_SIZE;
153 while (pages_pinned)
154 put_page(process_pages[--pages_pinned]);
Al Viro70eca122014-02-05 12:44:24 -0500155 if (rc < 0)
156 break;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700157 }
158
159 return rc;
160}
161
162/* Maximum number of entries for process pages array
163 which lives on stack */
164#define PVM_MAX_PP_ARRAY_COUNT 16
165
166/**
167 * process_vm_rw_core - core of reading/writing pages from task specified
168 * @pid: PID of process to read/write from/to
169 * @lvec: iovec array specifying where to copy to/from locally
170 * @liovcnt: size of lvec array
171 * @rvec: iovec array specifying where to copy to/from in the other process
172 * @riovcnt: size of rvec array
173 * @flags: currently unused
174 * @vm_write: 0 if reading from other process, 1 if writing to other process
175 * Returns the number of bytes read/written or error code. May
176 * return less bytes than expected if an error occurs during the copying
177 * process.
178 */
Al Viro9f78bdf2014-02-05 11:51:53 -0500179static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700180 const struct iovec *rvec,
181 unsigned long riovcnt,
182 unsigned long flags, int vm_write)
183{
184 struct task_struct *task;
185 struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
186 struct page **process_pages = pp_stack;
187 struct mm_struct *mm;
188 unsigned long i;
189 ssize_t rc = 0;
190 ssize_t bytes_copied_loop;
191 ssize_t bytes_copied = 0;
192 unsigned long nr_pages = 0;
193 unsigned long nr_pages_iov;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700194 ssize_t iov_len;
195
196 /*
197 * Work out how many pages of struct pages we're going to need
198 * when eventually calling get_user_pages
199 */
200 for (i = 0; i < riovcnt; i++) {
201 iov_len = rvec[i].iov_len;
202 if (iov_len > 0) {
203 nr_pages_iov = ((unsigned long)rvec[i].iov_base
204 + iov_len)
205 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
206 / PAGE_SIZE + 1;
207 nr_pages = max(nr_pages, nr_pages_iov);
208 }
209 }
210
211 if (nr_pages == 0)
212 return 0;
213
214 if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
215 /* For reliability don't try to kmalloc more than
216 2 pages worth */
217 process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
218 sizeof(struct pages *)*nr_pages),
219 GFP_KERNEL);
220
221 if (!process_pages)
222 return -ENOMEM;
223 }
224
225 /* Get process information */
226 rcu_read_lock();
227 task = find_task_by_vpid(pid);
228 if (task)
229 get_task_struct(task);
230 rcu_read_unlock();
231 if (!task) {
232 rc = -ESRCH;
233 goto free_proc_pages;
234 }
235
Christopher Yeoh8cdb8782012-02-02 11:34:09 +1030236 mm = mm_access(task, PTRACE_MODE_ATTACH);
237 if (!mm || IS_ERR(mm)) {
238 rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
239 /*
240 * Explicitly map EACCES to EPERM as EPERM is a more a
241 * appropriate error code for process_vw_readv/writev
242 */
243 if (rc == -EACCES)
244 rc = -EPERM;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700245 goto put_task_struct;
246 }
Christopher Yeohfcf63402011-10-31 17:06:39 -0700247
Al Viro240f3902014-02-05 12:14:11 -0500248 for (i = 0; i < riovcnt && iov_iter_count(iter); i++) {
Christopher Yeohfcf63402011-10-31 17:06:39 -0700249 rc = process_vm_rw_single_vec(
250 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
Al Viro9f78bdf2014-02-05 11:51:53 -0500251 iter, process_pages, mm, task, vm_write,
252 &bytes_copied_loop);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700253 bytes_copied += bytes_copied_loop;
254 if (rc != 0) {
255 /* If we have managed to copy any data at all then
256 we return the number of bytes copied. Otherwise
257 we return the error code */
258 if (bytes_copied)
259 rc = bytes_copied;
260 goto put_mm;
261 }
262 }
263
264 rc = bytes_copied;
265put_mm:
266 mmput(mm);
267
268put_task_struct:
269 put_task_struct(task);
270
271free_proc_pages:
272 if (process_pages != pp_stack)
273 kfree(process_pages);
274 return rc;
275}
276
277/**
278 * process_vm_rw - check iovecs before calling core routine
279 * @pid: PID of process to read/write from/to
280 * @lvec: iovec array specifying where to copy to/from locally
281 * @liovcnt: size of lvec array
282 * @rvec: iovec array specifying where to copy to/from in the other process
283 * @riovcnt: size of rvec array
284 * @flags: currently unused
285 * @vm_write: 0 if reading from other process, 1 if writing to other process
286 * Returns the number of bytes read/written or error code. May
287 * return less bytes than expected if an error occurs during the copying
288 * process.
289 */
290static ssize_t process_vm_rw(pid_t pid,
291 const struct iovec __user *lvec,
292 unsigned long liovcnt,
293 const struct iovec __user *rvec,
294 unsigned long riovcnt,
295 unsigned long flags, int vm_write)
296{
297 struct iovec iovstack_l[UIO_FASTIOV];
298 struct iovec iovstack_r[UIO_FASTIOV];
299 struct iovec *iov_l = iovstack_l;
300 struct iovec *iov_r = iovstack_r;
Al Viro9f78bdf2014-02-05 11:51:53 -0500301 struct iov_iter iter;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700302 ssize_t rc;
303
304 if (flags != 0)
305 return -EINVAL;
306
307 /* Check iovecs */
308 if (vm_write)
309 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700310 iovstack_l, &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700311 else
312 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700313 iovstack_l, &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700314 if (rc <= 0)
315 goto free_iovecs;
316
Al Viro9f78bdf2014-02-05 11:51:53 -0500317 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
318
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700319 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
320 iovstack_r, &iov_r);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700321 if (rc <= 0)
322 goto free_iovecs;
323
Al Viro9f78bdf2014-02-05 11:51:53 -0500324 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700325
326free_iovecs:
327 if (iov_r != iovstack_r)
328 kfree(iov_r);
329 if (iov_l != iovstack_l)
330 kfree(iov_l);
331
332 return rc;
333}
334
335SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
336 unsigned long, liovcnt, const struct iovec __user *, rvec,
337 unsigned long, riovcnt, unsigned long, flags)
338{
339 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
340}
341
342SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
343 const struct iovec __user *, lvec,
344 unsigned long, liovcnt, const struct iovec __user *, rvec,
345 unsigned long, riovcnt, unsigned long, flags)
346{
347 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
348}
349
350#ifdef CONFIG_COMPAT
351
352asmlinkage ssize_t
353compat_process_vm_rw(compat_pid_t pid,
354 const struct compat_iovec __user *lvec,
355 unsigned long liovcnt,
356 const struct compat_iovec __user *rvec,
357 unsigned long riovcnt,
358 unsigned long flags, int vm_write)
359{
360 struct iovec iovstack_l[UIO_FASTIOV];
361 struct iovec iovstack_r[UIO_FASTIOV];
362 struct iovec *iov_l = iovstack_l;
363 struct iovec *iov_r = iovstack_r;
Al Viro9f78bdf2014-02-05 11:51:53 -0500364 struct iov_iter iter;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700365 ssize_t rc = -EFAULT;
366
367 if (flags != 0)
368 return -EINVAL;
369
Christopher Yeohfcf63402011-10-31 17:06:39 -0700370 if (vm_write)
371 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
372 UIO_FASTIOV, iovstack_l,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700373 &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700374 else
375 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
376 UIO_FASTIOV, iovstack_l,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700377 &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700378 if (rc <= 0)
379 goto free_iovecs;
Al Viro9f78bdf2014-02-05 11:51:53 -0500380 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700381 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700382 UIO_FASTIOV, iovstack_r,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700383 &iov_r);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700384 if (rc <= 0)
385 goto free_iovecs;
386
Al Viro9f78bdf2014-02-05 11:51:53 -0500387 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700388
389free_iovecs:
390 if (iov_r != iovstack_r)
391 kfree(iov_r);
392 if (iov_l != iovstack_l)
393 kfree(iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700394 return rc;
395}
396
397asmlinkage ssize_t
398compat_sys_process_vm_readv(compat_pid_t pid,
399 const struct compat_iovec __user *lvec,
400 unsigned long liovcnt,
401 const struct compat_iovec __user *rvec,
402 unsigned long riovcnt,
403 unsigned long flags)
404{
405 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
406 riovcnt, flags, 0);
407}
408
409asmlinkage ssize_t
410compat_sys_process_vm_writev(compat_pid_t pid,
411 const struct compat_iovec __user *lvec,
412 unsigned long liovcnt,
413 const struct compat_iovec __user *rvec,
414 unsigned long riovcnt,
415 unsigned long flags)
416{
417 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
418 riovcnt, flags, 1);
419}
420
421#endif