Skip to contentSkip to conversation

io_uring: fix mm use with IORING_OP_{READ,WRITE}

PATCH In review Pavel Begunkov 2 participants 8 messages Feb 5, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring: fix mm use with IORING_OP_{READ,WRITE}

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

---
 fs/io_uring.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
fs/io_uring.c +7 -7
index edb00ae2619b..f00c2c9c67c0 100644
@@ -2038,13 +2038,6 @@ static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
 	if (req->rw.kiocb.private)
 		return -EINVAL;
 
-	if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
-		ssize_t ret;
-		ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
-		*iovec = NULL;
-		return ret;
-	}
-
 	if (req->io) {
 		struct io_async_rw *iorw = &req->io->rw;
 
@@ -2058,6 +2051,13 @@ static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
 	if (!req->has_user)
 		return -EFAULT;
 
+	if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
+		ssize_t ret;
+		ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
+		*iovec = NULL;
+		return ret;
+	}
+
 #ifdef CONFIG_COMPAT
 	if (req->ctx->compat)
 		return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
Signature · 1 line
2.24.0
maintainer
#1 lore raw
Quoted 3 lines · Pavel Begunkov

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

I'd need to double check, but I think the has_user check should just go. The import checks for access anyway, so we'll -EFAULT there if we somehow messed up and didn't acquire the right mm.

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Pavel Begunkov

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

I'd need to double check, but I think the has_user check should just go. The import checks for access anyway, so we'll -EFAULT there if we somehow messed up and didn't acquire the right mm.

It'd be even better. I have plans to remove it, but I was thinking from a different angle.

Signature · 1 line
Pavel Begunkov
maintainer
#3 lore raw
Quoted 6 lines · Pavel Begunkov
Quoted 5 lines · Jens Axboe
Quoted 3 lines · Pavel Begunkov

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

Quoted 6 lines
Quoted 4 lines

I'd need to double check, but I think the has_user check should just go. The import checks for access anyway, so we'll -EFAULT there if we somehow messed up and didn't acquire the right mm.

It'd be even better. I have plans to remove it, but I was thinking from a different angle.

Let me just confirm it in practice, but it should be fine. Then we can just kill it.

Signature · 1 line
Jens Axboe
maintainer
#4 lore raw
Quoted 11 lines · Jens Axboe
Quoted 10 lines · Pavel Begunkov
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Pavel Begunkov

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

I'd need to double check, but I think the has_user check should just go. The import checks for access anyway, so we'll -EFAULT there if we somehow messed up and didn't acquire the right mm.

Quoted 5 lines
Quoted 2 lines

It'd be even better. I have plans to remove it, but I was thinking from a different angle.

Let me just confirm it in practice, but it should be fine. Then we can just kill it.

OK now I remember - in terms of mm it's fine, we'll do the right thing. But the iov_iter_init() has this gem:

	/* It will get better.  Eventually... */
	if (uaccess_kernel()) {
		i->type = ITER_KVEC | direction;
		i->kvec = (struct kvec *)iov;
	} else {
		i->type = ITER_IOVEC | direction;
		i->iov = iov;
	}

which means that if we haven't set USER_DS, then iov_iter_init() will magically set the type to ITER_KVEC which then crashes when the iterator tries to copy.

Which is pretty lame. How about a patch that just checks for uaccess_kernel() and -EFAULTs if true for the non-fixed variants where we don't init the iter ourselves? Then we can still kill req->has_user and not have to fill it in.

Signature · 1 line
Jens Axboe
#5 lore raw
Quoted 30 lines · Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 10 lines · Pavel Begunkov
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Pavel Begunkov

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

I'd need to double check, but I think the has_user check should just go. The import checks for access anyway, so we'll -EFAULT there if we somehow messed up and didn't acquire the right mm.

Quoted 5 lines
Quoted 2 lines

It'd be even better. I have plans to remove it, but I was thinking from a different angle.

Let me just confirm it in practice, but it should be fine. Then we can just kill it.

OK now I remember - in terms of mm it's fine, we'll do the right thing. But the iov_iter_init() has this gem:

	/* It will get better.  Eventually... */
	if (uaccess_kernel()) {
		i->type = ITER_KVEC | direction;
		i->kvec = (struct kvec *)iov;
	} else {
		i->type = ITER_IOVEC | direction;
		i->iov = iov;
	}
Quoted 10 lines

which means that if we haven't set USER_DS, then iov_iter_init() will magically set the type to ITER_KVEC which then crashes when the iterator tries to copy.

Which is pretty lame. How about a patch that just checks for uaccess_kernel() and -EFAULTs if true for the non-fixed variants where we don't init the iter ourselves? Then we can still kill req->has_user and not have to fill it in.

On the other hand, we don't send requests async without @mm. So, if we fail them whenever can't grab mm, it solves all the problems even without extra checks. What do you think?

Signature · 1 line
Pavel Begunkov
maintainer
#6 lore raw
Quoted 41 lines · Pavel Begunkov
Quoted 30 lines · Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 10 lines · Pavel Begunkov
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Pavel Begunkov

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

I'd need to double check, but I think the has_user check should just go. The import checks for access anyway, so we'll -EFAULT there if we somehow messed up and didn't acquire the right mm.

Quoted 5 lines
Quoted 2 lines

It'd be even better. I have plans to remove it, but I was thinking from a different angle.

Let me just confirm it in practice, but it should be fine. Then we can just kill it.

OK now I remember - in terms of mm it's fine, we'll do the right thing. But the iov_iter_init() has this gem:

	/* It will get better.  Eventually... */
	if (uaccess_kernel()) {
		i->type = ITER_KVEC | direction;
		i->kvec = (struct kvec *)iov;
	} else {
		i->type = ITER_IOVEC | direction;
		i->iov = iov;
	}
Quoted 10 lines

which means that if we haven't set USER_DS, then iov_iter_init() will magically set the type to ITER_KVEC which then crashes when the iterator tries to copy.

Which is pretty lame. How about a patch that just checks for uaccess_kernel() and -EFAULTs if true for the non-fixed variants where we don't init the iter ourselves? Then we can still kill req->has_user and not have to fill it in.

Quoted 3 lines

On the other hand, we don't send requests async without @mm. So, if we fail them whenever can't grab mm, it solves all the problems even without extra checks. What do you think?

I agree, the check is/was just there as a safe guard, it's not really needed.

Signature · 1 line
Jens Axboe
#7 lore raw
Quoted 42 lines · Jens Axboe
Quoted 41 lines · Pavel Begunkov
Quoted 40 lines · Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 10 lines · Pavel Begunkov
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Pavel Begunkov

IORING_OP_{READ,WRITE} need mm to access user buffers, hence req->has_user check should go for them as well. Move the corresponding imports past the check.

I'd need to double check, but I think the has_user check should just go. The import checks for access anyway, so we'll -EFAULT there if we somehow messed up and didn't acquire the right mm.

Quoted 5 lines
Quoted 2 lines

It'd be even better. I have plans to remove it, but I was thinking from a different angle.

Let me just confirm it in practice, but it should be fine. Then we can just kill it.

OK now I remember - in terms of mm it's fine, we'll do the right thing. But the iov_iter_init() has this gem:

	/* It will get better.  Eventually... */
	if (uaccess_kernel()) {
		i->type = ITER_KVEC | direction;
		i->kvec = (struct kvec *)iov;
	} else {
		i->type = ITER_IOVEC | direction;
		i->iov = iov;
	}

which means that if we haven't set USER_DS, then iov_iter_init() will magically set the type to ITER_KVEC which then crashes when the iterator tries to copy.

Which is pretty lame. How about a patch that just checks for uaccess_kernel() and -EFAULTs if true for the non-fixed variants where we don't init the iter ourselves? Then we can still kill req->has_user and not have to fill it in.

Quoted 7 lines
Quoted 3 lines

On the other hand, we don't send requests async without @mm. So, if we fail them whenever can't grab mm, it solves all the problems even without extra checks. What do you think?

I agree, the check is/was just there as a safe guard, it's not really needed.

Cool, I'll deal with it.

Signature · 1 line
Pavel Begunkov