Commit | Line | Data |
---|---|---|
fcf63409 CY |
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 | ||
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 | */ | |
70eca12d AV |
42 | static int process_vm_rw_pages(struct page **pages, |
43 | unsigned offset, | |
fcf63409 | 44 | unsigned long len, |
9f78bdfa | 45 | struct iov_iter *iter, |
fcf63409 CY |
46 | int vm_write, |
47 | unsigned int nr_pages_to_copy, | |
48 | ssize_t *bytes_copied) | |
49 | { | |
fcf63409 CY |
50 | *bytes_copied = 0; |
51 | ||
fcf63409 | 52 | /* Do the copy for each page */ |
70eca12d AV |
53 | 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; | |
240f3905 AV |
57 | |
58 | if (vm_write) { | |
70eca12d AV |
59 | 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); | |
240f3905 AV |
64 | set_page_dirty_lock(page); |
65 | } else { | |
70eca12d | 66 | copied = copy_page_to_iter(page, offset, copy, iter); |
fcf63409 | 67 | } |
70eca12d AV |
68 | *bytes_copied += copied; |
69 | len -= copied; | |
70 | if (copied < copy && iov_iter_count(iter)) | |
71 | return -EFAULT; | |
72 | offset = 0; | |
fcf63409 | 73 | } |
70eca12d | 74 | return 0; |
fcf63409 CY |
75 | } |
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 | */ | |
96 | static int process_vm_rw_single_vec(unsigned long addr, | |
97 | unsigned long len, | |
9f78bdfa | 98 | struct iov_iter *iter, |
fcf63409 CY |
99 | 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; | |
fcf63409 CY |
111 | 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 | ||
240f3905 | 121 | while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) { |
70eca12d AV |
122 | int nr_pages_to_copy; |
123 | int pages_pinned; | |
fcf63409 CY |
124 | nr_pages_to_copy = min(nr_pages - nr_pages_copied, |
125 | max_pages_per_loop); | |
126 | ||
70eca12d AV |
127 | /* 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, | |
9f78bdfa | 138 | start_offset, len, iter, |
70eca12d | 139 | vm_write, pages_pinned, |
fcf63409 CY |
140 | &bytes_copied_loop); |
141 | start_offset = 0; | |
142 | *bytes_copied += bytes_copied_loop; | |
70eca12d AV |
143 | 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]); | |
fcf63409 | 148 | |
70eca12d AV |
149 | if (rc < 0) |
150 | break; | |
fcf63409 CY |
151 | } |
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 | */ | |
9f78bdfa | 173 | static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter, |
fcf63409 CY |
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; | |
fcf63409 CY |
188 | 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 | ||
8cdb878d CY |
230 | 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; | |
fcf63409 CY |
239 | goto put_task_struct; |
240 | } | |
241 | ||
240f3905 | 242 | for (i = 0; i < riovcnt && iov_iter_count(iter); i++) { |
fcf63409 CY |
243 | rc = process_vm_rw_single_vec( |
244 | (unsigned long)rvec[i].iov_base, rvec[i].iov_len, | |
9f78bdfa AV |
245 | iter, process_pages, mm, task, vm_write, |
246 | &bytes_copied_loop); | |
fcf63409 CY |
247 | 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; | |
259 | put_mm: | |
260 | mmput(mm); | |
261 | ||
262 | put_task_struct: | |
263 | put_task_struct(task); | |
264 | ||
265 | free_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 | */ | |
284 | static 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; | |
9f78bdfa | 295 | struct iov_iter iter; |
fcf63409 CY |
296 | 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, | |
ac34ebb3 | 304 | iovstack_l, &iov_l); |
fcf63409 CY |
305 | else |
306 | rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV, | |
ac34ebb3 | 307 | iovstack_l, &iov_l); |
fcf63409 CY |
308 | if (rc <= 0) |
309 | goto free_iovecs; | |
310 | ||
9f78bdfa AV |
311 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
312 | ||
ac34ebb3 CY |
313 | rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, |
314 | iovstack_r, &iov_r); | |
fcf63409 CY |
315 | if (rc <= 0) |
316 | goto free_iovecs; | |
317 | ||
9f78bdfa | 318 | rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); |
fcf63409 CY |
319 | |
320 | free_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 | ||
329 | SYSCALL_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 | ||
336 | SYSCALL_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 | ||
346 | asmlinkage ssize_t | |
347 | compat_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; | |
9f78bdfa | 358 | struct iov_iter iter; |
fcf63409 CY |
359 | ssize_t rc = -EFAULT; |
360 | ||
361 | if (flags != 0) | |
362 | return -EINVAL; | |
363 | ||
fcf63409 CY |
364 | if (vm_write) |
365 | rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt, | |
366 | UIO_FASTIOV, iovstack_l, | |
ac34ebb3 | 367 | &iov_l); |
fcf63409 CY |
368 | else |
369 | rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt, | |
370 | UIO_FASTIOV, iovstack_l, | |
ac34ebb3 | 371 | &iov_l); |
fcf63409 CY |
372 | if (rc <= 0) |
373 | goto free_iovecs; | |
9f78bdfa | 374 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
ac34ebb3 | 375 | rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, |
fcf63409 | 376 | UIO_FASTIOV, iovstack_r, |
ac34ebb3 | 377 | &iov_r); |
fcf63409 CY |
378 | if (rc <= 0) |
379 | goto free_iovecs; | |
380 | ||
9f78bdfa | 381 | rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); |
fcf63409 CY |
382 | |
383 | free_iovecs: | |
384 | if (iov_r != iovstack_r) | |
385 | kfree(iov_r); | |
386 | if (iov_l != iovstack_l) | |
387 | kfree(iov_l); | |
fcf63409 CY |
388 | return rc; |
389 | } | |
390 | ||
391 | asmlinkage ssize_t | |
392 | compat_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 | ||
403 | asmlinkage ssize_t | |
404 | compat_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 |