All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] s390: cio: fix two use-after-free bugs in device.c
@ 2020-11-20  7:48 ` Qinglang Miao
  0 siblings, 0 replies; 6+ messages in thread
From: Qinglang Miao @ 2020-11-20  7:48 UTC (permalink / raw)
  To: Vineeth Vijayan, Peter Oberparleiter, Heiko Carstens,
	Vasily Gorbik, Christian Borntraeger
  Cc: linux-s390, linux-kernel, Qinglang Miao

put_device calls release function which do kfree() inside.
So following use of sch&cdev would cause use-after-free bugs.

Fix these by simply adjusting the position of put_device.

Fixes: 37db8985b211 ("s390/cio: add basic protected virtualization support")
Fixes: 74bd0d859dc3 ("s390/cio: fix unlocked access of online member")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Qinglang Miao <miaoqinglang@huawei.com>
---
 drivers/s390/cio/device.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
index b29fe8d50..69492417b 100644
--- a/drivers/s390/cio/device.c
+++ b/drivers/s390/cio/device.c
@@ -1664,10 +1664,10 @@ void __init ccw_device_destroy_console(struct ccw_device *cdev)
 	struct io_subchannel_private *io_priv = to_io_private(sch);
 
 	set_io_private(sch, NULL);
-	put_device(&sch->dev);
-	put_device(&cdev->dev);
 	dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
 			  io_priv->dma_area, io_priv->dma_area_dma);
+	put_device(&sch->dev);
+	put_device(&cdev->dev);
 	kfree(io_priv);
 }
 
@@ -1774,8 +1774,8 @@ static int ccw_device_remove(struct device *dev)
 				      ret, cdev->private->dev_id.ssid,
 				      cdev->private->dev_id.devno);
 		/* Give up reference obtained in ccw_device_set_online(). */
-		put_device(&cdev->dev);
 		spin_lock_irq(cdev->ccwlock);
+		put_device(&cdev->dev);
 	}
 	ccw_device_set_timeout(cdev, 0);
 	cdev->drv = NULL;
-- 
2.23.0


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

* [PATCH] s390: cio: fix two use-after-free bugs in device.c
@ 2020-11-20  7:48 ` Qinglang Miao
  0 siblings, 0 replies; 6+ messages in thread
From: Qinglang Miao @ 2020-11-20  7:48 UTC (permalink / raw)
  To: Vineeth Vijayan, Peter Oberparleiter, Heiko Carstens,
	Vasily Gorbik, Christian Borntraeger
  Cc: linux-s390, linux-kernel, Qinglang Miao

put_device calls release function which do kfree() inside.
So following use of sch&cdev would cause use-after-free bugs.

Fix these by simply adjusting the position of put_device.

Fixes: 37db8985b211 ("s390/cio: add basic protected virtualization support")
Fixes: 74bd0d859dc3 ("s390/cio: fix unlocked access of online member")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Qinglang Miao <miaoqinglang@huawei.com>
---
 drivers/s390/cio/device.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
index b29fe8d50..69492417b 100644
--- a/drivers/s390/cio/device.c
+++ b/drivers/s390/cio/device.c
@@ -1664,10 +1664,10 @@ void __init ccw_device_destroy_console(struct ccw_device *cdev)
 	struct io_subchannel_private *io_priv = to_io_private(sch);
 
 	set_io_private(sch, NULL);
-	put_device(&sch->dev);
-	put_device(&cdev->dev);
 	dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
 			  io_priv->dma_area, io_priv->dma_area_dma);
+	put_device(&sch->dev);
+	put_device(&cdev->dev);
 	kfree(io_priv);
 }
 
@@ -1774,8 +1774,8 @@ static int ccw_device_remove(struct device *dev)
 				      ret, cdev->private->dev_id.ssid,
 				      cdev->private->dev_id.devno);
 		/* Give up reference obtained in ccw_device_set_online(). */
-		put_device(&cdev->dev);
 		spin_lock_irq(cdev->ccwlock);
+		put_device(&cdev->dev);
 	}
 	ccw_device_set_timeout(cdev, 0);
 	cdev->drv = NULL;
