Skip to content

Commit 9d85cba

Browse files
JeffMoyertorvalds
authored andcommitted
aio: fix the compat vectored operations
The aio compat code was not converting the struct iovecs from 32bit to 64bit pointers, causing either EINVAL to be returned from io_getevents, or EFAULT as the result of the I/O. This patch passes a compat flag to io_submit to signal that pointer conversion is necessary for a given iocb array. A variant of this was tested by Michael Tokarev. I have also updated the libaio test harness to exercise this code path with good success. Further, I grabbed a copy of ltp and ran the testcases/kernel/syscall/readv and writev tests there (compiled with -m32 on my 64bit system). All seems happy, but extra eyes on this would be welcome. [akpm@linux-foundation.org: coding-style fixes] [akpm@linux-foundation.org: fix CONFIG_COMPAT=n build] 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 b837336 commit 9d85cba

3 files changed

Lines changed: 47 additions & 25 deletions

File tree

‎fs/aio.c‎

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/blkdev.h>
3737
#include <linux/mempool.h>
3838
#include <linux/hash.h>
39+
#include <linux/compat.h>
3940

4041
#include <asm/kmap_types.h>
4142
#include <asm/uaccess.h>
@@ -1384,13 +1385,22 @@ static ssize_t aio_fsync(struct kiocb *iocb)
13841385
return ret;
13851386
}
13861387

1387-
static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
1388+
static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
13881389
{
13891390
ssize_t ret;
13901391

1391-
ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
1392-
kiocb->ki_nbytes, 1,
1393-
&kiocb->ki_inline_vec, &kiocb->ki_iovec);
1392+
#ifdef CONFIG_COMPAT
1393+
if (compat)
1394+
ret = compat_rw_copy_check_uvector(type,
1395+
(struct compat_iovec __user *)kiocb->ki_buf,
1396+
kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
1397+
&kiocb->ki_iovec);
1398+
else
1399+
#endif
1400+
ret = rw_copy_check_uvector(type,
1401+
(struct iovec __user *)kiocb->ki_buf,
1402+
kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
1403+
&kiocb->ki_iovec);
13941404
if (ret < 0)
13951405
goto out;
13961406

@@ -1420,7 +1430,7 @@ static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
14201430
* Performs the initial checks and aio retry method
14211431
* setup for the kiocb at the time of io submission.
14221432
*/
1423-
static ssize_t aio_setup_iocb(struct kiocb *kiocb)
1433+
static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
14241434
{
14251435
struct file *file = kiocb->ki_filp;
14261436
ssize_t ret = 0;
@@ -1469,7 +1479,7 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb)
14691479
ret = security_file_permission(file, MAY_READ);
14701480
if (unlikely(ret))
14711481
break;
1472-
ret = aio_setup_vectored_rw(READ, kiocb);
1482+
ret = aio_setup_vectored_rw(READ, kiocb, compat);
14731483
if (ret)
14741484
break;
14751485
ret = -EINVAL;
@@ -1483,7 +1493,7 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb)
14831493
ret = security_file_permission(file, MAY_WRITE);
14841494
if (unlikely(ret))
14851495
break;
1486-
ret = aio_setup_vectored_rw(WRITE, kiocb);
1496+
ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
14871497
if (ret)
14881498
break;
14891499
ret = -EINVAL;
@@ -1548,7 +1558,8 @@ static void aio_batch_free(struct hlist_head *batch_hash)
15481558
}
15491559

15501560
static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1551-
struct iocb *iocb, struct hlist_head *batch_hash)
1561+
struct iocb *iocb, struct hlist_head *batch_hash,
1562+
bool compat)
15521563
{
15531564
struct kiocb *req;
15541565
struct file *file;
@@ -1609,7 +1620,7 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
16091620
req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
16101621
req->ki_opcode = iocb->aio_lio_opcode;
16111622

1612-
ret = aio_setup_iocb(req);
1623+
ret = aio_setup_iocb(req, compat);
16131624

16141625
if (ret)
16151626
goto out_put_req;
@@ -1637,20 +1648,8 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
16371648
return ret;
16381649
}
16391650

