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 | */ |
| 42 | static int process_vm_rw_pages(struct task_struct *task, |
| 43 | struct mm_struct *mm, |
| 44 | struct page **process_pages, |
| 45 | unsigned long pa, |
| 46 | unsigned long start_offset, |
| 47 | unsigned long len, |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 48 | struct iov_iter *iter, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 49 | int vm_write, |
| 50 | unsigned int nr_pages_to_copy, |
| 51 | ssize_t *bytes_copied) |
| 52 | { |
| 53 | int pages_pinned; |
| 54 | void *target_kaddr; |
| 55 | int pgs_copied = 0; |
| 56 | int j; |
| 57 | int ret; |
| 58 | ssize_t bytes_to_copy; |
| 59 | ssize_t rc = 0; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 60 | const struct iovec *iov = iter->iov; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 61 | |
| 62 | *bytes_copied = 0; |
| 63 | |
| 64 | /* Get the pages we're interested in */ |
| 65 | down_read(&mm->mmap_sem); |
| 66 | pages_pinned = get_user_pages(task, mm, pa, |
| 67 | nr_pages_to_copy, |
| 68 | vm_write, 0, process_pages, NULL); |
| 69 | up_read(&mm->mmap_sem); |
| 70 | |
| 71 | if (pages_pinned != nr_pages_to_copy) { |
| 72 | rc = -EFAULT; |
| 73 | goto end; |
| 74 | } |
| 75 | |
| 76 | /* Do the copy for each page */ |
| 77 | for (pgs_copied = 0; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 78 | (pgs_copied < nr_pages_to_copy) && iter->nr_segs; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 79 | pgs_copied++) { |
| 80 | /* Make sure we have a non zero length iovec */ |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 81 | while (iter->nr_segs && iov->iov_len == 0) { |
Al Viro | 480402e | 2014-02-05 10:14:01 -0500 | [diff] [blame] | 82 | iov++; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 83 | iter->nr_segs--; |
Al Viro | 480402e | 2014-02-05 10:14:01 -0500 | [diff] [blame] | 84 | } |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 85 | if (!iter->nr_segs) |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 86 | break; |
| 87 | |
| 88 | /* |
| 89 | * Will copy smallest of: |
| 90 | * - bytes remaining in page |
| 91 | * - bytes remaining in destination iovec |
| 92 | */ |
| 93 | bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset, |
| 94 | len - *bytes_copied); |
| 95 | bytes_to_copy = min_t(ssize_t, bytes_to_copy, |
Al Viro | 480402e | 2014-02-05 10:14:01 -0500 | [diff] [blame] | 96 | iov->iov_len |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 97 | - iter->iov_offset); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 98 | |
| 99 | target_kaddr = kmap(process_pages[pgs_copied]) + start_offset; |
| 100 | |
| 101 | if (vm_write) |
| 102 | ret = copy_from_user(target_kaddr, |
Al Viro | 480402e | 2014-02-05 10:14:01 -0500 | [diff] [blame] | 103 | iov->iov_base |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 104 | + iter->iov_offset, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 105 | bytes_to_copy); |
| 106 | else |
Al Viro | 480402e | 2014-02-05 10:14:01 -0500 | [diff] [blame] | 107 | ret = copy_to_user(iov->iov_base |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 108 | + iter->iov_offset, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 109 | target_kaddr, bytes_to_copy); |
| 110 | kunmap(process_pages[pgs_copied]); |
| 111 | if (ret) { |
| 112 | *bytes_copied += bytes_to_copy - ret; |
| 113 | pgs_copied++; |
| 114 | rc = -EFAULT; |
| 115 | goto end; |
| 116 | } |
| 117 | *bytes_copied += bytes_to_copy; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 118 | iter->iov_offset += bytes_to_copy; |
| 119 | if (iter->iov_offset == iov->iov_len) { |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 120 | /* |
| 121 | * Need to copy remaining part of page into the |
| 122 | * next iovec if there are any bytes left in page |
| 123 | */ |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 124 | iter->nr_segs--; |
Al Viro | 480402e | 2014-02-05 10:14:01 -0500 | [diff] [blame] | 125 | iov++; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 126 | iter->iov_offset = 0; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 127 | start_offset = (start_offset + bytes_to_copy) |
| 128 | % PAGE_SIZE; |
| 129 | if (start_offset) |
| 130 | pgs_copied--; |
| 131 | } else { |
| 132 | start_offset = 0; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | end: |
| 137 | if (vm_write) { |
| 138 | for (j = 0; j < pages_pinned; j++) { |
| 139 | if (j < pgs_copied) |
| 140 | set_page_dirty_lock(process_pages[j]); |
| 141 | put_page(process_pages[j]); |
| 142 | } |
| 143 | } else { |
| 144 | for (j = 0; j < pages_pinned; j++) |
| 145 | put_page(process_pages[j]); |
| 146 | } |
| 147 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 148 | iter->iov = iov; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 149 | return rc; |
| 150 | } |
| 151 | |
| 152 | /* Maximum number of pages kmalloc'd to hold struct page's during copy */ |
| 153 | #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2) |
| 154 | |
| 155 | /** |
| 156 | * process_vm_rw_single_vec - read/write pages from task specified |
| 157 | * @addr: start memory address of target process |
| 158 | * @len: size of area to copy to/from |
| 159 | * @lvec: iovec array specifying where to copy to/from locally |
| 160 | * @lvec_cnt: number of elements in iovec array |
| 161 | * @lvec_current: index in iovec array we are up to |
| 162 | * @lvec_offset: offset in bytes from current iovec iov_base we are up to |
| 163 | * @process_pages: struct pages area that can store at least |
| 164 | * nr_pages_to_copy struct page pointers |
| 165 | * @mm: mm for task |
| 166 | * @task: task to read/write from |
| 167 | * @vm_write: 0 means copy from, 1 means copy to |
| 168 | * @bytes_copied: returns number of bytes successfully copied |
| 169 | * Returns 0 on success or on failure error code |
| 170 | */ |
| 171 | static int process_vm_rw_single_vec(unsigned long addr, |
| 172 | unsigned long len, |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 173 | struct iov_iter *iter, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 174 | struct page **process_pages, |
| 175 | struct mm_struct *mm, |
| 176 | struct task_struct *task, |
| 177 | int vm_write, |
| 178 | ssize_t *bytes_copied) |
| 179 | { |
| 180 | unsigned long pa = addr & PAGE_MASK; |
| 181 | unsigned long start_offset = addr - pa; |
| 182 | unsigned long nr_pages; |
| 183 | ssize_t bytes_copied_loop; |
| 184 | ssize_t rc = 0; |
| 185 | unsigned long nr_pages_copied = 0; |
| 186 | unsigned long nr_pages_to_copy; |
| 187 | unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES |
| 188 | / sizeof(struct pages *); |
| 189 | |
| 190 | *bytes_copied = 0; |
| 191 | |
| 192 | /* Work out address and page range required */ |
| 193 | if (len == 0) |
| 194 | return 0; |
| 195 | nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1; |
| 196 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 197 | while ((nr_pages_copied < nr_pages) && iter->nr_segs) { |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 198 | nr_pages_to_copy = min(nr_pages - nr_pages_copied, |
| 199 | max_pages_per_loop); |
| 200 | |
| 201 | rc = process_vm_rw_pages(task, mm, process_pages, pa, |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 202 | start_offset, len, iter, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 203 | vm_write, nr_pages_to_copy, |
| 204 | &bytes_copied_loop); |
| 205 | start_offset = 0; |
| 206 | *bytes_copied += bytes_copied_loop; |
| 207 | |
| 208 | if (rc < 0) { |
| 209 | return rc; |
| 210 | } else { |
| 211 | len -= bytes_copied_loop; |
| 212 | nr_pages_copied += nr_pages_to_copy; |
| 213 | pa += nr_pages_to_copy * PAGE_SIZE; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | /* Maximum number of entries for process pages array |
| 221 | which lives on stack */ |
| 222 | #define PVM_MAX_PP_ARRAY_COUNT 16 |
| 223 | |
| 224 | /** |
| 225 | * process_vm_rw_core - core of reading/writing pages from task specified |
| 226 | * @pid: PID of process to read/write from/to |
| 227 | * @lvec: iovec array specifying where to copy to/from locally |
| 228 | * @liovcnt: size of lvec array |
| 229 | * @rvec: iovec array specifying where to copy to/from in the other process |
| 230 | * @riovcnt: size of rvec array |
| 231 | * @flags: currently unused |
| 232 | * @vm_write: 0 if reading from other process, 1 if writing to other process |
| 233 | * Returns the number of bytes read/written or error code. May |
| 234 | * return less bytes than expected if an error occurs during the copying |
| 235 | * process. |
| 236 | */ |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 237 | 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] | 238 | const struct iovec *rvec, |
| 239 | unsigned long riovcnt, |
| 240 | unsigned long flags, int vm_write) |
| 241 | { |
| 242 | struct task_struct *task; |
| 243 | struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT]; |
| 244 | struct page **process_pages = pp_stack; |
| 245 | struct mm_struct *mm; |
| 246 | unsigned long i; |
| 247 | ssize_t rc = 0; |
| 248 | ssize_t bytes_copied_loop; |
| 249 | ssize_t bytes_copied = 0; |
| 250 | unsigned long nr_pages = 0; |
| 251 | unsigned long nr_pages_iov; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 252 | ssize_t iov_len; |
| 253 | |
| 254 | /* |
| 255 | * Work out how many pages of struct pages we're going to need |
| 256 | * when eventually calling get_user_pages |
| 257 | */ |
| 258 | for (i = 0; i < riovcnt; i++) { |
| 259 | iov_len = rvec[i].iov_len; |
| 260 | if (iov_len > 0) { |
| 261 | nr_pages_iov = ((unsigned long)rvec[i].iov_base |
| 262 | + iov_len) |
| 263 | / PAGE_SIZE - (unsigned long)rvec[i].iov_base |
| 264 | / PAGE_SIZE + 1; |
| 265 | nr_pages = max(nr_pages, nr_pages_iov); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (nr_pages == 0) |
| 270 | return 0; |
| 271 | |
| 272 | if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) { |
| 273 | /* For reliability don't try to kmalloc more than |
| 274 | 2 pages worth */ |
| 275 | process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES, |
| 276 | sizeof(struct pages *)*nr_pages), |
| 277 | GFP_KERNEL); |
| 278 | |
| 279 | if (!process_pages) |
| 280 | return -ENOMEM; |
| 281 | } |
| 282 | |
| 283 | /* Get process information */ |
| 284 | rcu_read_lock(); |
| 285 | task = find_task_by_vpid(pid); |
| 286 | if (task) |
| 287 | get_task_struct(task); |
| 288 | rcu_read_unlock(); |
| 289 | if (!task) { |
| 290 | rc = -ESRCH; |
| 291 | goto free_proc_pages; |
| 292 | } |
| 293 | |
Christopher Yeoh | 8cdb878 | 2012-02-02 11:34:09 +1030 | [diff] [blame] | 294 | mm = mm_access(task, PTRACE_MODE_ATTACH); |
| 295 | if (!mm || IS_ERR(mm)) { |
| 296 | rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH; |
| 297 | /* |
| 298 | * Explicitly map EACCES to EPERM as EPERM is a more a |
| 299 | * appropriate error code for process_vw_readv/writev |
| 300 | */ |
| 301 | if (rc == -EACCES) |
| 302 | rc = -EPERM; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 303 | goto put_task_struct; |
| 304 | } |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 305 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 306 | for (i = 0; i < riovcnt && iter->nr_segs; i++) { |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 307 | rc = process_vm_rw_single_vec( |
| 308 | (unsigned long)rvec[i].iov_base, rvec[i].iov_len, |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 309 | iter, process_pages, mm, task, vm_write, |
| 310 | &bytes_copied_loop); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 311 | bytes_copied += bytes_copied_loop; |
| 312 | if (rc != 0) { |
| 313 | /* If we have managed to copy any data at all then |
| 314 | we return the number of bytes copied. Otherwise |
| 315 | we return the error code */ |
| 316 | if (bytes_copied) |
| 317 | rc = bytes_copied; |
| 318 | goto put_mm; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | rc = bytes_copied; |
| 323 | put_mm: |
| 324 | mmput(mm); |
| 325 | |
| 326 | put_task_struct: |
| 327 | put_task_struct(task); |
| 328 | |
| 329 | free_proc_pages: |
| 330 | if (process_pages != pp_stack) |
| 331 | kfree(process_pages); |
| 332 | return rc; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * process_vm_rw - check iovecs before calling core routine |
| 337 | * @pid: PID of process to read/write from/to |
| 338 | * @lvec: iovec array specifying where to copy to/from locally |
| 339 | * @liovcnt: size of lvec array |
| 340 | * @rvec: iovec array specifying where to copy to/from in the other process |
| 341 | * @riovcnt: size of rvec array |
| 342 | * @flags: currently unused |
| 343 | * @vm_write: 0 if reading from other process, 1 if writing to other process |
| 344 | * Returns the number of bytes read/written or error code. May |
| 345 | * return less bytes than expected if an error occurs during the copying |
| 346 | * process. |
| 347 | */ |
| 348 | static ssize_t process_vm_rw(pid_t pid, |
| 349 | const struct iovec __user *lvec, |
| 350 | unsigned long liovcnt, |
| 351 | const struct iovec __user *rvec, |
| 352 | unsigned long riovcnt, |
| 353 | unsigned long flags, int vm_write) |
| 354 | { |
| 355 | struct iovec iovstack_l[UIO_FASTIOV]; |
| 356 | struct iovec iovstack_r[UIO_FASTIOV]; |
| 357 | struct iovec *iov_l = iovstack_l; |
| 358 | struct iovec *iov_r = iovstack_r; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 359 | struct iov_iter iter; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 360 | ssize_t rc; |
| 361 | |
| 362 | if (flags != 0) |
| 363 | return -EINVAL; |
| 364 | |
| 365 | /* Check iovecs */ |
| 366 | if (vm_write) |
| 367 | rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 368 | iovstack_l, &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 369 | else |
| 370 | rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 371 | iovstack_l, &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 372 | if (rc <= 0) |
| 373 | goto free_iovecs; |
| 374 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 375 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
| 376 | |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 377 | rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, |
| 378 | iovstack_r, &iov_r); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 379 | if (rc <= 0) |
| 380 | goto free_iovecs; |
| 381 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 382 | 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] | 383 | |
| 384 | free_iovecs: |
| 385 | if (iov_r != iovstack_r) |
| 386 | kfree(iov_r); |
| 387 | if (iov_l != iovstack_l) |
| 388 | kfree(iov_l); |
| 389 | |
| 390 | return rc; |
| 391 | } |
| 392 | |
| 393 | SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec, |
| 394 | unsigned long, liovcnt, const struct iovec __user *, rvec, |
| 395 | unsigned long, riovcnt, unsigned long, flags) |
| 396 | { |
| 397 | return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0); |
| 398 | } |
| 399 | |
| 400 | SYSCALL_DEFINE6(process_vm_writev, pid_t, pid, |
| 401 | const struct iovec __user *, lvec, |
| 402 | unsigned long, liovcnt, const struct iovec __user *, rvec, |
| 403 | unsigned long, riovcnt, unsigned long, flags) |
| 404 | { |
| 405 | return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1); |
| 406 | } |
| 407 | |
| 408 | #ifdef CONFIG_COMPAT |
| 409 | |
| 410 | asmlinkage ssize_t |
| 411 | compat_process_vm_rw(compat_pid_t pid, |
| 412 | const struct compat_iovec __user *lvec, |
| 413 | unsigned long liovcnt, |
| 414 | const struct compat_iovec __user *rvec, |
| 415 | unsigned long riovcnt, |
| 416 | unsigned long flags, int vm_write) |
| 417 | { |
| 418 | struct iovec iovstack_l[UIO_FASTIOV]; |
| 419 | struct iovec iovstack_r[UIO_FASTIOV]; |
| 420 | struct iovec *iov_l = iovstack_l; |
| 421 | struct iovec *iov_r = iovstack_r; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 422 | struct iov_iter iter; |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 423 | ssize_t rc = -EFAULT; |
| 424 | |
| 425 | if (flags != 0) |
| 426 | return -EINVAL; |
| 427 | |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 428 | if (vm_write) |
| 429 | rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt, |
| 430 | UIO_FASTIOV, iovstack_l, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 431 | &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 432 | else |
| 433 | rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt, |
| 434 | UIO_FASTIOV, iovstack_l, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 435 | &iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 436 | if (rc <= 0) |
| 437 | goto free_iovecs; |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 438 | iov_iter_init(&iter, iov_l, liovcnt, rc, 0); |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 439 | rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 440 | UIO_FASTIOV, iovstack_r, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 441 | &iov_r); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 442 | if (rc <= 0) |
| 443 | goto free_iovecs; |
| 444 | |
Al Viro | 9f78bdf | 2014-02-05 11:51:53 -0500 | [diff] [blame^] | 445 | 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] | 446 | |
| 447 | free_iovecs: |
| 448 | if (iov_r != iovstack_r) |
| 449 | kfree(iov_r); |
| 450 | if (iov_l != iovstack_l) |
| 451 | kfree(iov_l); |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 452 | return rc; |
| 453 | } |
| 454 | |
| 455 | asmlinkage ssize_t |
| 456 | compat_sys_process_vm_readv(compat_pid_t pid, |
| 457 | const struct compat_iovec __user *lvec, |
| 458 | unsigned long liovcnt, |
| 459 | const struct compat_iovec __user *rvec, |
| 460 | unsigned long riovcnt, |
| 461 | unsigned long flags) |
| 462 | { |
| 463 | return compat_process_vm_rw(pid, lvec, liovcnt, rvec, |
| 464 | riovcnt, flags, 0); |
| 465 | } |
| 466 | |
| 467 | asmlinkage ssize_t |
| 468 | compat_sys_process_vm_writev(compat_pid_t pid, |
| 469 | const struct compat_iovec __user *lvec, |
| 470 | unsigned long liovcnt, |
| 471 | const struct compat_iovec __user *rvec, |
| 472 | unsigned long riovcnt, |
| 473 | unsigned long flags) |
| 474 | { |
| 475 | return compat_process_vm_rw(pid, lvec, liovcnt, rvec, |
| 476 | riovcnt, flags, 1); |
| 477 | } |
| 478 | |
| 479 | #endif |