Back to home page

LXR

 
 

    


0001 /*
0002  * linux/mm/process_vm_access.c
0003  *
0004  * Copyright (C) 2010-2011 Christopher Yeoh <cyeoh@au1.ibm.com>, IBM Corp.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version
0009  * 2 of the License, or (at your option) any later version.
0010  */
0011 
0012 #include <linux/mm.h>
0013 #include <linux/uio.h>
0014 #include <linux/sched.h>
0015 #include <linux/highmem.h>
0016 #include <linux/ptrace.h>
0017 #include <linux/slab.h>
0018 #include <linux/syscalls.h>
0019 
0020 #ifdef CONFIG_COMPAT
0021 #include <linux/compat.h>
0022 #endif
0023 
0024 /**
0025  * process_vm_rw_pages - read/write pages from task specified
0026  * @pages: array of pointers to pages we want to copy
0027  * @start_offset: offset in page to start copying from/to
0028  * @len: number of bytes to copy
0029  * @iter: where to copy to/from locally
0030  * @vm_write: 0 means copy from, 1 means copy to
0031  * Returns 0 on success, error code otherwise
0032  */
0033 static int process_vm_rw_pages(struct page **pages,
0034                    unsigned offset,
0035                    size_t len,
0036                    struct iov_iter *iter,
0037                    int vm_write)
0038 {
0039     /* Do the copy for each page */
0040     while (len && iov_iter_count(iter)) {
0041         struct page *page = *pages++;
0042         size_t copy = PAGE_SIZE - offset;
0043         size_t copied;
0044 
0045         if (copy > len)
0046             copy = len;
0047 
0048         if (vm_write) {
0049             copied = copy_page_from_iter(page, offset, copy, iter);
0050             set_page_dirty_lock(page);
0051         } else {
0052             copied = copy_page_to_iter(page, offset, copy, iter);
0053         }
0054         len -= copied;
0055         if (copied < copy && iov_iter_count(iter))
0056             return -EFAULT;
0057         offset = 0;
0058     }
0059     return 0;
0060 }
0061 
0062 /* Maximum number of pages kmalloc'd to hold struct page's during copy */
0063 #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
0064 
0065 /**
0066  * process_vm_rw_single_vec - read/write pages from task specified
0067  * @addr: start memory address of target process
0068  * @len: size of area to copy to/from
0069  * @iter: where to copy to/from locally
0070  * @process_pages: struct pages area that can store at least
0071  *  nr_pages_to_copy struct page pointers
0072  * @mm: mm for task
0073  * @task: task to read/write from
0074  * @vm_write: 0 means copy from, 1 means copy to
0075  * Returns 0 on success or on failure error code
0076  */
0077 static int process_vm_rw_single_vec(unsigned long addr,
0078                     unsigned long len,
0079                     struct iov_iter *iter,
0080                     struct page **process_pages,
0081                     struct mm_struct *mm,
0082                     struct task_struct *task,
0083                     int vm_write)
0084 {
0085     unsigned long pa = addr & PAGE_MASK;
0086     unsigned long start_offset = addr - pa;
0087     unsigned long nr_pages;
0088     ssize_t rc = 0;
0089     unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
0090         / sizeof(struct pages *);
0091     unsigned int flags = 0;
0092 
0093     /* Work out address and page range required */
0094     if (len == 0)
0095         return 0;
0096     nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
0097 
0098     if (vm_write)
0099         flags |= FOLL_WRITE;
0100 
0101     while (!rc && nr_pages && iov_iter_count(iter)) {
0102         int pages = min(nr_pages, max_pages_per_loop);
0103         int locked = 1;
0104         size_t bytes;
0105 
0106         /*
0107          * Get the pages we're interested in.  We must
0108          * access remotely because task/mm might not
0109          * current/current->mm
0110          */
0111         down_read(&mm->mmap_sem);
0112         pages = get_user_pages_remote(task, mm, pa, pages, flags,
0113                           process_pages, NULL, &locked);
0114         if (locked)
0115             up_read(&mm->mmap_sem);
0116         if (pages <= 0)
0117             return -EFAULT;
0118 
0119         bytes = pages * PAGE_SIZE - start_offset;
0120         if (bytes > len)
0121             bytes = len;
0122 
0123         rc = process_vm_rw_pages(process_pages,
0124                      start_offset, bytes, iter,
0125                      vm_write);
0126         len -= bytes;
0127         start_offset = 0;
0128         nr_pages -= pages;
0129         pa += pages * PAGE_SIZE;
0130         while (pages)
0131             put_page(process_pages[--pages]);
0132     }
0133 
0134     return rc;
0135 }
0136 
0137 /* Maximum number of entries for process pages array
0138    which lives on stack */
0139 #define PVM_MAX_PP_ARRAY_COUNT 16
0140 
0141 /**
0142  * process_vm_rw_core - core of reading/writing pages from task specified
0143  * @pid: PID of process to read/write from/to
0144  * @iter: where to copy to/from locally
0145  * @rvec: iovec array specifying where to copy to/from in the other process
0146  * @riovcnt: size of rvec array
0147  * @flags: currently unused
0148  * @vm_write: 0 if reading from other process, 1 if writing to other process
0149  * Returns the number of bytes read/written or error code. May
0150  *  return less bytes than expected if an error occurs during the copying
0151  *  process.
0152  */
0153 static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
0154                   const struct iovec *rvec,
0155                   unsigned long riovcnt,
0156                   unsigned long flags, int vm_write)
0157 {
0158     struct task_struct *task;
0159     struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
0160     struct page **process_pages = pp_stack;
0161     struct mm_struct *mm;
0162     unsigned long i;
0163     ssize_t rc = 0;
0164     unsigned long nr_pages = 0;
0165     unsigned long nr_pages_iov;
0166     ssize_t iov_len;
0167     size_t total_len = iov_iter_count(iter);
0168 
0169     /*
0170      * Work out how many pages of struct pages we're going to need
0171      * when eventually calling get_user_pages
0172      */
0173     for (i = 0; i < riovcnt; i++) {
0174         iov_len = rvec[i].iov_len;
0175         if (iov_len > 0) {
0176             nr_pages_iov = ((unsigned long)rvec[i].iov_base
0177                     + iov_len)
0178                 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
0179                 / PAGE_SIZE + 1;
0180             nr_pages = max(nr_pages, nr_pages_iov);
0181         }
0182     }
0183 
0184     if (nr_pages == 0)
0185         return 0;
0186 
0187     if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
0188         /* For reliability don't try to kmalloc more than
0189            2 pages worth */
0190         process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
0191                           sizeof(struct pages *)*nr_pages),
0192                     GFP_KERNEL);
0193 
0194         if (!process_pages)
0195             return -ENOMEM;
0196     }
0197 
0198     /* Get process information */
0199     rcu_read_lock();
0200     task = find_task_by_vpid(pid);
0201     if (task)
0202         get_task_struct(task);
0203     rcu_read_unlock();
0204     if (!task) {
0205         rc = -ESRCH;
0206         goto free_proc_pages;
0207     }
0208 
0209     mm = mm_access(task, PTRACE_MODE_ATTACH_REALCREDS);
0210     if (!mm || IS_ERR(mm)) {
0211         rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
0212         /*
0213          * Explicitly map EACCES to EPERM as EPERM is a more a
0214          * appropriate error code for process_vw_readv/writev
0215          */
0216         if (rc == -EACCES)
0217             rc = -EPERM;
0218         goto put_task_struct;
0219     }
0220 
0221     for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++)
0222         rc = process_vm_rw_single_vec(
0223             (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
0224             iter, process_pages, mm, task, vm_write);
0225 
0226     /* copied = space before - space after */
0227     total_len -= iov_iter_count(iter);
0228 
0229     /* If we have managed to copy any data at all then
0230        we return the number of bytes copied. Otherwise
0231        we return the error code */
0232     if (total_len)
0233         rc = total_len;
0234 
0235     mmput(mm);
0236 
0237 put_task_struct:
0238     put_task_struct(task);
0239 
0240 free_proc_pages:
0241     if (process_pages != pp_stack)
0242         kfree(process_pages);
0243     return rc;
0244 }
0245 
0246 /**
0247  * process_vm_rw - check iovecs before calling core routine
0248  * @pid: PID of process to read/write from/to
0249  * @lvec: iovec array specifying where to copy to/from locally
0250  * @liovcnt: size of lvec array
0251  * @rvec: iovec array specifying where to copy to/from in the other process
0252  * @riovcnt: size of rvec array
0253  * @flags: currently unused
0254  * @vm_write: 0 if reading from other process, 1 if writing to other process
0255  * Returns the number of bytes read/written or error code. May
0256  *  return less bytes than expected if an error occurs during the copying
0257  *  process.
0258  */
0259 static ssize_t process_vm_rw(pid_t pid,
0260                  const struct iovec __user *lvec,
0261                  unsigned long liovcnt,
0262                  const struct iovec __user *rvec,
0263                  unsigned long riovcnt,
0264                  unsigned long flags, int vm_write)
0265 {
0266     struct iovec iovstack_l[UIO_FASTIOV];
0267     struct iovec iovstack_r[UIO_FASTIOV];
0268     struct iovec *iov_l = iovstack_l;
0269     struct iovec *iov_r = iovstack_r;
0270     struct iov_iter iter;
0271     ssize_t rc;
0272     int dir = vm_write ? WRITE : READ;
0273 
0274     if (flags != 0)
0275         return -EINVAL;
0276 
0277     /* Check iovecs */
0278     rc = import_iovec(dir, lvec, liovcnt, UIO_FASTIOV, &iov_l, &iter);
0279     if (rc < 0)
0280         return rc;
0281     if (!iov_iter_count(&iter))
0282         goto free_iovecs;
0283 
0284     rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
0285                    iovstack_r, &iov_r);
0286     if (rc <= 0)
0287         goto free_iovecs;
0288 
0289     rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
0290 
0291 free_iovecs:
0292     if (iov_r != iovstack_r)
0293         kfree(iov_r);
0294     kfree(iov_l);
0295 
0296     return rc;
0297 }
0298 
0299 SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
0300         unsigned long, liovcnt, const struct iovec __user *, rvec,
0301         unsigned long, riovcnt, unsigned long, flags)
0302 {
0303     return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
0304 }
0305 
0306 SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
0307         const struct iovec __user *, lvec,
0308         unsigned long, liovcnt, const struct iovec __user *, rvec,
0309         unsigned long, riovcnt, unsigned long, flags)
0310 {
0311     return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
0312 }
0313 
0314 #ifdef CONFIG_COMPAT
0315 
0316 static ssize_t
0317 compat_process_vm_rw(compat_pid_t pid,
0318              const struct compat_iovec __user *lvec,
0319              unsigned long liovcnt,
0320              const struct compat_iovec __user *rvec,
0321              unsigned long riovcnt,
0322              unsigned long flags, int vm_write)
0323 {
0324     struct iovec iovstack_l[UIO_FASTIOV];
0325     struct iovec iovstack_r[UIO_FASTIOV];
0326     struct iovec *iov_l = iovstack_l;
0327     struct iovec *iov_r = iovstack_r;
0328     struct iov_iter iter;
0329     ssize_t rc = -EFAULT;
0330     int dir = vm_write ? WRITE : READ;
0331 
0332     if (flags != 0)
0333         return -EINVAL;
0334 
0335     rc = compat_import_iovec(dir, lvec, liovcnt, UIO_FASTIOV, &iov_l, &iter);
0336     if (rc < 0)
0337         return rc;
0338     if (!iov_iter_count(&iter))
0339         goto free_iovecs;
0340     rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
0341                       UIO_FASTIOV, iovstack_r,
0342                       &iov_r);
0343     if (rc <= 0)
0344         goto free_iovecs;
0345 
0346     rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
0347 
0348 free_iovecs:
0349     if (iov_r != iovstack_r)
0350         kfree(iov_r);
0351     kfree(iov_l);
0352     return rc;
0353 }
0354 
0355 COMPAT_SYSCALL_DEFINE6(process_vm_readv, compat_pid_t, pid,
0356                const struct compat_iovec __user *, lvec,
0357                compat_ulong_t, liovcnt,
0358                const struct compat_iovec __user *, rvec,
0359                compat_ulong_t, riovcnt,
0360                compat_ulong_t, flags)
0361 {
0362     return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
0363                     riovcnt, flags, 0);
0364 }
0365 
0366 COMPAT_SYSCALL_DEFINE6(process_vm_writev, compat_pid_t, pid,
0367                const struct compat_iovec __user *, lvec,
0368                compat_ulong_t, liovcnt,
0369                const struct compat_iovec __user *, rvec,
0370                compat_ulong_t, riovcnt,
0371                compat_ulong_t, flags)
0372 {
0373     return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
0374                     riovcnt, flags, 1);
0375 }
0376 
0377 #endif