Skip to content

Commit b837336

Browse files
JeffMoyertorvalds
authored andcommitted
compat: factor out compat_rw_copy_check_uvector from compat_do_readv_writev
It was reported in http://lkml.org/lkml/2010/3/8/309 that 32 bit readv and writev AIO operations were not functioning properly. It turns out that the code to convert the 32bit io vectors to 64 bits was never written. The results of that can be pretty bad, but in my testing, it mostly ended up in generating EFAULT as we walked off the list of I/O vectors provided. This patch set fixes the problem in my environment. are greatly appreciated. This patch: Factor out code that will be used by both compat_do_readv_writev and the compat aio submission code paths. Signed-off-by: Jeff Moyer <jmoyer@redhat.com> Reported-by: Michael Tokarev <mjt@tls.msk.ru> Cc: Zach Brown <zach.brown@oracle.com> Cc: <stable@kernel.org> [2.6.35.1] Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5b530fc commit b837336

2 files changed

Lines changed: 81 additions & 53 deletions

File tree

‎fs/compat.c‎

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,79 @@ compat_sys_io_getevents(aio_context_t ctx_id,
568568
return ret;
569569
}
570570

571+
/* A write operation does a read from user space and vice versa */
572+
#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
573+
574+
ssize_t compat_rw_copy_check_uvector(int type,
575+
const struct compat_iovec __user *uvector, unsigned long nr_segs,
576+
unsigned long fast_segs, struct iovec *fast_pointer,
577+
struct iovec **ret_pointer)
578+
{
579+
compat_ssize_t tot_len;
580+
struct iovec *iov = *ret_pointer = fast_pointer;
581+
ssize_t ret = 0;
582+
int seg;
583+
584+
/*
585+
* SuS says "The readv() function *may* fail if the iovcnt argument
586+
* was less than or equal to 0, or greater than {IOV_MAX}. Linux has
587+
* traditionally returned zero for zero segments, so...
588+
*/
589+
if (nr_segs == 0)
590+
goto out;
591+
592+
ret = -EINVAL;
593+
if (nr_segs > UIO_MAXIOV || nr_segs < 0)
594+
goto out;
595+
if (nr_segs > fast_segs) {
596+
ret = -ENOMEM;
597+
iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
598+
if (iov == NULL) {
599+
*ret_pointer = fast_pointer;
600+
goto out;
601+
}
602+
}
603+
*ret_pointer = iov;
604+
605+
/*
606+
* Single unix specification:
607+
* We should -EINVAL if an element length is not >= 0 and fitting an
608+
* ssize_t. The total length is fitting an ssize_t
609+
*
610+
* Be careful here because iov_len is a size_t not an ssize_t
611+
*/
612+
tot_len = 0;
613+
ret = -EINVAL;
614+
for (seg = 0; seg < nr_segs; seg++) {
615+
compat_ssize_t tmp = tot_len;
616+
compat_uptr_t buf;
617+
compat_ssize_t len;
618+
619+
if (__get_user(len, &uvector->iov_len) ||
620+
__get_user(buf, &uvector->iov_base)) {
621+
ret = -EFAULT;
622+
goto out;
623+
}
624+
if (len < 0) /* size_t not fitting in compat_ssize_t .. */
625+
goto out;
626+
tot_len += len;
627+
if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
628+
goto out;
629+
if (!access_ok(vrfy_dir(type), buf, len)) {
630+
ret = -EFAULT;
631+
goto out;
632+
}
633+
iov->iov_base = compat_ptr(buf);
634+
iov->iov_len = (compat_size_t) len;
635+
uvector++;
636+
iov++;
637+
}
638+
ret = tot_len;
639+
640+
out:
641+
return ret;
642+
}
643+
571644
static inline long
572645
copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
573646
{
@@ -1077,70 +1150,21 @@ static ssize_t compat_do_readv_writev(int type, struct file *file,
10771150
{
10781151
compat_ssize_t tot_len;
10791152
struct iovec iovstack[UIO_FASTIOV];
1080-
struct iovec *iov=iovstack, *vector;
1153+
struct iovec *iov;
10811154
ssize_t ret;
1082-
int seg;
10831155
io_fn_t fn;
10841156
iov_fn_t fnv;
10851157

1086-
/*
1087-
* SuS says "The readv() function *may* fail if the iovcnt argument
1088-
* was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1089-
* traditionally returned zero for zero segments, so...
1090-
*/
1091-
ret = 0;
1092-
if (nr_segs == 0)
1093-
goto out;
1094-
1095-
/*
1096-
* First get the "struct iovec" from user memory and
1097-
* verify all the pointers
1098-
*/
10991158
ret = -EINVAL;
1100-
if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1101-
goto out;
11021159
if (!file->f_op)
11031160
goto out;
1104-
if (nr_segs > UIO_FASTIOV) {
1105-
ret = -ENOMEM;
1106-
iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1107-
if (!iov)
1108-
goto out;
1109-
}
1161+
11101162
ret = -EFAULT;
11111163
if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
11121164
goto out;
11131165

1114-
/*
1115-
* Single unix specification:
1116-
* We should -EINVAL if an element length is not >= 0 and fitting an
1117-
* ssize_t. The total length is fitting an ssize_t
1118-
*
1119-
* Be careful here because iov_len is a size_t not an ssize_t
1120-
*/
1121-
tot_len = 0;
1122-
vector = iov;
1123-
ret = -EINVAL;
1124-
for (seg = 0 ; seg < nr_segs; seg++) {
1125-
compat_ssize_t tmp = tot_len;
1126-
compat_ssize_t len;
1127-
compat_uptr_t buf;
1128-
1129-
if (__get_user(len, &uvector->iov_len) ||
1130-
__get_user(buf, &uvector->iov_base)) {
1131-
ret = -EFAULT;
1132-
goto out;
1133-
}
1134-
if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1135-
goto out;
1136-
tot_len += len;
1137-
if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1138-
goto out;
1139-
vector->iov_base = compat_ptr(buf);
1140-
vector->iov_len = (compat_size_t) len;
1141-
uvector++;
1142-
vector++;
1143-
}
1166+
tot_len = compat_rw_copy_check_uvector(type, uvector, nr_segs,
1167+
UIO_FASTIOV, iovstack, &iov);
11441168
if (tot_len == 0) {
11451169
ret = 0;
11461170
goto out;

‎include/linux/compat.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,5 +356,9 @@ asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user * filename,
356356
asmlinkage long compat_sys_openat(unsigned int dfd, const char __user *filename,
357357
int flags, int mode);
358358

359+
extern ssize_t compat_rw_copy_check_uvector(int type,
360+
const struct compat_iovec __user *uvector, unsigned long nr_segs,
361+
unsigned long fast_segs, struct iovec *fast_pointer,
362+
struct iovec **ret_pointer);
359363
#endif /* CONFIG_COMPAT */
360364
#endif /* _LINUX_COMPAT_H */

0 commit comments

Comments
 (0)