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, | |
e21345f9 | 44 | size_t len, |
9f78bdfa | 45 | struct iov_iter *iter, |
9acc1a0f | 46 | int vm_write) |
fcf63409 | 47 | { |
fcf63409 | 48 | /* Do the copy for each page */ |
9acc1a0f | 49 | while (len && iov_iter_count(iter)) { |
70eca12d | 50 | struct page *page = *pages++; |
e21345f9 | 51 | size_t copy = PAGE_SIZE - offset; |
70eca12d | 52 | size_t copied; |
240f3905 | 53 | |
e21345f9 AV |
54 | if (copy > len) |
55 | copy = len; | |
56 | ||
240f3905 | 57 | if (vm_write) { |
70eca12d AV |
58 | 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); | |
240f3905 AV |
63 | set_page_dirty_lock(page); |
64 | } else { | |
70eca12d | 65 | copied = copy_page_to_iter(page, offset, copy, iter); |
fcf63409 | 66 | } |
70eca12d AV |
67 | len -= copied; |
68 | if (copied < copy && iov_iter_count(iter)) | |
69 | return -EFAULT; | |
70 | offset = 0; | |
fcf63409 | 71 | } |
70eca12d | 72 | return 0; |
fcf63409 CY |
73 | } |
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 | */ | |
94 | static int process_vm_rw_single_vec(unsigned long addr, | |
95 | unsigned long len, | |
9f78bdfa | 96 | struct iov_iter *iter, |
fcf63409 CY |
97 | struct page **process_pages, |
98 | struct mm_struct *mm, | |
99 | struct task_struct *task, | |
9acc1a0f | 100 | int vm_write) |
fcf63409 CY |
101 | { |
102 | unsigned long pa = addr & PAGE_MASK; | |
103 | unsigned long start_offset = addr - pa; | |
104 | unsigned long nr_pages; | |
fcf63409 CY |
105 | ssize_t rc = 0; |
106 | unsigned long nr_pages_copied = 0; | |
fcf63409 CY |
107 | unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES |
108 | / sizeof(struct pages *); | |
109 | ||
fcf63409 CY |
110 | /* 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 | ||
240f3905 | 115 | while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) { |
70eca12d AV |
116 | int nr_pages_to_copy; |
117 | int pages_pinned; | |
e21345f9 | 118 | size_t n; |
fcf63409 CY |
119 | nr_pages_to_copy = min(nr_pages - nr_pages_copied, |
120 | max_pages_per_loop); | |
121 | ||
70eca12d AV |
122 | /* 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 | ||
e21345f9 AV |
132 | n = pages_pinned * PAGE_SIZE - start_offset; |
133 | if (n > len) | |
134 | n = len; | |
135 | ||
70eca12d | 136 | rc = process_vm_rw_pages(process_pages, |
e21345f9 | 137 | start_offset, n, iter, |
9acc1a0f | 138 | vm_write); |
e21345f9 | 139 | len -= n; |
fcf63409 | 140 | start_offset = 0; |
70eca12d AV |
141 | nr_pages_copied += pages_pinned; |
142 | pa += pages_pinned * PAGE_SIZE; | |
143 | while (pages_pinned) | |
144 | put_page(process_pages[--pages_pinned]); | |
70eca12d AV |
145 | if (rc < 0) |
146 | break; | |
fcf63409 CY |
147 | } |
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 | */ | |
9f78bdfa | 169 | static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter, |
fcf63409 CY |
170 | 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; | |
fcf63409 CY |
180 | unsigned long nr_pages = 0; |
181 | unsigned long nr_pages_iov; | |
fcf63409 | 182 | ssize_t iov_len; |
9acc1a0f | 183 | size_t total_len = iov_iter_count(iter); |
fcf63409 CY |
184 | |
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 | ||
8cdb878d CY |
225 | 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; | |
fcf63409 CY |
234 | goto put_task_struct; |
235 | } | |
236 | ||
9acc1a0f | 237 | for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++) |
fcf63409 CY |
238 | rc = process_vm_rw_single_vec( |
239 | (unsigned long)rvec[i].iov_base, rvec[i].iov_len, | |
9acc1a0f AV |
240 | iter, process_pages, mm, task, vm_write); |
241 | ||
242 | /* 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; | |
fcf63409 | 250 | |
fcf63409 CY |
251 | mmput(mm); |
252 | ||
253 | put_task_struct: | |
254 | put_task_struct(task); | |
255 | ||
256 | free_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 | */ | |
275 | static 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; | |
9f78bdfa | 286 | struct iov_iter iter; |
fcf63409 CY |
287 | 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, | |
ac34ebb3 | 295 | iovstack_l, &iov_l); |
fcf63409 CY |
296 | else |
297 | rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV, | |
ac34ebb3 | 298 | iovstack_l, &iov_l); |
fcf63409 CY |
299 | if (rc <= 0) |
300 | goto free_iovecs; | |
301 | ||
9f78bdfa AV |
302 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
303 | ||
ac34ebb3 CY |
304 | rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, |
305 | iovstack_r, &iov_r); | |
fcf63409 CY |
306 | if (rc <= 0) |
307 | goto free_iovecs; | |
308 | ||
9f78bdfa | 309 | rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); |
fcf63409 CY |
310 | |
311 | free_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 | ||
320 | SYSCALL_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 | ||
327 | SYSCALL_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 | ||
337 | asmlinkage ssize_t | |
338 | compat_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; | |
9f78bdfa | 349 | struct iov_iter iter; |
fcf63409 CY |
350 | ssize_t rc = -EFAULT; |
351 | ||
352 | if (flags != 0) | |
353 | return -EINVAL; | |
354 | ||
fcf63409 CY |
355 | if (vm_write) |
356 | rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt, | |
357 | UIO_FASTIOV, iovstack_l, | |
ac34ebb3 | 358 | &iov_l); |
fcf63409 CY |
359 | else |
360 | rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt, | |
361 | UIO_FASTIOV, iovstack_l, | |
ac34ebb3 | 362 | &iov_l); |
fcf63409 CY |
363 | if (rc <= 0) |
364 | goto free_iovecs; | |
9f78bdfa | 365 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
ac34ebb3 | 366 | rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, |
fcf63409 | 367 | UIO_FASTIOV, iovstack_r, |
ac34ebb3 | 368 | &iov_r); |
fcf63409 CY |
369 | if (rc <= 0) |
370 | goto free_iovecs; | |
371 | ||
9f78bdfa | 372 | rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); |
fcf63409 CY |
373 | |
374 | free_iovecs: | |
375 | if (iov_r != iovstack_r) | |
376 | kfree(iov_r); | |
377 | if (iov_l != iovstack_l) | |
378 | kfree(iov_l); | |
fcf63409 CY |
379 | return rc; |
380 | } | |
381 | ||
382 | asmlinkage ssize_t | |
383 | compat_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 | ||
394 | asmlinkage ssize_t | |
395 | compat_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 |