mirror of
https://github.com/torvalds/linux.git
synced 2026-11-20 13:03:48 +00:00
iov_iter: take an unsigned long segment count in import_iovec()
readv(), writev() and their positional variants, process_vm_readv(),
process_vm_writev(), vmsplice(), process_madvise(), vectored aio and
the btrfs encoded I/O ioctls all take a full-width segment count, and
all of them hand it to import_iovec() or __import_iovec(). Those take
an unsigned int count, so on 64-bit kernels the upper 32 bits are
dropped before iovec_from_user() checks the count against UIO_MAXIOV.
A count of 0x100000001 is therefore imported as one segment: writev()
on a pipe writes a byte and readv() reads it back, instead of both
failing with EINVAL. A count of 0x100000000 returns 0 as an empty
vector.
import_iovec() was added with an unsigned int count, but the helper it
wrapped, rw_copy_check_uvector(), took an unsigned long, as does
iovec_from_user() today. Until v4.1, readv(), writev(), preadv(),
pwritev(), process_vm_readv(), process_vm_writev() and vectored aio
therefore failed these counts with EINVAL, as did vmsplice() until
v4.18.
Widen nr_segs in both import helpers so the UIO_MAXIOV check sees the
count the caller passed. Callers with 32-bit counts are unaffected.
AI assistance was used to find the bug, develop the fix, and run the
reproducers.
Fixes: bc917be810 ("saner iov_iter initialization primitives")
Signed-off-by: Andrea Parri <[email protected]>
Link: https://patch.msgid.link/[email protected]
Reviewed-by: Jan Kara <[email protected]>
Signed-off-by: Christian Brauner (Amutable) <[email protected]>
This commit is contained in:
+4
-4
@@ -373,11 +373,11 @@ struct iovec *iovec_from_user(const struct iovec __user *uvector,
|
||||
unsigned long nr_segs, unsigned long fast_segs,
|
||||
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);
|
||||
unsigned long nr_segs, unsigned int 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);
|
||||
unsigned long nr_segs, unsigned int fast_segs,
|
||||
struct iovec **iovp, struct iov_iter *i, bool compat);
|
||||
int import_ubuf(int type, void __user *buf, size_t len, struct iov_iter *i);
|
||||
|
||||
static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction,
|
||||
|
||||
+3
-3
@@ -1382,8 +1382,8 @@ static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec,
|
||||
}
|
||||
|
||||
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)
|
||||
unsigned long nr_segs, unsigned int fast_segs,
|
||||
struct iovec **iovp, struct iov_iter *i, bool compat)
|
||||
{
|
||||
ssize_t total_len = 0;
|
||||
unsigned long seg;
|
||||
@@ -1454,7 +1454,7 @@ ssize_t __import_iovec(int type, const struct iovec __user *uvec,
|
||||
* Return: Negative error code on error, bytes imported on success
|
||||
*/
|
||||
ssize_t import_iovec(int type, const struct iovec __user *uvec,
|
||||
unsigned nr_segs, unsigned fast_segs,
|
||||
unsigned long nr_segs, unsigned int fast_segs,
|
||||
struct iovec **iovp, struct iov_iter *i)
|
||||
{
|
||||
return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i,
|
||||
|
||||
Reference in New Issue
Block a user