Skip to content

Commit 7f13657

Browse files
Xiaoguang Wangaxboe
authored andcommitted
io_uring: handle -EFAULT properly in io_uring_setup()
If copy_to_user() in io_uring_setup() failed, we'll leak many kernel resources, which will be recycled until process terminates. This bug can be reproduced by using mprotect to set params to PROT_READ. To fix this issue, refactor io_uring_create() a bit to add a new 'struct io_uring_params __user *params' parameter and move the copy_to_user() in io_uring_setup() to io_uring_setup(), if copy_to_user() failed, we can free kernel resource properly. Suggested-by: Jens Axboe <[email protected]> Signed-off-by: Xiaoguang Wang <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent d8f1b97 commit 7f13657

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

fs/io_uring.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7760,7 +7760,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
77607760
return ret;
77617761
}
77627762

7763-
static int io_uring_create(unsigned entries, struct io_uring_params *p)
7763+
static int io_uring_create(unsigned entries, struct io_uring_params *p,
7764+
struct io_uring_params __user *params)
77647765
{
77657766
struct user_struct *user = NULL;
77667767
struct io_ring_ctx *ctx;
@@ -7852,6 +7853,14 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
78527853
p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
78537854
p->cq_off.cqes = offsetof(struct io_rings, cqes);
78547855

7856+
p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7857+
IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7858+
IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
7859+
7860+
if (copy_to_user(params, p, sizeof(*p))) {
7861+
ret = -EFAULT;
7862+
goto err;
7863+
}
78557864
/*
78567865
* Install ring fd as the very last thing, so we don't risk someone
78577866
* having closed it before we finish setup
@@ -7860,9 +7869,6 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
78607869
if (ret < 0)
78617870
goto err;
78627871

7863-
p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
7864-
IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7865-
IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
78667872
trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
78677873
return ret;
78687874
err:
@@ -7878,7 +7884,6 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
78787884
static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
78797885
{
78807886
struct io_uring_params p;
7881-
long ret;
78827887
int i;
78837888

78847889
if (copy_from_user(&p, params, sizeof(p)))
@@ -7893,14 +7898,7 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
78937898
IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
78947899
return -EINVAL;
78957900

7896-
ret = io_uring_create(entries, &p);
7897-
if (ret < 0)
7898-
return ret;
7899-
7900-
if (copy_to_user(params, &p, sizeof(p)))
7901-
return -EFAULT;
7902-
7903-
return ret;
7901+
return io_uring_create(entries, &p, params);
79047902
}
79057903

79067904
SYSCALL_DEFINE2(io_uring_setup, u32, entries,

0 commit comments

Comments
 (0)