blob: 15e4c2f312e8388d6c5895c9a8d593b0e731c293 [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,
Al Viro9acc1a02014-02-05 13:15:28 -050046 int vm_write)
Christopher Yeohfcf63402011-10-31 17:06:39 -070047{
Christopher Yeohfcf63402011-10-31 17:06:39 -070048 /* Do the copy for each page */
Al Viro9acc1a02014-02-05 13:15:28 -050049 while (len && iov_iter_count(iter)) {
Al Viro70eca122014-02-05 12:44:24 -050050 struct page *page = *pages++;
Al Viroe21345f2014-02-05 12:55:11 -050051 size_t copy = PAGE_SIZE - offset;
Al Viro70eca122014-02-05 12:44:24 -050052 size_t copied;
Christopher Yeohfcf63402011-10-31 17:06:39 -070053
Al Viroe21345f2014-02-05 12:55:11 -050054 if (copy > len)
55 copy = len;
56
Al Viro240f3902014-02-05 12:14:11 -050057 if (vm_write) {
Al Viro70eca122014-02-05 12:44:24 -050058 if (copy > iov_iter_count(iter))
59 copy = iov_iter_count(iter);
60 copied = iov_iter_copy_from_user(page, iter,
61 offset, copy);
62 iov_iter_advance(iter, copied);
Al Viro240f3902014-02-05 12:14:11 -050063 set_page_dirty_lock(page);
Christopher Yeohfcf63402011-10-31 17:06:39 -070064 } else {
Al Viro70eca122014-02-05 12:44:24 -050065 copied = copy_page_to_iter(page, offset, copy, iter);
Christopher Yeohfcf63402011-10-31 17:06:39 -070066 }
Al Viro70eca122014-02-05 12:44:24 -050067 len -= copied;
68 if (copied < copy && iov_iter_count(iter))
69 return -EFAULT;
70 offset = 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -070071 }
Al Viro70eca122014-02-05 12:44:24 -050072 return 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -070073}
74
75/* Maximum number of pages kmalloc'd to hold struct page's during copy */
76#define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
77
78/**
79 * process_vm_rw_single_vec - read/write pages from task specified
80 * @addr: start memory address of target process
81 * @len: size of area to copy to/from
82 * @lvec: iovec array specifying where to copy to/from locally
83 * @lvec_cnt: number of elements in iovec array
84 * @lvec_current: index in iovec array we are up to
85 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
86 * @process_pages: struct pages area that can store at least
87 * nr_pages_to_copy struct page pointers
88 * @mm: mm for task
89 * @task: task to read/write from
90 * @vm_write: 0 means copy from, 1 means copy to
91 * @bytes_copied: returns number of bytes successfully copied
92 * Returns 0 on success or on failure error code
93 */
94static int process_vm_rw_single_vec(unsigned long addr,
95 unsigned long len,
Al Viro9f78bdf2014-02-05 11:51:53 -050096 struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -070097 struct page **process_pages,
98 struct mm_struct *mm,
99 struct task_struct *task,
Al Viro9acc1a02014-02-05 13:15:28 -0500100 int vm_write)
Christopher Yeohfcf63402011-10-31 17:06:39 -0700101{
102 unsigned long pa = addr & PAGE_MASK;
103 unsigned long start_offset = addr - pa;
104 unsigned long nr_pages;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700105 ssize_t rc = 0;
106 unsigned long nr_pages_copied = 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700107 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
108 / sizeof(struct pages *);
109
Christopher Yeohfcf63402011-10-31 17:06:39 -0700110 /* Work out address and page range required */
111 if (len == 0)
112 return 0;
113 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
114
Al Viro240f3902014-02-05 12:14:11 -0500115 while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) {
Al Viro70eca122014-02-05 12:44:24 -0500116 int nr_pages_to_copy;
117 int pages_pinned;
Al Viroe21345f2014-02-05 12:55:11 -0500118 size_t n;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700119 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
120 max_pages_per_loop);
121
Al Viro70eca122014-02-05 12:44:24 -0500122 /* Get the pages we're interested in */
123 down_read(&mm->mmap_sem);
124 pages_pinned = get_user_pages(task, mm, pa,
125 nr_pages_to_copy,
126 vm_write, 0, process_pages, NULL);
127 up_read(&mm->mmap_sem);
128
129 if (pages_pinned <= 0)
130 return -EFAULT;
131
Al Viroe21345f2014-02-05 12:55:11 -0500132 n = pages_pinned * PAGE_SIZE - start_offset;
133 if (n > len)
134 n = len;
135
Al Viro70eca122014-02-05 12:44:24 -0500136 rc = process_vm_rw_pages(process_pages,
Al Viroe21345f2014-02-05 12:55:11 -0500137 start_offset, n, iter,
Al Viro9acc1a02014-02-05 13:15:28 -0500138 vm_write);
Al Viroe21345f2014-02-05 12:55:11 -0500139 len -= n;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700140 start_offset = 0;
Al Viro70eca122014-02-05 12:44:24 -0500141 nr_pages_copied += pages_pinned;
142 pa += pages_pinned * PAGE_SIZE;
143 while (pages_pinned)
144 put_page(process_pages[--pages_pinned]);
Al Viro70eca122014-02-05 12:44:24 -0500145 if (rc < 0)
146 break;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700147 }
148
149 return rc;
150}
151
152/* Maximum number of entries for process pages array
153 which lives on stack */
154#define PVM_MAX_PP_ARRAY_COUNT 16
155
156/**
157 * process_vm_rw_core - core of reading/writing pages from task specified
158 * @pid: PID of process to read/write from/to
159 * @lvec: iovec array specifying where to copy to/from locally
160 * @liovcnt: size of lvec array
161 * @rvec: iovec array specifying where to copy to/from in the other process
162 * @riovcnt: size of rvec array
163 * @flags: currently unused
164 * @vm_write: 0 if reading from other process, 1 if writing to other process
165 * Returns the number of bytes read/written or error code. May
166 * return less bytes than expected if an error occurs during the copying
167 * process.
168 */
Al Viro9f78bdf2014-02-05 11:51:53 -0500169static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700170 const struct iovec *rvec,
171 unsigned long riovcnt,
172 unsigned long flags, int vm_write)
173{
174 struct task_struct *task;
175 struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
176 struct page **process_pages = pp_stack;
177 struct mm_struct *mm;
178 unsigned long i;
179 ssize_t rc = 0;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700180 unsigned long nr_pages = 0;
181 unsigned long nr_pages_iov;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700182 ssize_t iov_len;
Al Viro9acc1a02014-02-05 13:15:28 -0500183 size_t total_len = iov_iter_count(iter);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700184
185 /*
186 * Work out how many pages of struct pages we're going to need
187 * when eventually calling get_user_pages
188 */
189 for (i = 0; i < riovcnt; i++) {
190 iov_len = rvec[i].iov_len;
191 if (iov_len > 0) {
192 nr_pages_iov = ((unsigned long)rvec[i].iov_base
193 + iov_len)
194 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
195 / PAGE_SIZE + 1;
196 nr_pages = max(nr_pages, nr_pages_iov);
197 }
198 }
199
200 if (nr_pages == 0)
201 return 0;
202
203 if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
204 /* For reliability don't try to kmalloc more than
205 2 pages worth */
206 process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
207 sizeof(struct pages *)*nr_pages),
208 GFP_KERNEL);
209
210 if (!process_pages)
211 return -ENOMEM;
212 }
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
Christopher Yeoh8cdb8782012-02-02 11:34:09 +1030225 mm = mm_access(task, PTRACE_MODE_ATTACH);
226 if (!mm || IS_ERR(mm)) {
227 rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
228 /*
229 * Explicitly map EACCES to EPERM as EPERM is a more a
230 * appropriate error code for process_vw_readv/writev
231 */
232 if (rc == -EACCES)
233 rc = -EPERM;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700234 goto put_task_struct;
235 }
Christopher Yeohfcf63402011-10-31 17:06:39 -0700236
Al Viro9acc1a02014-02-05 13:15:28 -0500237 for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++)
Christopher Yeohfcf63402011-10-31 17:06:39 -0700238 rc = process_vm_rw_single_vec(
239 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
Al Viro9acc1a02014-02-05 13:15:28 -0500240 iter, process_pages, mm, task, vm_write);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700241
Al Viro9acc1a02014-02-05 13:15:28 -0500242 /* copied = space before - space after */
243 total_len -= iov_iter_count(iter);
244
245 /* If we have managed to copy any data at all then
246 we return the number of bytes copied. Otherwise
247 we return the error code */
248 if (total_len)
249 rc = total_len;
250
Christopher Yeohfcf63402011-10-31 17:06:39 -0700251 mmput(mm);
252
253put_task_struct:
254 put_task_struct(task);
255
256free_proc_pages:
257 if (process_pages != pp_stack)
258 kfree(process_pages);
259 return rc;
260}
261
262/**
263 * process_vm_rw - check iovecs before calling core routine
264 * @pid: PID of process to read/write from/to
265 * @lvec: iovec array specifying where to copy to/from locally
266 * @liovcnt: size of lvec array
267 * @rvec: iovec array specifying where to copy to/from in the other process
268 * @riovcnt: size of rvec array
269 * @flags: currently unused
270 * @vm_write: 0 if reading from other process, 1 if writing to other process
271 * Returns the number of bytes read/written or error code. May
272 * return less bytes than expected if an error occurs during the copying
273 * process.
274 */
275static ssize_t process_vm_rw(pid_t pid,
276 const struct iovec __user *lvec,
277 unsigned long liovcnt,
278 const struct iovec __user *rvec,
279 unsigned long riovcnt,
280 unsigned long flags, int vm_write)
281{
282 struct iovec iovstack_l[UIO_FASTIOV];
283 struct iovec iovstack_r[UIO_FASTIOV];
284 struct iovec *iov_l = iovstack_l;
285 struct iovec *iov_r = iovstack_r;
Al Viro9f78bdf2014-02-05 11:51:53 -0500286 struct iov_iter iter;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700287 ssize_t rc;
288
289 if (flags != 0)
290 return -EINVAL;
291
292 /* Check iovecs */
293 if (vm_write)
294 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700295 iovstack_l, &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700296 else
297 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700298 iovstack_l, &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700299 if (rc <= 0)
300 goto free_iovecs;
301
Al Viro9f78bdf2014-02-05 11:51:53 -0500302 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
303
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700304 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
305 iovstack_r, &iov_r);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700306 if (rc <= 0)
307 goto free_iovecs;
308
Al Viro9f78bdf2014-02-05 11:51:53 -0500309 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700310
311free_iovecs:
312 if (iov_r != iovstack_r)
313 kfree(iov_r);
314 if (iov_l != iovstack_l)
315 kfree(iov_l);
316
317 return rc;
318}
319
320SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
321 unsigned long, liovcnt, const struct iovec __user *, rvec,
322 unsigned long, riovcnt, unsigned long, flags)
323{
324 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
325}
326
327SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
328 const struct iovec __user *, lvec,
329 unsigned long, liovcnt, const struct iovec __user *, rvec,
330 unsigned long, riovcnt, unsigned long, flags)
331{
332 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
333}
334
335#ifdef CONFIG_COMPAT
336
337asmlinkage ssize_t
338compat_process_vm_rw(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, int vm_write)
344{
345 struct iovec iovstack_l[UIO_FASTIOV];
346 struct iovec iovstack_r[UIO_FASTIOV];
347 struct iovec *iov_l = iovstack_l;
348 struct iovec *iov_r = iovstack_r;
Al Viro9f78bdf2014-02-05 11:51:53 -0500349 struct iov_iter iter;
Christopher Yeohfcf63402011-10-31 17:06:39 -0700350 ssize_t rc = -EFAULT;
351
352 if (flags != 0)
353 return -EINVAL;
354
Christopher Yeohfcf63402011-10-31 17:06:39 -0700355 if (vm_write)
356 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
357 UIO_FASTIOV, iovstack_l,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700358 &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700359 else
360 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
361 UIO_FASTIOV, iovstack_l,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700362 &iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700363 if (rc <= 0)
364 goto free_iovecs;
Al Viro9f78bdf2014-02-05 11:51:53 -0500365 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700366 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
Christopher Yeohfcf63402011-10-31 17:06:39 -0700367 UIO_FASTIOV, iovstack_r,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700368 &iov_r);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700369 if (rc <= 0)
370 goto free_iovecs;
371
Al Viro9f78bdf2014-02-05 11:51:53 -0500372 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700373
374free_iovecs:
375 if (iov_r != iovstack_r)
376 kfree(iov_r);
377 if (iov_l != iovstack_l)
378 kfree(iov_l);
Christopher Yeohfcf63402011-10-31 17:06:39 -0700379 return rc;
380}
381
382asmlinkage ssize_t
383compat_sys_process_vm_readv(compat_pid_t pid,
384 const struct compat_iovec __user *lvec,
385 unsigned long liovcnt,
386 const struct compat_iovec __user *rvec,
387 unsigned long riovcnt,
388 unsigned long flags)
389{
390 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
391 riovcnt, flags, 0);
392}
393
394asmlinkage ssize_t
395compat_sys_process_vm_writev(compat_pid_t pid,
396 const struct compat_iovec __user *lvec,
397 unsigned long liovcnt,
398 const struct compat_iovec __user *rvec,
399 unsigned long riovcnt,
400 unsigned long flags)
401{
402 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
403 riovcnt, flags, 1);
404}
405
406#endif