1640-
/* sys_io_submit:
1641-
* Queue the nr iocbs pointed to by iocbpp for processing. Returns
1642-
* the number of iocbs queued. May return -EINVAL if the aio_context
1643-
* specified by ctx_id is invalid, if nr is < 0, if the iocb at
1644-
* *iocbpp[0] is not properly initialized, if the operation specified
1645-
* is invalid for the file descriptor in the iocb. May fail with
1646-
* -EFAULT if any of the data structures point to invalid data. May
1647-
* fail with -EBADF if the file descriptor specified in the first
1648-
* iocb is invalid. May fail with -EAGAIN if insufficient resources
1649-
* are available to queue any iocbs. Will return 0 if nr is 0. Will
1650-
* fail with -ENOSYS if not implemented.
1651-
*/
1652-
SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1653-
struct iocb __user * __user *, iocbpp)
1651+
long do_io_submit(aio_context_t ctx_id, long nr,
1652+
struct iocb __user *__user *iocbpp, bool compat)
16541653
{
16551654
struct kioctx *ctx;
16561655
long ret = 0;
@@ -1687,7 +1686,7 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
16871686
break;
16881687
}
16891688

1690-
ret = io_submit_one(ctx, user_iocb, &tmp, batch_hash);
1689+
ret = io_submit_one(ctx, user_iocb, &tmp, batch_hash, compat);
16911690
if (ret)
16921691
break;
16931692
}
@@ -1697,6 +1696,24 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
16971696
return i ? i : ret;
16981697
}
16991698

1699+
/* sys_io_submit:
1700+
* Queue the nr iocbs pointed to by iocbpp for processing. Returns
1701+
* the number of iocbs queued. May return -EINVAL if the aio_context
1702+
* specified by ctx_id is invalid, if nr is < 0, if the iocb at
1703+
* *iocbpp[0] is not properly initialized, if the operation specified
1704+
* is invalid for the file descriptor in the iocb. May fail with
1705+
* -EFAULT if any of the data structures point to invalid data. May
1706+
* fail with -EBADF if the file descriptor specified in the first
1707+
* iocb is invalid. May fail with -EAGAIN if insufficient resources
1708+
* are available to queue any iocbs. Will return 0 if nr is 0. Will
1709+
* fail with -ENOSYS if not implemented.
1710+
*/
1711+
SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1712+
struct iocb __user * __user *, iocbpp)
1713+
{
1714+
return do_io_submit(ctx_id, nr, iocbpp, 0);
1715+
}
1716+
17001717
/* lookup_kiocb
17011718
* Finds a given iocb for cancellation.
17021719
*/

‎fs/compat.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
673673
iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
674674
ret = copy_iocb(nr, iocb, iocb64);
675675
if (!ret)
676-
ret = sys_io_submit(ctx_id, nr, iocb64);
676+
ret = do_io_submit(ctx_id, nr, iocb64, 1);
677677
return ret;
678678
}
679679

‎include/linux/aio.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,18 @@ extern void kick_iocb(struct kiocb *iocb);
212212
extern int aio_complete(struct kiocb *iocb, long res, long res2);
213213
struct mm_struct;
214214
extern void exit_aio(struct mm_struct *mm);
215+
extern long do_io_submit(aio_context_t ctx_id, long nr,
216+
struct iocb __user *__user *iocbpp, bool compat);
215217
#else
216218
static inline ssize_t wait_on_sync_kiocb(struct kiocb *iocb) { return 0; }
217219
static inline int aio_put_req(struct kiocb *iocb) { return 0; }
218220
static inline void kick_iocb(struct kiocb *iocb) { }
219221
static inline int aio_complete(struct kiocb *iocb, long res, long res2) { return 0; }
220222
struct mm_struct;
221223
static inline void exit_aio(struct mm_struct *mm) { }
224+
static inline long do_io_submit(aio_context_t ctx_id, long nr,
225+
struct iocb __user * __user *iocbpp,
226+
bool compat) { return 0; }
222227
#endif /* CONFIG_AIO */
223228

224229
static inline struct kiocb *list_kiocb(struct list_head *h)

0 commit comments

Comments
 (0)