Commit 7f13657d authored by Xiaoguang Wang's avatar Xiaoguang Wang Committed by Jens Axboe

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: default avatarJens Axboe <axboe@kernel.dk>
Signed-off-by: default avatarXiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent d8f1b971
...@@ -7760,7 +7760,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx) ...@@ -7760,7 +7760,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
return ret; return ret;
} }
static int io_uring_create(unsigned entries, struct io_uring_params *p) static int io_uring_create(unsigned entries, struct io_uring_params *p,
struct io_uring_params __user *params)
{ {
struct user_struct *user = NULL; struct user_struct *user = NULL;
struct io_ring_ctx *ctx; struct io_ring_ctx *ctx;
...@@ -7852,6 +7853,14 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p) ...@@ -7852,6 +7853,14 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
p->cq_off.cqes = offsetof(struct io_rings, cqes); p->cq_off.cqes = offsetof(struct io_rings, cqes);
p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
if (copy_to_user(params, p, sizeof(*p))) {
ret = -EFAULT;
goto err;
}
/* /*
* Install ring fd as the very last thing, so we don't risk someone * Install ring fd as the very last thing, so we don't risk someone
* having closed it before we finish setup * having closed it before we finish setup
...@@ -7860,9 +7869,6 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p) ...@@ -7860,9 +7869,6 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
if (ret < 0) if (ret < 0)
goto err; goto err;
p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
return ret; return ret;
err: err:
...@@ -7878,7 +7884,6 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p) ...@@ -7878,7 +7884,6 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
static long io_uring_setup(u32 entries, struct io_uring_params __user *params) static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
{ {
struct io_uring_params p; struct io_uring_params p;
long ret;
int i; int i;
if (copy_from_user(&p, params, sizeof(p))) 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) ...@@ -7893,14 +7898,7 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
return -EINVAL; return -EINVAL;
ret = io_uring_create(entries, &p); return io_uring_create(entries, &p, params);
if (ret < 0)
return ret;
if (copy_to_user(params, &p, sizeof(p)))
return -EFAULT;
return ret;
} }
SYSCALL_DEFINE2(io_uring_setup, u32, entries, SYSCALL_DEFINE2(io_uring_setup, u32, entries,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment