public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH] io_uring: handle -EFAULT properly in io_uring_setup()
@ 2020-05-04 13:53 Xiaoguang Wang
  2020-05-04 15:14 ` Jens Axboe
  2020-05-04 15:40 ` Pavel Begunkov
  0 siblings, 2 replies; 5+ messages in thread
From: Xiaoguang Wang @ 2020-05-04 13:53 UTC (permalink / raw)
  To: io-uring; +Cc: axboe, joseph.qi, Xiaoguang Wang

If copy_to_user() in io_uring_setup() failed, we'll leak many kernel
resources, which could be reproduced by using mprotect to set params
to PROT_READ. To fix this issue, refactor io_uring_create() a bit to
let it return 'struct io_ring_ctx *', then when copy_to_user() failed,
we can free kernel resource properly.

Signed-off-by: Xiaoguang Wang <[email protected]>
---
 fs/io_uring.c | 45 ++++++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 0b91b0631173..a19885dee621 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -7761,7 +7761,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
 	return ret;
 }
 
-static int io_uring_create(unsigned entries, struct io_uring_params *p)
+static struct io_ring_ctx *io_uring_create(unsigned entries,
+				struct io_uring_params *p)
 {
 	struct user_struct *user = NULL;
 	struct io_ring_ctx *ctx;
@@ -7769,10 +7770,10 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
 	int ret;
 
 	if (!entries)
-		return -EINVAL;
+		return ERR_PTR(-EINVAL);
 	if (entries > IORING_MAX_ENTRIES) {
 		if (!(p->flags & IORING_SETUP_CLAMP))
-			return -EINVAL;
+			return ERR_PTR(-EINVAL);
 		entries = IORING_MAX_ENTRIES;
 	}
 
@@ -7792,10 +7793,10 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
 		 * any cq vs sq ring sizing.
 		 */
 		if (p->cq_entries < p->sq_entries)
-			return -EINVAL;
+			return ERR_PTR(-EINVAL);
 		if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
 			if (!(p->flags & IORING_SETUP_CLAMP))
-				return -EINVAL;
+				return ERR_PTR(-EINVAL);
 			p->cq_entries = IORING_MAX_CQ_ENTRIES;
 		}
 		p->cq_entries = roundup_pow_of_two(p->cq_entries);
@@ -7811,7 +7812,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
 				ring_pages(p->sq_entries, p->cq_entries));
 		if (ret) {
 			free_uid(user);
-			return ret;
+			return ERR_PTR(ret);
 		}
 	}
 
@@ -7821,7 +7822,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
 			io_unaccount_mem(user, ring_pages(p->sq_entries,
 								p->cq_entries));
 		free_uid(user);
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 	}
 	ctx->compat = in_compat_syscall();
 	ctx->account_mem = account_mem;
@@ -7853,22 +7854,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.cqes = offsetof(struct io_rings, cqes);
 
-	/*
-	 * Install ring fd as the very last thing, so we don't risk someone
-	 * having closed it before we finish setup
-	 */
-	ret = io_uring_get_fd(ctx);
-	if (ret < 0)
-		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);
-	return ret;
+	return ctx;
 err:
 	io_ring_ctx_wait_and_kill(ctx);
-	return ret;
+	return ERR_PTR(ret);
 }
 
 /*
@@ -7878,6 +7871,7 @@ 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)
 {
+	struct io_ring_ctx *ctx;
 	struct io_uring_params p;
 	long ret;
 	int i;
@@ -7894,12 +7888,21 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
 			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
 		return -EINVAL;
 
-	ret = io_uring_create(entries, &p);
-	if (ret < 0)
-		return ret;
+	ctx = io_uring_create(entries, &p);
+	if (IS_ERR(ctx))
+		return PTR_ERR(ctx);
 
-	if (copy_to_user(params, &p, sizeof(p)))
+	if (copy_to_user(params, &p, sizeof(p))) {
+		io_ring_ctx_wait_and_kill(ctx);
 		return -EFAULT;
+	}
+	/*
+	 * Install ring fd as the very last thing, so we don't risk someone
+	 * having closed it before we finish setup
+	 */
+	ret = io_uring_get_fd(ctx);
+	if (ret < 0)
+		io_ring_ctx_wait_and_kill(ctx);
 
 	return ret;
 }