-- 
2.23.0

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

* Re: [PATCH] s390: cio: fix two use-after-free bugs in device.c
  2020-11-20  7:48 ` Qinglang Miao
@ 2020-11-20  7:55   ` Cornelia Huck
  -1 siblings, 0 replies; 6+ messages in thread
From: Cornelia Huck @ 2020-11-20  7:55 UTC (permalink / raw)
  To: Qinglang Miao
  Cc: Vineeth Vijayan, Peter Oberparleiter, Heiko Carstens,
	Vasily Gorbik, Christian Borntraeger, linux-s390, linux-kernel

On Fri, 20 Nov 2020 15:48:49 +0800
Qinglang Miao <miaoqinglang@huawei.com> wrote:

> put_device calls release function which do kfree() inside.
> So following use of sch&cdev would cause use-after-free bugs.
> 
> Fix these by simply adjusting the position of put_device.
> 
> Fixes: 37db8985b211 ("s390/cio: add basic protected virtualization support")
> Fixes: 74bd0d859dc3 ("s390/cio: fix unlocked access of online member")
> Reported-by: Hulk Robot <hulkci@huawei.com>
> Signed-off-by: Qinglang Miao <miaoqinglang@huawei.com>
> ---
>  drivers/s390/cio/device.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
> index b29fe8d50..69492417b 100644
> --- a/drivers/s390/cio/device.c
> +++ b/drivers/s390/cio/device.c
> @@ -1664,10 +1664,10 @@ void __init ccw_device_destroy_console(struct ccw_device *cdev)
>  	struct io_subchannel_private *io_priv = to_io_private(sch);
>  
>  	set_io_private(sch, NULL);
> -	put_device(&sch->dev);
> -	put_device(&cdev->dev);
>  	dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
>  			  io_priv->dma_area, io_priv->dma_area_dma);
> +	put_device(&sch->dev);
> +	put_device(&cdev->dev);

That change looks reasonable.

>  	kfree(io_priv);
>  }
>  
> @@ -1774,8 +1774,8 @@ static int ccw_device_remove(struct device *dev)
>  				      ret, cdev->private->dev_id.ssid,
>  				      cdev->private->dev_id.devno);
>  		/* Give up reference obtained in ccw_device_set_online(). */
> -		put_device(&cdev->dev);
>  		spin_lock_irq(cdev->ccwlock);
> +		put_device(&cdev->dev);

As the comment above states, the put_device() gives up the reference
obtained in ccw_device_set_online(). There's at least one more
reference remaining (held by the caller of the remove function). Moving
the put_device() does not fix anything here.

>  	}
>  	ccw_device_set_timeout(cdev, 0);
>  	cdev->drv = NULL;


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

