io-uring.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
@ 2020-02-05 15:46 Pavel Begunkov
  2020-02-05 15:54 ` Jens Axboe
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Begunkov @ 2020-02-05 15:46 UTC (permalink / raw)
  To: Jens Axboe, io-uring, linux-kernel

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.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 fs/io_uring.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index edb00ae2619b..f00c2c9c67c0 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -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,
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
  2020-02-05 15:46 [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE} Pavel Begunkov
@ 2020-02-05 15:54 ` Jens Axboe
  2020-02-05 16:02   ` Pavel Begunkov
  0 siblings, 1 reply; 8+ messages in thread
From: Jens Axboe @ 2020-02-05 15:54 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring, linux-kernel

On 2/5/20 8:46 AM, Pavel Begunkov wrote:
> 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.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
  2020-02-05 15:54 ` Jens Axboe
@ 2020-02-05 16:02   ` Pavel Begunkov
  2020-02-05 16:05     ` Jens Axboe
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Begunkov @ 2020-02-05 16:02 UTC (permalink / raw)
  To: Jens Axboe, io-uring, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 577 bytes --]

On 05/02/2020 18:54, Jens Axboe wrote:
> On 2/5/20 8:46 AM, Pavel Begunkov wrote:
>> 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.

-- 
Pavel Begunkov


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
  2020-02-05 16:02   ` Pavel Begunkov
@ 2020-02-05 16:05     ` Jens Axboe
  2020-02-05 16:16       ` Jens Axboe
  0 siblings, 1 reply; 8+ messages in thread
From: Jens Axboe @ 2020-02-05 16:05 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring, linux-kernel

On 2/5/20 9:02 AM, Pavel Begunkov wrote:
> On 05/02/2020 18:54, Jens Axboe wrote:
>> On 2/5/20 8:46 AM, Pavel Begunkov wrote:
>>> 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.

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

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
  2020-02-05 16:05     ` Jens Axboe
@ 2020-02-05 16:16       ` Jens Axboe
  2020-02-05 16:50         ` Pavel Begunkov
  0 siblings, 1 reply; 8+ messages in thread
From: Jens Axboe @ 2020-02-05 16:16 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring, linux-kernel

On 2/5/20 9:05 AM, Jens Axboe wrote:
> On 2/5/20 9:02 AM, Pavel Begunkov wrote:
>> On 05/02/2020 18:54, Jens Axboe wrote:
>>> On 2/5/20 8:46 AM, Pavel Begunkov wrote:
>>>> 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.
> 
> 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.


-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
  2020-02-05 16:16       ` Jens Axboe
@ 2020-02-05 16:50         ` Pavel Begunkov
  2020-02-05 16:52           ` Jens Axboe
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Begunkov @ 2020-02-05 16:50 UTC (permalink / raw)
  To: Jens Axboe, io-uring, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 1792 bytes --]

On 05/02/2020 19:16, Jens Axboe wrote:
> On 2/5/20 9:05 AM, Jens Axboe wrote:
>> On 2/5/20 9:02 AM, Pavel Begunkov wrote:
>>> On 05/02/2020 18:54, Jens Axboe wrote:
>>>> On 2/5/20 8:46 AM, Pavel Begunkov wrote:
>>>>> 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.
>>
>> 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.
> 
> 
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?


-- 
Pavel Begunkov


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
  2020-02-05 16:50         ` Pavel Begunkov
@ 2020-02-05 16:52           ` Jens Axboe
  2020-02-05 16:59             ` Pavel Begunkov
  0 siblings, 1 reply; 8+ messages in thread
From: Jens Axboe @ 2020-02-05 16:52 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring, linux-kernel

On 2/5/20 9:50 AM, Pavel Begunkov wrote:
> On 05/02/2020 19:16, Jens Axboe wrote:
>> On 2/5/20 9:05 AM, Jens Axboe wrote:
>>> On 2/5/20 9:02 AM, Pavel Begunkov wrote:
>>>> On 05/02/2020 18:54, Jens Axboe wrote:
>>>>> On 2/5/20 8:46 AM, Pavel Begunkov wrote:
>>>>>> 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.
>>>
>>> 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.
>>
>>
> 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.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE}
  2020-02-05 16:52           ` Jens Axboe
@ 2020-02-05 16:59             ` Pavel Begunkov
  0 siblings, 0 replies; 8+ messages in thread
From: Pavel Begunkov @ 2020-02-05 16:59 UTC (permalink / raw)
  To: Jens Axboe, io-uring, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 2078 bytes --]

On 05/02/2020 19:52, Jens Axboe wrote:
> On 2/5/20 9:50 AM, Pavel Begunkov wrote:
>> On 05/02/2020 19:16, Jens Axboe wrote:
>>> On 2/5/20 9:05 AM, Jens Axboe wrote:
>>>> On 2/5/20 9:02 AM, Pavel Begunkov wrote:
>>>>> On 05/02/2020 18:54, Jens Axboe wrote:
>>>>>> On 2/5/20 8:46 AM, Pavel Begunkov wrote:
>>>>>>> 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.
>>>>
>>>> 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.
>>>
>>>
>> 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.

-- 
Pavel Begunkov


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-02-05 17:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-05 15:46 [PATCH] io_uring: fix mm use with IORING_OP_{READ,WRITE} Pavel Begunkov
2020-02-05 15:54 ` Jens Axboe
2020-02-05 16:02   ` Pavel Begunkov
2020-02-05 16:05     ` Jens Axboe
2020-02-05 16:16       ` Jens Axboe
2020-02-05 16:50         ` Pavel Begunkov
2020-02-05 16:52           ` Jens Axboe
2020-02-05 16:59             ` Pavel Begunkov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).