-- 
2.17.2


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

* Re: [PATCH] io_uring: handle -EFAULT properly in io_uring_setup()
  2020-05-04 13:53 [PATCH] io_uring: handle -EFAULT properly in io_uring_setup() Xiaoguang Wang
@ 2020-05-04 15:14 ` Jens Axboe
  2020-05-04 15:40 ` Pavel Begunkov
  1 sibling, 0 replies; 5+ messages in thread
From: Jens Axboe @ 2020-05-04 15:14 UTC (permalink / raw)
  To: Xiaoguang Wang, io-uring; +Cc: joseph.qi

On 5/4/20 7:53 AM, Xiaoguang Wang wrote:
> If copy_to_user() in io_uring_setup() failed, we'll leak many kernel
> resources, which could be reproduced by using mprotect to set params
> to PROT_READ. To fix this issue, refactor io_uring_create() a bit to
> let it return 'struct io_ring_ctx *', then when copy_to_user() failed,
> we can free kernel resource properly.

Might be simpler to just pass in the __user pointer for the copy,
ala the below. Totally untested...


diff --git a/fs/io_uring.c b/fs/io_uring.c
index 6d52ff98279d..70361f588c5b 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -7760,7 +7760,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
 	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 io_ring_ctx *ctx;
@@ -7863,6 +7864,12 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
 	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;
+	}
+
 	trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
 	return ret;
 err:
@@ -7878,7 +7885,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)
 {
 	struct io_uring_params p;
-	long ret;
 	int i;
 
 	if (copy_from_user(&p, params, sizeof(p)))
@@ -7893,14 +7899,7 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
 			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
 		return -EINVAL;
 
-	ret = io_uring_create(entries, &p);
-	if (ret < 0)
-		return ret;
-
-	if (copy_to_user(params, &p, sizeof(p)))
-		return -EFAULT;
-
-	return ret;
+	return io_uring_create(entries, &p, params);
 }
 
 SYSCALL_DEFINE2(io_uring_setup, u32, entries,

-- 
Jens Axboe


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

* Re: [PATCH] io_uring: handle -EFAULT properly in io_uring_setup()
  2020-05-04 13:53 [PATCH] io_uring: handle -EFAULT properly in io_uring_setup() Xiaoguang Wang
  2020-05-04 15:14 ` Jens Axboe
@ 2020-05-04 15:40 ` Pavel Begunkov
  2020-05-04 15:53   ` Jens Axboe
  2020-05-05  2:59   ` Xiaoguang Wang
  1 sibling, 2 replies; 5+ messages in thread
From: Pavel Begunkov @ 2020-05-04 15:40 UTC (permalink / raw)
  To: Xiaoguang Wang, io-uring; +Cc: axboe, joseph.qi

On 04/05/2020 16:53, Xiaoguang Wang wrote:
> If copy_to_user() in io_uring_setup() failed, we'll leak many kernel
> resources, which could be reproduced by using mprotect to set params

At least it recycles everything upon killing the process, so that's rather not
notifying a user about a successfully installed fd. Good catch


> to PROT_READ. To fix this issue, refactor io_uring_create() a bit to
> let it return 'struct io_ring_ctx *', then when copy_to_user() failed,
> we can free kernel resource properly.
> 
> Signed-off-by: Xiaoguang Wang <[email protected]>
> ---
>  fs/io_uring.c | 45 ++++++++++++++++++++++++---------------------
>  1 file changed, 24 insertions(+), 21 deletions(-)
> 
> diff --git a/fs/io_uring.c b/fs/io_uring.c
> index 0b91b0631173..a19885dee621 100644
> --- a/fs/io_uring.c
> +++ b/fs/io_uring.c
> @@ -7761,7 +7761,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
>  	return ret;
>  }
>  
> -static int io_uring_create(unsigned entries, struct io_uring_params *p)
> +static struct io_ring_ctx *io_uring_create(unsigned entries,
> +				struct io_uring_params *p)
>  {
>  	struct user_struct *user = NULL;
>  	struct io_ring_ctx *ctx;
> @@ -7769,10 +7770,10 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>  	int ret;
>  
>  	if (!entries)
> -		return -EINVAL;
> +		return ERR_PTR(-EINVAL);
>  	if (entries > IORING_MAX_ENTRIES) {
>  		if (!(p->flags & IORING_SETUP_CLAMP))
> -			return -EINVAL;
> +			return ERR_PTR(-EINVAL);
>  		entries = IORING_MAX_ENTRIES;
>  	}
>  
> @@ -7792,10 +7793,10 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>  		 * any cq vs sq ring sizing.
>  		 */
>  		if (p->cq_entries < p->sq_entries)
> -			return -EINVAL;
> +			return ERR_PTR(-EINVAL);
>  		if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
>  			if (!(p->flags & IORING_SETUP_CLAMP))
> -				return -EINVAL;
> +				return ERR_PTR(-EINVAL);
>  			p->cq_entries = IORING_MAX_CQ_ENTRIES;
>  		}
>  		p->cq_entries = roundup_pow_of_two(p->cq_entries);
> @@ -7811,7 +7812,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>  				ring_pages(p->sq_entries, p->cq_entries));
>  		if (ret) {
>  			free_uid(user);
> -			return ret;
> +			return ERR_PTR(ret);
>  		}
>  	}
>  
> @@ -7821,7 +7822,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>  			io_unaccount_mem(user, ring_pages(p->sq_entries,
>  								p->cq_entries));
>  		free_uid(user);
> -		return -ENOMEM;
> +		return ERR_PTR(-ENOMEM);
>  	}
>  	ctx->compat = in_compat_syscall();
>  	ctx->account_mem = account_mem;
> @@ -7853,22 +7854,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.cqes = offsetof(struct io_rings, cqes);
>  
> -	/*
> -	 * Install ring fd as the very last thing, so we don't risk someone
> -	 * having closed it before we finish setup
> -	 */
> -	ret = io_uring_get_fd(ctx);
> -	if (ret < 0)
> -		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);
> -	return ret;
> +	return ctx;
>  err:
>  	io_ring_ctx_wait_and_kill(ctx);
> -	return ret;
> +	return ERR_PTR(ret);
>  }
>  
>  /*
> @@ -7878,6 +7871,7 @@ 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)
>  {
> +	struct io_ring_ctx *ctx;
>  	struct io_uring_params p;
>  	long ret;
>  	int i;
> @@ -7894,12 +7888,21 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
>  			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
>  		return -EINVAL;
>  
> -	ret = io_uring_create(entries, &p);
> -	if (ret < 0)
> -		return ret;
> +	ctx = io_uring_create(entries, &p);
> +	if (IS_ERR(ctx))
> +		return PTR_ERR(ctx);
>  
> -	if (copy_to_user(params, &p, sizeof(p)))
> +	if (copy_to_user(params, &p, sizeof(p))) {
> +		io_ring_ctx_wait_and_kill(ctx);
>  		return -EFAULT;
> +	}
> +	/*
> +	 * Install ring fd as the very last thing, so we don't risk someone
> +	 * having closed it before we finish setup
> +	 */
> +	ret = io_uring_get_fd(ctx);
> +	if (ret < 0)
> +		io_ring_ctx_wait_and_kill(ctx);
>  
>  	return ret;
>  }
> 

-- 
Pavel Begunkov

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

* Re: [PATCH] io_uring: handle -EFAULT properly in io_uring_setup()
  2020-05-04 15:40 ` Pavel Begunkov
@ 2020-05-04 15:53   ` Jens Axboe
  2020-05-05  2:59   ` Xiaoguang Wang
  1 sibling, 0 replies; 5+ messages in thread
From: Jens Axboe @ 2020-05-04 15:53 UTC (permalink / raw)
  To: Pavel Begunkov, Xiaoguang Wang, io-uring; +Cc: joseph.qi

On 5/4/20 9:40 AM, Pavel Begunkov wrote:
> On 04/05/2020 16:53, Xiaoguang Wang wrote:
>> If copy_to_user() in io_uring_setup() failed, we'll leak many kernel
>> resources, which could be reproduced by using mprotect to set params
> 
> At least it recycles everything upon killing the process, so that's rather not
> notifying a user about a successfully installed fd. Good catch

Let me revise the simpler version, so we error before doing the fd install
at least. Still untested...


diff --git a/fs/io_uring.c b/fs/io_uring.c
index 6d52ff98279d..8eea54197489 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -7760,7 +7760,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
 	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 io_ring_ctx *ctx;
@@ -7852,6 +7853,15 @@ 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.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
 	 * having closed it before we finish setup
@@ -7860,9 +7870,6 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
 	if (ret < 0)
 		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);
 	return ret;
 err:
@@ -7878,7 +7885,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)
 {
 	struct io_uring_params p;
-	long ret;
 	int i;
 
 	if (copy_from_user(&p, params, sizeof(p)))
@@ -7893,14 +7899,7 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
 			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
 		return -EINVAL;
 
-	ret = io_uring_create(entries, &p);
-	if (ret < 0)
-		return ret;
-
-	if (copy_to_user(params, &p, sizeof(p)))
-		return -EFAULT;
-
-	return ret;
+	return io_uring_create(entries, &p, params);
 }
 
 SYSCALL_DEFINE2(io_uring_setup, u32, entries,

-- 
Jens Axboe


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

* Re: [PATCH] io_uring: handle -EFAULT properly in io_uring_setup()
  2020-05-04 15:40 ` Pavel Begunkov
  2020-05-04 15:53   ` Jens Axboe
@ 2020-05-05  2:59   ` Xiaoguang Wang
  1 sibling, 0 replies; 5+ messages in thread
From: Xiaoguang Wang @ 2020-05-05  2:59 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring; +Cc: axboe, joseph.qi

hi,

> On 04/05/2020 16:53, Xiaoguang Wang wrote:
>> If copy_to_user() in io_uring_setup() failed, we'll leak many kernel
>> resources, which could be reproduced by using mprotect to set params
> 
> At least it recycles everything upon killing the process, so that's rather not
> notifying a user about a successfully installed fd. Good catch
Yeah, I'll add more explanation in commit message, thanks.

