linux-rdma.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] IB/ucma: Fix theoretical user triggered use-after-free
@ 2015-08-04 23:13 Jason Gunthorpe
       [not found] ` <20150804231332.GA22959-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Gunthorpe @ 2015-08-04 23:13 UTC (permalink / raw)
  To: dledford-H+wXaHxf7aLQT0dZR+AlfA, Sean Hefty
  Cc: linux-rdma-u79uwXL29TY76Z2rM5mHXA

Something like this:

CPU A                         CPU B
========================      ================================
ucma_destroy_id()
 wait_for_completion()
                              .. anything
                                ucma_put_ctx()
                                  complete()
 .. continues ...
                              ucma_leave_multicast()
                               mutex_lock(mut)
                                 atomic_inc(ctx->ref)
                               mutex_unlock(mut)
 ucma_free_ctx()
  ucma_cleanup_multicast()
   mutex_lock(mut)
     kfree(mc)
                               rdma_leave_multicast(mc->ctx->cm_id,..

Fix it by latching the ref at 0. Once it goes to 0 mc and ctx cannot
leave the mutex(mut) protection.

The other atomic_inc in ucma_get_ctx is OK because mutex(mut) protects
it from racing with ucma_destroy_id.

Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
---
 drivers/infiniband/core/ucma.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c
index 29b21213ea75..acac9eafdbf6 100644
--- a/drivers/infiniband/core/ucma.c
+++ b/drivers/infiniband/core/ucma.c
@@ -1321,10 +1321,10 @@ static ssize_t ucma_leave_multicast(struct ucma_file *file,
 		mc = ERR_PTR(-ENOENT);
 	else if (mc->ctx->file != file)
 		mc = ERR_PTR(-EINVAL);
-	else {
+	else if (!atomic_inc_not_zero(&mc->ctx->ref))
+		mc = ERR_PTR(-ENXIO);
+	else
 		idr_remove(&multicast_idr, mc->id);
-		atomic_inc(&mc->ctx->ref);
-	}
 	mutex_unlock(&mut);
 
 	if (IS_ERR(mc)) {
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* RE: [PATCH] IB/ucma: Fix theoretical user triggered use-after-free
       [not found] ` <20150804231332.GA22959-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
@ 2015-08-06 17:33   ` Hefty, Sean
       [not found]     ` <1828884A29C6694DAF28B7E6B8A82373A9023323-P5GAC/sN6hkd3b2yrw5b5LfspsVTdybXVpNB7YpNyf8@public.gmane.org>
  0 siblings, 1 reply; 3+ messages in thread
From: Hefty, Sean @ 2015-08-06 17:33 UTC (permalink / raw)
  To: Jason Gunthorpe, dledford-H+wXaHxf7aLQT0dZR+AlfA
  Cc: linux-rdma-u79uwXL29TY76Z2rM5mHXA

> Something like this:
> 
> CPU A                         CPU B
> ========================      ================================
> ucma_destroy_id()
>  wait_for_completion()
>                               .. anything
>                                 ucma_put_ctx()
>                                   complete()
>  .. continues ...
>                               ucma_leave_multicast()
>                                mutex_lock(mut)
>                                  atomic_inc(ctx->ref)
>                                mutex_unlock(mut)
>  ucma_free_ctx()
>   ucma_cleanup_multicast()
>    mutex_lock(mut)
>      kfree(mc)
>                                rdma_leave_multicast(mc->ctx->cm_id,..
> 
> Fix it by latching the ref at 0. Once it goes to 0 mc and ctx cannot
> leave the mutex(mut) protection.
> 
> The other atomic_inc in ucma_get_ctx is OK because mutex(mut) protects
> it from racing with ucma_destroy_id.
> 
> Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>

Acked-by: Sean Hefty <sean.hefty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>

> ---
>  drivers/infiniband/core/ucma.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/infiniband/core/ucma.c
> b/drivers/infiniband/core/ucma.c
> index 29b21213ea75..acac9eafdbf6 100644
> --- a/drivers/infiniband/core/ucma.c
> +++ b/drivers/infiniband/core/ucma.c
> @@ -1321,10 +1321,10 @@ static ssize_t ucma_leave_multicast(struct
> ucma_file *file,
>  		mc = ERR_PTR(-ENOENT);
>  	else if (mc->ctx->file != file)
>  		mc = ERR_PTR(-EINVAL);
> -	else {
> +	else if (!atomic_inc_not_zero(&mc->ctx->ref))
> +		mc = ERR_PTR(-ENXIO);
> +	else
>  		idr_remove(&multicast_idr, mc->id);
> -		atomic_inc(&mc->ctx->ref);
> -	}
>  	mutex_unlock(&mut);
> 
>  	if (IS_ERR(mc)) {

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] IB/ucma: Fix theoretical user triggered use-after-free
       [not found]     ` <1828884A29C6694DAF28B7E6B8A82373A9023323-P5GAC/sN6hkd3b2yrw5b5LfspsVTdybXVpNB7YpNyf8@public.gmane.org>
@ 2015-08-15  1:22       ` Doug Ledford
  0 siblings, 0 replies; 3+ messages in thread
From: Doug Ledford @ 2015-08-15  1:22 UTC (permalink / raw)
  To: Hefty, Sean, Jason Gunthorpe; +Cc: linux-rdma-u79uwXL29TY76Z2rM5mHXA

[-- Attachment #1: Type: text/plain, Size: 1351 bytes --]

On 08/06/2015 01:33 PM, Hefty, Sean wrote:
>> Something like this:
>>
>> CPU A                         CPU B
>> ========================      ================================
>> ucma_destroy_id()
>>  wait_for_completion()
>>                               .. anything
>>                                 ucma_put_ctx()
>>                                   complete()
>>  .. continues ...
>>                               ucma_leave_multicast()
>>                                mutex_lock(mut)
>>                                  atomic_inc(ctx->ref)
>>                                mutex_unlock(mut)
>>  ucma_free_ctx()
>>   ucma_cleanup_multicast()
>>    mutex_lock(mut)
>>      kfree(mc)
>>                                rdma_leave_multicast(mc->ctx->cm_id,..
>>
>> Fix it by latching the ref at 0. Once it goes to 0 mc and ctx cannot
>> leave the mutex(mut) protection.
>>
>> The other atomic_inc in ucma_get_ctx is OK because mutex(mut) protects
>> it from racing with ucma_destroy_id.
>>
>> Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
> 
> Acked-by: Sean Hefty <sean.hefty-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>

This has been picked up.  Thanks.


-- 
Doug Ledford <dledford-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
              GPG KeyID: 0E572FDD



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

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

end of thread, other threads:[~2015-08-15  1:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-04 23:13 [PATCH] IB/ucma: Fix theoretical user triggered use-after-free Jason Gunthorpe
     [not found] ` <20150804231332.GA22959-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2015-08-06 17:33   ` Hefty, Sean
     [not found]     ` <1828884A29C6694DAF28B7E6B8A82373A9023323-P5GAC/sN6hkd3b2yrw5b5LfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2015-08-15  1:22       ` Doug Ledford

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).