Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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 | */ |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 42 | static int process_vm_rw_pages(struct page **pages, |
| 43 | unsigned offset, |
Al Viro | e21345f | 2014-02-05 12:55:11 -0500 | [diff] [blame] | 44 | size_t len, |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 45 | struct iov_iter *iter, |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 46 | int vm_write) |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 47 | { |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 48 | /* Do the copy for each page */ |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 49 | while (len && iov_iter_count(iter)) { |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 50 | struct page *page = *pages++; |
Al Viro | e21345f | 2014-02-05 12:55:11 -0500 | [diff] [blame] | 51 | size_t copy = PAGE_SIZE - offset; |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 52 | size_t copied; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 53 | |
Al Viro | e21345f | 2014-02-05 12:55:11 -0500 | [diff] [blame] | 54 | if (copy > len) |
| 55 | copy = len; |
| 56 | |
Al Viro | 240f390 | 2014-02-05 12:14:11 -0500 | [diff] [blame] | 57 | if (vm_write) { |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 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); |
Al Viro | 240f390 | 2014-02-05 12:14:11 -0500 | [diff] [blame] | 63 | set_page_dirty_lock(page); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 64 | } else { |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 65 | copied = copy_page_to_iter(page, offset, copy, iter); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 66 | } |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 67 | len -= copied; |
| 68 | if (copied < copy && iov_iter_count(iter)) |
| 69 | return -EFAULT; |
| 70 | offset = 0; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 71 | } |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 72 | return 0; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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, |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 96 | struct iov_iter *iter, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 97 | struct page **process_pages, |
| 98 | struct mm_struct *mm, |
| 99 | struct task_struct *task, |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 100 | int vm_write) |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 101 | { |
| 102 | unsigned long pa = addr & PAGE_MASK; |
| 103 | unsigned long start_offset = addr - pa; |
| 104 | unsigned long nr_pages; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 105 | ssize_t rc = 0; |
| 106 | unsigned long nr_pages_copied = 0; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 107 | unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES |
| 108 | / sizeof(struct pages *); |
| 109 | |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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 | |
Al Viro | 240f390 | 2014-02-05 12:14:11 -0500 | [diff] [blame] | 115 | while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) { |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 116 | int nr_pages_to_copy; |
| 117 | int pages_pinned; |
Al Viro | e21345f | 2014-02-05 12:55:11 -0500 | [diff] [blame] | 118 | size_t n; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 119 | nr_pages_to_copy = min(nr_pages - nr_pages_copied, |
| 120 | max_pages_per_loop); |
| 121 | |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 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 | |
Al Viro | e21345f | 2014-02-05 12:55:11 -0500 | [diff] [blame] | 132 | n = pages_pinned * PAGE_SIZE - start_offset; |
| 133 | if (n > len) |
| 134 | n = len; |
| 135 | |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 136 | rc = process_vm_rw_pages(process_pages, |
Al Viro | e21345f | 2014-02-05 12:55:11 -0500 | [diff] [blame] | 137 | start_offset, n, iter, |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 138 | vm_write); |
Al Viro | e21345f | 2014-02-05 12:55:11 -0500 | [diff] [blame] | 139 | len -= n; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 140 | start_offset = 0; |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 141 | nr_pages_copied += pages_pinned; |
| 142 | pa += pages_pinned * PAGE_SIZE; |
| 143 | while (pages_pinned) |
| 144 | put_page(process_pages[--pages_pinned]); |
Al Viro | 70eca12 | 2014-02-05 12:44:24 -0500 | [diff] [blame] | 145 | if (rc < 0) |
| 146 | break; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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 | */ |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 169 | static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 180 | unsigned long nr_pages = 0; |
| 181 | unsigned long nr_pages_iov; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 182 | ssize_t iov_len; |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 183 | size_t total_len = iov_iter_count(iter); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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 | |
Christopher Yeoh | 8cdb878 | 2012-02-02 11:34:09 +1030 | [diff] [blame] | 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; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 234 | goto put_task_struct; |
| 235 | } |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 236 | |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 237 | for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++) |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 238 | rc = process_vm_rw_single_vec( |
| 239 | (unsigned long)rvec[i].iov_base, rvec[i].iov_len, |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 240 | iter, process_pages, mm, task, vm_write); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 241 | |
Al Viro | 9acc1a0 | 2014-02-05 13:15:28 -0500 | [diff] [blame^] | 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; |
| 250 | |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 286 | struct iov_iter iter; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 295 | iovstack_l, &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 296 | else |
| 297 | rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 298 | iovstack_l, &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 299 | if (rc <= 0) |
| 300 | goto free_iovecs; |
| 301 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 302 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
| 303 | |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 304 | rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, |
| 305 | iovstack_r, &iov_r); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 306 | if (rc <= 0) |
| 307 | goto free_iovecs; |
| 308 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 309 | rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 349 | struct iov_iter iter; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 350 | ssize_t rc = -EFAULT; |
| 351 | |
| 352 | if (flags != 0) |
| 353 | return -EINVAL; |
| 354 | |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 355 | if (vm_write) |
| 356 | rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt, |
| 357 | UIO_FASTIOV, iovstack_l, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 358 | &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 359 | else |
| 360 | rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt, |
| 361 | UIO_FASTIOV, iovstack_l, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 362 | &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 363 | if (rc <= 0) |
| 364 | goto free_iovecs; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 365 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 366 | rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 367 | UIO_FASTIOV, iovstack_r, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 368 | &iov_r); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 369 | if (rc <= 0) |
| 370 | goto free_iovecs; |
| 371 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame] | 372 | rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 373 | |
| 374 | free_iovecs: |
| 375 | if (iov_r != iovstack_r) |
| 376 | kfree(iov_r); |
| 377 | if (iov_l != iovstack_l) |
| 378 | kfree(iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 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 |