* Re: [PATCH] s390: cio: fix two use-after-free bugs in device.c
@ 2020-11-20  7:55   ` Cornelia Huck
  0 siblings, 0 replies; 6+ messages in thread
From: Cornelia Huck @ 2020-11-20  7:55 UTC (permalink / raw)
  To: Qinglang Miao
  Cc: Vineeth Vijayan, Peter Oberparleiter, Heiko Carstens,
	Vasily Gorbik, Christian Borntraeger, linux-s390, linux-kernel

On Fri, 20 Nov 2020 15:48:49 +0800
Qinglang Miao <miaoqinglang@huawei.com> wrote:

> put_device calls release function which do kfree() inside.
> So following use of sch&cdev would cause use-after-free bugs.
> 
> Fix these by simply adjusting the position of put_device.
> 
> Fixes: 37db8985b211 ("s390/cio: add basic protected virtualization support")
> Fixes: 74bd0d859dc3 ("s390/cio: fix unlocked access of online member")
> Reported-by: Hulk Robot <hulkci@huawei.com>
> Signed-off-by: Qinglang Miao <miaoqinglang@huawei.com>
> ---
>  drivers/s390/cio/device.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
> index b29fe8d50..69492417b 100644
> --- a/drivers/s390/cio/device.c
> +++ b/drivers/s390/cio/device.c
> @@ -1664,10 +1664,10 @@ void __init ccw_device_destroy_console(struct ccw_device *cdev)
>  	struct io_subchannel_private *io_priv = to_io_private(sch);
>  
>  	set_io_private(sch, NULL);
> -	put_device(&sch->dev);
> -	put_device(&cdev->dev);
>  	dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
>  			  io_priv->dma_area, io_priv->dma_area_dma);
> +	put_device(&sch->dev);
> +	put_device(&cdev->dev);

That change looks reasonable.

>  	kfree(io_priv);
>  }
>  
> @@ -1774,8 +1774,8 @@ static int ccw_device_remove(struct device *dev)
>  				      ret, cdev->private->dev_id.ssid,
>  				      cdev->private->dev_id.devno);
>  		/* Give up reference obtained in ccw_device_set_online(). */
> -		put_device(&cdev->dev);
>  		spin_lock_irq(cdev->ccwlock);
> +		put_device(&cdev->dev);

As the comment above states, the put_device() gives up the reference
obtained in ccw_device_set_online(). There's at least one more
reference remaining (held by the caller of the remove function). Moving
the put_device() does not fix anything here.

>  	}
>  	ccw_device_set_timeout(cdev, 0);
>  	cdev->drv = NULL;

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

* Re: [PATCH] s390: cio: fix two use-after-free bugs in device.c
  2020-11-20  7:55   ` Cornelia Huck
@ 2020-12-01  6:31     ` Qinglang Miao
  -1 siblings, 0 replies; 6+ messages in thread
From: Qinglang Miao @ 2020-12-01  6:31 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: Vineeth Vijayan, Peter Oberparleiter, Heiko Carstens,
	Vasily Gorbik, Christian Borntraeger, linux-s390, linux-kernel



在 2020/11/20 15:55, Cornelia Huck 写道:
> On Fri, 20 Nov 2020 15:48:49 +0800
> Qinglang Miao <miaoqinglang@huawei.com> wrote:
> 
>> put_device calls release function which do kfree() inside.
>> So following use of sch&cdev would cause use-after-free bugs.
>>
>> Fix these by simply adjusting the position of put_device.
>>
>> Fixes: 37db8985b211 ("s390/cio: add basic protected virtualization support")
>> Fixes: 74bd0d859dc3 ("s390/cio: fix unlocked access of online member")
>> Reported-by: Hulk Robot <hulkci@huawei.com>
>> Signed-off-by: Qinglang Miao <miaoqinglang@huawei.com>
>> ---
>>   drivers/s390/cio/device.c | 6 +++---
>>   1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
>> index b29fe8d50..69492417b 100644
>> --- a/drivers/s390/cio/device.c
>> +++ b/drivers/s390/cio/device.c
>> @@ -1664,10 +1664,10 @@ void __init ccw_device_destroy_console(struct ccw_device *cdev)
>>   	struct io_subchannel_private *io_priv = to_io_private(sch);
>>   
>>   	set_io_private(sch, NULL);
>> -	put_device(&sch->dev);
>> -	put_device(&cdev->dev);
>>   	dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
>>   			  io_priv->dma_area, io_priv->dma_area_dma);
>> +	put_device(&sch->dev);
>> +	put_device(&cdev->dev);
> 
> That change looks reasonable.
> 
>>   	kfree(io_priv);
>>   }
>>   
>> @@ -1774,8 +1774,8 @@ static int ccw_device_remove(struct device *dev)
>>   				      ret, cdev->private->dev_id.ssid,
>>   				      cdev->private->dev_id.devno);
>>   		/* Give up reference obtained in ccw_device_set_online(). */
>> -		put_device(&cdev->dev);
>>   		spin_lock_irq(cdev->ccwlock);
>> +		put_device(&cdev->dev);
> 
> As the comment above states, the put_device() gives up the reference
> obtained in ccw_device_set_online(). There's at least one more
> reference remaining (held by the caller of the remove function). Moving
> the put_device() does not fix anything here.
Hi, Cornelia

Sorry for the delayed reply.

Your suggestion is reasonable, there is a mistake in this patch for I 
didn't notice that there would be at least one more reference remaining.

So I sent a new patch only to move put_device after dma_free_coherent. I 
put the link as below:
https://lore.kernel.org/lkml/20201201063150.82128-1-miaoqinglang@huawei.com/

Thanks!
> 
>>   	}
>>   	ccw_device_set_timeout(cdev, 0);
>>   	cdev->drv = NULL;
> 
> .
> 

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

* Re: [PATCH] s390: cio: fix two use-after-free bugs in device.c
@ 2020-12-01  6:31     ` Qinglang Miao
  0 siblings, 0 replies; 6+ messages in thread
From: Qinglang Miao @ 2020-12-01  6:31 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: Vineeth Vijayan, Peter Oberparleiter, Heiko Carstens,
	Vasily Gorbik, Christian Borntraeger, linux-s390, linux-kernel



锟斤拷 2020/11/20 15:55, Cornelia Huck 写锟斤拷:
> On Fri, 20 Nov 2020 15:48:49 +0800
> Qinglang Miao <miaoqinglang@huawei.com> wrote:
> 
>> put_device calls release function which do kfree() inside.
>> So following use of sch&cdev would cause use-after-free bugs.
>>
>> Fix these by simply adjusting the position of put_device.
>>
>> Fixes: 37db8985b211 ("s390/cio: add basic protected virtualization support")
>> Fixes: 74bd0d859dc3 ("s390/cio: fix unlocked access of online member")
>> Reported-by: Hulk Robot <hulkci@huawei.com>
>> Signed-off-by: Qinglang Miao <miaoqinglang@huawei.com>
>> ---
>>   drivers/s390/cio/device.c | 6 +++---
>>   1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
>> index b29fe8d50..69492417b 100644
>> --- a/drivers/s390/cio/device.c
>> +++ b/drivers/s390/cio/device.c
>> @@ -1664,10 +1664,10 @@ void __init ccw_device_destroy_console(struct ccw_device *cdev)
>>   	struct io_subchannel_private *io_priv = to_io_private(sch);
>>   
>>   	set_io_private(sch, NULL);
>> -	put_device(&sch->dev);
>> -	put_device(&cdev->dev);
>>   	dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
>>   			  io_priv->dma_area, io_priv->dma_area_dma);
>> +	put_device(&sch->dev);
>> +	put_device(&cdev->dev);
> 
> That change looks reasonable.
> 
>>   	kfree(io_priv);
>>   }
>>   
>> @@ -1774,8 +1774,8 @@ static int ccw_device_remove(struct device *dev)
>>   				      ret, cdev->private->dev_id.ssid,
>>   				      cdev->private->dev_id.devno);
>>   		/* Give up reference obtained in ccw_device_set_online(). */
>> -		put_device(&cdev->dev);
>>   		spin_lock_irq(cdev->ccwlock);
>> +		put_device(&cdev->dev);
> 
> As the comment above states, the put_device() gives up the reference
> obtained in ccw_device_set_online(). There's at least one more
> reference remaining (held by the caller of the remove function). Moving
> the put_device() does not fix anything here.
Hi, Cornelia

Sorry for the delayed reply.

Your suggestion is reasonable, there is a mistake in this patch for I 
didn't notice that there would be at least one more reference remaining.

So I sent a new patch only to move put_device after dma_free_coherent. I 
put the link as below:
https://lore.kernel.org/lkml/20201201063150.82128-1-miaoqinglang@huawei.com/

Thanks!
> 
>>   	}
>>   	ccw_device_set_timeout(cdev, 0);
>>   	cdev->drv = NULL;
> 
> .
> 

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

end of thread, other threads:[~2020-12-01  6:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-20  7:48 [PATCH] s390: cio: fix two use-after-free bugs in device.c Qinglang Miao
2020-11-20  7:48 ` Qinglang Miao
2020-11-20  7:55 ` Cornelia Huck
2020-11-20  7:55   ` Cornelia Huck
2020-12-01  6:31   ` Qinglang Miao
2020-12-01  6:31     ` Qinglang Miao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.