Regards,
Xiaoguang Wnag
> 
> 
>> to PROT_READ. To fix this issue, refactor io_uring_create() a bit to
>> let it return 'struct io_ring_ctx *', then when copy_to_user() failed,
>> we can free kernel resource properly.
>>
>> Signed-off-by: Xiaoguang Wang <[email protected]>
>> ---
>>   fs/io_uring.c | 45 ++++++++++++++++++++++++---------------------
>>   1 file changed, 24 insertions(+), 21 deletions(-)
>>
>> diff --git a/fs/io_uring.c b/fs/io_uring.c
>> index 0b91b0631173..a19885dee621 100644
>> --- a/fs/io_uring.c
>> +++ b/fs/io_uring.c
>> @@ -7761,7 +7761,8 @@ static int io_uring_get_fd(struct io_ring_ctx *ctx)
>>   	return ret;
>>   }
>>   
>> -static int io_uring_create(unsigned entries, struct io_uring_params *p)
>> +static struct io_ring_ctx *io_uring_create(unsigned entries,
>> +				struct io_uring_params *p)
>>   {
>>   	struct user_struct *user = NULL;
>>   	struct io_ring_ctx *ctx;
>> @@ -7769,10 +7770,10 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>>   	int ret;
>>   
>>   	if (!entries)
>> -		return -EINVAL;
>> +		return ERR_PTR(-EINVAL);
>>   	if (entries > IORING_MAX_ENTRIES) {
>>   		if (!(p->flags & IORING_SETUP_CLAMP))
>> -			return -EINVAL;
>> +			return ERR_PTR(-EINVAL);
>>   		entries = IORING_MAX_ENTRIES;
>>   	}
>>   
>> @@ -7792,10 +7793,10 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>>   		 * any cq vs sq ring sizing.
>>   		 */
>>   		if (p->cq_entries < p->sq_entries)
>> -			return -EINVAL;
>> +			return ERR_PTR(-EINVAL);
>>   		if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
>>   			if (!(p->flags & IORING_SETUP_CLAMP))
>> -				return -EINVAL;
>> +				return ERR_PTR(-EINVAL);
>>   			p->cq_entries = IORING_MAX_CQ_ENTRIES;
>>   		}
>>   		p->cq_entries = roundup_pow_of_two(p->cq_entries);
>> @@ -7811,7 +7812,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>>   				ring_pages(p->sq_entries, p->cq_entries));
>>   		if (ret) {
>>   			free_uid(user);
>> -			return ret;
>> +			return ERR_PTR(ret);
>>   		}
>>   	}
>>   
>> @@ -7821,7 +7822,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p)
>>   			io_unaccount_mem(user, ring_pages(p->sq_entries,
>>   								p->cq_entries));
>>   		free_uid(user);
>> -		return -ENOMEM;
>> +		return ERR_PTR(-ENOMEM);
>>   	}
>>   	ctx->compat = in_compat_syscall();
>>   	ctx->account_mem = account_mem;
>> @@ -7853,22 +7854,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.cqes = offsetof(struct io_rings, cqes);
>>   
>> -	/*
>> -	 * Install ring fd as the very last thing, so we don't risk someone
>> -	 * having closed it before we finish setup
>> -	 */
>> -	ret = io_uring_get_fd(ctx);
>> -	if (ret < 0)
>> -		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);
>> -	return ret;
>> +	return ctx;
>>   err:
>>   	io_ring_ctx_wait_and_kill(ctx);
>> -	return ret;
>> +	return ERR_PTR(ret);
>>   }
>>   
>>   /*
>> @@ -7878,6 +7871,7 @@ 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)
>>   {
>> +	struct io_ring_ctx *ctx;
>>   	struct io_uring_params p;
>>   	long ret;
>>   	int i;
>> @@ -7894,12 +7888,21 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
>>   			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
>>   		return -EINVAL;
>>   
>> -	ret = io_uring_create(entries, &p);
>> -	if (ret < 0)
>> -		return ret;
>> +	ctx = io_uring_create(entries, &p);
>> +	if (IS_ERR(ctx))
>> +		return PTR_ERR(ctx);
>>   
>> -	if (copy_to_user(params, &p, sizeof(p)))
>> +	if (copy_to_user(params, &p, sizeof(p))) {
>> +		io_ring_ctx_wait_and_kill(ctx);
>>   		return -EFAULT;
>> +	}
>> +	/*
>> +	 * Install ring fd as the very last thing, so we don't risk someone
>> +	 * having closed it before we finish setup
>> +	 */
>> +	ret = io_uring_get_fd(ctx);
>> +	if (ret < 0)
>> +		io_ring_ctx_wait_and_kill(ctx);
>>   
>>   	return ret;
>>   }
>>
> 

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

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

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-05-04 13:53 [PATCH] io_uring: handle -EFAULT properly in io_uring_setup() Xiaoguang Wang
2020-05-04 15:14 ` Jens Axboe
2020-05-04 15:40 ` Pavel Begunkov
2020-05-04 15:53   ` Jens Axboe
2020-05-05  2:59   ` Xiaoguang Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox