Skip to content

Commit bfdc597

Browse files
Christoph HellwigAl Viro
authored andcommitted
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: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent fb041b5 commit bfdc597

5 files changed

Lines changed: 143 additions & 232 deletions

File tree

‎include/linux/compat.h‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
9292
#endif /* COMPAT_SYSCALL_DEFINEx */
9393

94+
struct compat_iovec {
95+
compat_uptr_t iov_base;
96+
compat_size_t iov_len;
97+
};
98+
9499
#ifdef CONFIG_COMPAT
95100

96101
#ifndef compat_user_stack_pointer
@@ -248,11 +253,6 @@ typedef struct compat_siginfo {
248253
} _sifields;
249254
} compat_siginfo_t;
250255

251-
struct compat_iovec {
252-
compat_uptr_t iov_base;
253-
compat_size_t iov_len;
254-
};
255-
256256
struct compat_rlimit {
257257
compat_ulong_t rlim_cur;
258258
compat_ulong_t rlim_max;
@@ -451,12 +451,6 @@ extern long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
451451

452452
struct epoll_event; /* fortunately, this one is fixed-layout */
453453

454-
extern ssize_t compat_rw_copy_check_uvector(int type,
455-
const struct compat_iovec __user *uvector,
456-
unsigned long nr_segs,
457-
unsigned long fast_segs, struct iovec *fast_pointer,
458-
struct iovec **ret_pointer);
459-
460454
extern void __user *compat_alloc_user_space(unsigned long len);
461455

462456
int compat_restore_altstack(const compat_stack_t __user *uss);

‎include/linux/fs.h‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,6 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
178178
/* File supports async buffered reads */
179179
#define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000)
180180

181-
/*
182-
* Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector
183-
* that indicates that they should check the contents of the iovec are
184-
* valid, but not check the memory that the iovec elements
185-
* points too.
186-
*/
187-
#define CHECK_IOVEC_ONLY -1
188-
189181
/*
190182
* Attribute flags. These should be or-ed together to figure out what
191183
* has been changed!
@@ -1887,11 +1879,6 @@ static inline int call_mmap(struct file *file, struct vm_area_struct *vma)
18871879
return file->f_op->mmap(file, vma);
18881880
}
18891881

1890-
ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
1891-
unsigned long nr_segs, unsigned long fast_segs,
1892-
struct iovec *fast_pointer,
1893-
struct iovec **ret_pointer);
1894-
18951882
extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
18961883
extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
18971884
extern ssize_t vfs_readv(struct file *, const struct iovec __user *,

‎include/linux/uio.h‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,15 @@ bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct
266266
size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
267267
struct iov_iter *i);
268268

269-
ssize_t import_iovec(int type, const struct iovec __user * uvector,
270-
unsigned nr_segs, unsigned fast_segs,
271-
struct iovec **iov, struct iov_iter *i);
269+
struct iovec *iovec_from_user(const struct iovec __user *uvector,
270+
unsigned long nr_segs, unsigned long fast_segs,
271+
struct iovec *fast_iov, bool compat);
272+
ssize_t import_iovec(int type, const struct iovec __user *uvec,
273+
unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
274+
struct iov_iter *i);
275+
ssize_t __import_iovec(int type, const struct iovec __user *uvec,
276+
unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
277+
struct iov_iter *i, bool compat);
272278

273279
#ifdef CONFIG_COMPAT
274280
struct compat_iovec;

0 commit comments

Comments
 (0)