blob: c2a1916bacbf079725c24f657876ae158ba1b9a1 [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,
Christopher Yeohfcf63402011-10-31 17:06:39 -070044 unsigned long len,
Al Viro9f78bdf2014-02-05 11:51:53 -050045 struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -070046 int vm_write,
47 unsigned int nr_pages_to_copy,
48 ssize_t *bytes_copied)
49{
Christopher Yeohfcf63402011-10-31 17:06:39 -070050 *bytes_copied = 0;
51
Christopher Yeohfcf63402011-10-31 17:06:39 -070052 /* Do the copy for each page */
Al Viro70eca122014-02-05 12:44:24 -050053 while (iov_iter_count(iter) && nr_pages_to_copy--) {
54 struct page *page = *pages++;
55 size_t copy = min_t(ssize_t, PAGE_SIZE - offset, len);
56 size_t copied;
Christopher Yeohfcf63402011-10-31 17:06:39 -070057
Al Viro240f3902014-02-05 12:14:11 -050058 if (vm_write) {
Al Viro70eca122014-02-05 12:44:24 -050059 if (copy > iov_iter_count(iter))
60 copy = iov_iter_count(iter);
61 copied = iov_iter_copy_from_user(page, iter,
62 offset, copy);
63 iov_iter_advance(iter, copied);
Al Viro240f3902014-02-05 12:14:11 -050064 set_page_dirty_lock(page);
Christopher Yeohfcf63402011-10-31 17:06:39 -070065 } else {
Al Viro70eca122014-02-05 12:44:24 -050066 copied = copy_page_to_iter(page, offset, copy, iter);
Christopher Yeohfcf63402011-10-31 17:06:39 -070067 }
Al Viro70eca122014-02-05 12:44:24 -050068 *bytes_copied += copied;
69 len -= copied;
70 if (copied < copy && iov_iter_count(iter))
71 return -EFAULT;
72 offset = 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -070073 }
Al Viro70eca122014-02-05 12:44:24 -050074 return 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -070075}
76
77/* Maximum number of pages kmalloc'd to hold struct page's during copy */
78#define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
79
80/**
81 * process_vm_rw_single_vec - read/write pages from task specified
82 * @addr: start memory address of target process
83 * @len: size of area to copy to/from
84 * @lvec: iovec array specifying where to copy to/from locally
85 * @lvec_cnt: number of elements in iovec array
86 * @lvec_current: index in iovec array we are up to
87 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
88 * @process_pages: struct pages area that can store at least
89 * nr_pages_to_copy struct page pointers
90 * @mm: mm for task
91 * @task: task to read/write from
92 * @vm_write: 0 means copy from, 1 means copy to
93 * @bytes_copied: returns number of bytes successfully copied
94 * Returns 0 on success or on failure error code
95 */
96static int process_vm_rw_single_vec(unsigned long addr,
97 unsigned long len,
Al Viro9f78bdf2014-02-05 11:51:53 -050098 struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -070099 struct page **process_pages,
100 struct mm_struct *mm,
101 struct task_struct *task,
102 int vm_write,
103 ssize_t *bytes_copied)
104{
105 unsigned long pa = addr & PAGE_MASK;
106 unsigned long start_offset = addr - pa;
107 unsigned long nr_pages;
108 ssize_t bytes_copied_loop;
109 ssize_t rc = 0;
110 unsigned long nr_pages_copied = 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700111 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
112 / sizeof(struct pages *);
113
114 *bytes_copied = 0;
115
116 /* Work out address and page range required */
117 if (len == 0)
118 return 0;
119 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
120
Al Viro240f3902014-02-05 12:14:11 -0500121 while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) {
Al Viro70eca122014-02-05 12:44:24 -0500122 int nr_pages_to_copy;
123 int pages_pinned;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700124 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
125 max_pages_per_loop);
126
Al Viro70eca122014-02-05 12:44:24 -0500127 /* Get the pages we're interested in */
128 down_read(&mm->mmap_sem);
129 pages_pinned = get_user_pages(task, mm, pa,
130 nr_pages_to_copy,
131 vm_write, 0, process_pages, NULL);
132 up_read(&mm->mmap_sem);
133
134 if (pages_pinned <= 0)
135 return -EFAULT;
136
137 rc = process_vm_rw_pages(process_pages,
Al Viro9f78bdf2014-02-05 11:51:53 -0500138 start_offset, len, iter,
Al Viro70eca122014-02-05 12:44:24 -0500139 vm_write, pages_pinned,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700140 &bytes_copied_loop);
141 start_offset = 0;
142 *bytes_copied += bytes_copied_loop;
Al Viro70eca122014-02-05 12:44:24 -0500143 len -= bytes_copied_loop;
144 nr_pages_copied += pages_pinned;
145 pa += pages_pinned * PAGE_SIZE;
146 while (pages_pinned)
147 put_page(process_pages[--pages_pinned]);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700148
Al Viro70eca122014-02-05 12:44:24 -0500149 if (rc < 0)
150 break;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700151 }
152
153 return rc;
154}
155
156/* Maximum number of entries for process pages array
157 which lives on stack */
158#define PVM_MAX_PP_ARRAY_COUNT 16
159
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 */
Al Viro9f78bdf2014-02-05 11:51:53 -0500173static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700174 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;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700188 ssize_t iov_len;
189
190 /*
191 * Work out how many pages of struct pages we're going to need
192 * when eventually calling get_user_pages
193 */
194 for (i = 0; i < riovcnt; i++) {
195 iov_len = rvec[i].iov_len;
196 if (iov_len > 0) {
197 nr_pages_iov = ((unsigned long)rvec[i].iov_base
198 + iov_len)
199 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
200 / PAGE_SIZE + 1;
201 nr_pages = max(nr_pages, nr_pages_iov);
202 }
203 }
204
205 if (nr_pages == 0)
206 return 0;
207
208 if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
209 /* For reliability don't try to kmalloc more than
210 2 pages worth */
211 process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
212 sizeof(struct pages *)*nr_pages),
213 GFP_KERNEL);
214
215 if (!process_pages)
216 return -ENOMEM;
217 }
218
219 /* Get process information */
220 rcu_read_lock();
221 task = find_task_by_vpid(pid);
222 if (task)
223 get_task_struct(task);
224 rcu_read_unlock();
225 if (!task) {
226 rc = -ESRCH;
227 goto free_proc_pages;
228 }
229
Christopher Yeoh8cdb8782012-02-02 11:34:09 +1030230 mm = mm_access(task, PTRACE_MODE_ATTACH);
231 if (!mm || IS_ERR(mm)) {
232 rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
233 /*
234 * Explicitly map EACCES to EPERM as EPERM is a more a
235 * appropriate error code for process_vw_readv/writev
236 */
237 if (rc == -EACCES)
238 rc = -EPERM;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700239 goto put_task_struct;
240 }
Christopher Yeohfcf63402011-10-31 17:06:39 -0700241
Al Viro240f3902014-02-05 12:14:11 -0500242 for (i = 0; i < riovcnt && iov_iter_count(iter); i++) {
Christopher Yeohfcf63402011-10-31 17:06:39 -0700243 rc = process_vm_rw_single_vec(
244 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
Al Viro9f78bdf2014-02-05 11:51:53 -0500245 iter, process_pages, mm, task, vm_write,
246 &bytes_copied_loop);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700247 bytes_copied += bytes_copied_loop;
248 if (rc != 0) {
249 /* If we have managed to copy any data at all then
250 we return the number of bytes copied. Otherwise
251 we return the error code */
252 if (bytes_copied)
253 rc = bytes_copied;
254 goto put_mm;
255 }
256 }
257
258 rc = bytes_copied;
259put_mm:
260 mmput(mm);
261
262put_task_struct:
263 put_task_struct(task);
264
265free_proc_pages:
266 if (process_pages != pp_stack)
267 kfree(process_pages);
268 return rc;
269}
270
271/**
272 * process_vm_rw - check iovecs before calling core routine
273 * @pid: PID of process to read/write from/to
274 * @lvec: iovec array specifying where to copy to/from locally
275 * @liovcnt: size of lvec array
276 * @rvec: iovec array specifying where to copy to/from in the other process
277 * @riovcnt: size of rvec array
278 * @flags: currently unused
279 * @vm_write: 0 if reading from other process, 1 if writing to other process
280 * Returns the number of bytes read/written or error code. May
281 * return less bytes than expected if an error occurs during the copying
282 * process.
283 */
284static ssize_t process_vm_rw(pid_t pid,
285 const struct iovec __user *lvec,
286 unsigned long liovcnt,
287 const struct iovec __user *rvec,
288 unsigned long riovcnt,
289 unsigned long flags, int vm_write)
290{
291 struct iovec iovstack_l[UIO_FASTIOV];
292 struct iovec iovstack_r[UIO_FASTIOV];
293 struct iovec *iov_l = iovstack_l;
294 struct iovec *iov_r = iovstack_r;
Al Viro9f78bdf2014-02-05 11:51:53 -0500295 struct iov_iter iter;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700296 ssize_t rc;
297
298 if (flags != 0)
299 return -EINVAL;
300
301 /* Check iovecs */
302 if (vm_write)
303 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700304 iovstack_l, &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700305 else
306 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700307 iovstack_l, &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700308 if (rc <= 0)
309 goto free_iovecs;
310
Al Viro9f78bdf2014-02-05 11:51:53 -0500311 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
312
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700313 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
314 iovstack_r, &iov_r);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700315 if (rc <= 0)
316 goto free_iovecs;
317
Al Viro9f78bdf2014-02-05 11:51:53 -0500318 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700319
320free_iovecs:
321 if (iov_r != iovstack_r)
322 kfree(iov_r);
323 if (iov_l != iovstack_l)
324 kfree(iov_l);
325
326 return rc;
327}
328
329SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
330 unsigned long, liovcnt, const struct iovec __user *, rvec,
331 unsigned long, riovcnt, unsigned long, flags)
332{
333 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
334}
335
336SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
337 const struct iovec __user *, lvec,
338 unsigned long, liovcnt, const struct iovec __user *, rvec,
339 unsigned long, riovcnt, unsigned long, flags)
340{
341 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
342}
343
344#ifdef CONFIG_COMPAT
345
346asmlinkage ssize_t
347compat_process_vm_rw(compat_pid_t pid,
348 const struct compat_iovec __user *lvec,
349 unsigned long liovcnt,
350 const struct compat_iovec __user *rvec,
351 unsigned long riovcnt,
352 unsigned long flags, int vm_write)
353{
354 struct iovec iovstack_l[UIO_FASTIOV];
355 struct iovec iovstack_r[UIO_FASTIOV];
356 struct iovec *iov_l = iovstack_l;
357 struct iovec *iov_r = iovstack_r;
Al Viro9f78bdf2014-02-05 11:51:53 -0500358 struct iov_iter iter;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700359 ssize_t rc = -EFAULT;
360
361 if (flags != 0)
362 return -EINVAL;
363
Christopher Yeohfcf63402011-10-31 17:06:39 -0700364 if (vm_write)
365 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
366 UIO_FASTIOV, iovstack_l,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700367 &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700368 else
369 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
370 UIO_FASTIOV, iovstack_l,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700371 &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700372 if (rc <= 0)
373 goto free_iovecs;
Al Viro9f78bdf2014-02-05 11:51:53 -0500374 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700375 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700376 UIO_FASTIOV, iovstack_r,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700377 &iov_r);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700378 if (rc <= 0)
379 goto free_iovecs;
380
Al Viro9f78bdf2014-02-05 11:51:53 -0500381 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700382
383free_iovecs:
384 if (iov_r != iovstack_r)
385 kfree(iov_r);
386 if (iov_l != iovstack_l)
387 kfree(iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700388 return rc;
389}
390
391asmlinkage ssize_t
392compat_sys_process_vm_readv(compat_pid_t pid,
393 const struct compat_iovec __user *lvec,
394 unsigned long liovcnt,
395 const struct compat_iovec __user *rvec,
396 unsigned long riovcnt,
397 unsigned long flags)
398{
399 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
400 riovcnt, flags, 0);
401}
402
403asmlinkage ssize_t
404compat_sys_process_vm_writev(compat_pid_t pid,
405 const struct compat_iovec __user *lvec,
406 unsigned long liovcnt,
407 const struct compat_iovec __user *rvec,
408 unsigned long riovcnt,
409 unsigned long flags)
410{
411 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
412 riovcnt, flags, 1);
413}
414
415#endif