Commit bfdc5970 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Al Viro

iov_iter: refactor rw_copy_check_uvector and import_iovec

Split rw_copy_check_uvector into two new helpers with more sensible
calling conventions:

 - iovec_from_user copies a iovec from userspace either into the provided
   stack buffer if it fits, or allocates a new buffer for it.  Returns
   the actually used iovec.  It also verifies that iov_len does fit a
   signed type, and handles compat iovecs if the compat flag is set.
 - __import_iovec consolidates the native and compat versions of
   import_iovec. It calls iovec_from_user, then validates each iovec
   actually points to user addresses, and ensures the total length
   doesn't overflow.

This has two major implications:

 - the access_process_vm case loses the total lenght checking, which
   wasn't required anyway, given that each call receives two iovecs
   for the local and remote side of the operation, and it verifies
   the total length on the local side already.
 - instead of a single loop there now are two loops over the iovecs.
   Given that the iovecs are cache hot this doesn't make a major
   difference
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent fb041b59
...@@ -91,6 +91,11 @@ ...@@ -91,6 +91,11 @@
static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__)) static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
#endif /* COMPAT_SYSCALL_DEFINEx */ #endif /* COMPAT_SYSCALL_DEFINEx */
struct compat_iovec {
compat_uptr_t iov_base;
compat_size_t iov_len;
};
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
#ifndef compat_user_stack_pointer #ifndef compat_user_stack_pointer
...@@ -248,11 +253,6 @@ typedef struct compat_siginfo { ...@@ -248,11 +253,6 @@ typedef struct compat_siginfo {
} _sifields; } _sifields;
} compat_siginfo_t; } compat_siginfo_t;
struct compat_iovec {
compat_uptr_t iov_base;
compat_size_t iov_len;
};
struct compat_rlimit { struct compat_rlimit {
compat_ulong_t rlim_cur; compat_ulong_t rlim_cur;
compat_ulong_t rlim_max; compat_ulong_t rlim_max;
...@@ -451,12 +451,6 @@ extern long compat_arch_ptrace(struct task_struct *child, compat_long_t request, ...@@ -451,12 +451,6 @@ extern long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
struct epoll_event; /* fortunately, this one is fixed-layout */ struct epoll_event; /* fortunately, this one is fixed-layout */
extern ssize_t compat_rw_copy_check_uvector(int type,
const struct compat_iovec __user *uvector,
unsigned long nr_segs,
unsigned long fast_segs, struct iovec *fast_pointer,
struct iovec **ret_pointer);
extern void __user *compat_alloc_user_space(unsigned long len); extern void __user *compat_alloc_user_space(unsigned long len);
int compat_restore_altstack(const compat_stack_t __user *uss); int compat_restore_altstack(const compat_stack_t __user *uss);
......
...@@ -178,14 +178,6 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, ...@@ -178,14 +178,6 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
/* File supports async buffered reads */ /* File supports async buffered reads */
#define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000) #define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000)
/*
* Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector
* that indicates that they should check the contents of the iovec are
* valid, but not check the memory that the iovec elements
* points too.
*/
#define CHECK_IOVEC_ONLY -1
/* /*
* Attribute flags. These should be or-ed together to figure out what * Attribute flags. These should be or-ed together to figure out what
* has been changed! * has been changed!
...@@ -1887,11 +1879,6 @@ static inline int call_mmap(struct file *file, struct vm_area_struct *vma) ...@@ -1887,11 +1879,6 @@ static inline int call_mmap(struct file *file, struct vm_area_struct *vma)
return file->f_op->mmap(file, vma); return file->f_op->mmap(file, vma);
} }
ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
unsigned long nr_segs, unsigned long fast_segs,
struct iovec *fast_pointer,
struct iovec **ret_pointer);
extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *); extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *); extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
extern ssize_t vfs_readv(struct file *, const struct iovec __user *, extern ssize_t vfs_readv(struct file *, const struct iovec __user *,
......
...@@ -266,9 +266,15 @@ bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct ...@@ -266,9 +266,15 @@ bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct
size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
struct iov_iter *i); struct iov_iter *i);
ssize_t import_iovec(int type, const struct iovec __user * uvector, struct iovec *iovec_from_user(const struct iovec __user *uvector,
unsigned nr_segs, unsigned fast_segs, unsigned long nr_segs, unsigned long fast_segs,
struct iovec **iov, struct iov_iter *i); struct iovec *fast_iov, bool compat);
ssize_t import_iovec(int type, const struct iovec __user *uvec,
unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
struct iov_iter *i);
ssize_t __import_iovec(int type, const struct iovec __user *uvec,
unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
struct iov_iter *i, bool compat);
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
struct compat_iovec; struct compat_iovec;
......
This diff is collapsed.
...@@ -276,20 +276,17 @@ static ssize_t process_vm_rw(pid_t pid, ...@@ -276,20 +276,17 @@ static ssize_t process_vm_rw(pid_t pid,
if (rc < 0) if (rc < 0)
return rc; return rc;
if (!iov_iter_count(&iter)) if (!iov_iter_count(&iter))
goto free_iovecs; goto free_iov_l;
iov_r = iovec_from_user(rvec, riovcnt, UIO_FASTIOV, iovstack_r, false);
rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, if (IS_ERR(iov_r)) {
iovstack_r, &iov_r); rc = PTR_ERR(iov_r);
if (rc <= 0) goto free_iov_l;
goto free_iovecs; }
rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
free_iovecs:
if (iov_r != iovstack_r) if (iov_r != iovstack_r)
kfree(iov_r); kfree(iov_r);
free_iov_l:
kfree(iov_l); kfree(iov_l);
return rc; return rc;
} }
...@@ -333,18 +330,17 @@ compat_process_vm_rw(compat_pid_t pid, ...@@ -333,18 +330,17 @@ compat_process_vm_rw(compat_pid_t pid,
if (rc < 0) if (rc < 0)
return rc; return rc;
if (!iov_iter_count(&iter)) if (!iov_iter_count(&iter))
goto free_iovecs; goto free_iov_l;
rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, iov_r = iovec_from_user((const struct iovec __user *)rvec, riovcnt,
UIO_FASTIOV, iovstack_r, UIO_FASTIOV, iovstack_r, true);
&iov_r); if (IS_ERR(iov_r)) {
if (rc <= 0) rc = PTR_ERR(iov_r);
goto free_iovecs; goto free_iov_l;
}
rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
free_iovecs:
if (iov_r != iovstack_r) if (iov_r != iovstack_r)
kfree(iov_r); kfree(iov_r);
free_iov_l:
kfree(iov_l); kfree(iov_l);
return rc; return rc;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment