[PATCH] [v3] scsi: zfcp: Fix a double put in zfcp_port_enqueue

Dinghao Liu posted 1 patch 2 years, 4 months ago
drivers/s390/scsi/zfcp_aux.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
[PATCH] [v3] scsi: zfcp: Fix a double put in zfcp_port_enqueue
Posted by Dinghao Liu 2 years, 4 months ago
When device_register() fails, zfcp_port_release() will be called
after put_device(). As a result, zfcp_ccw_adapter_put() will be
called twice: one in zfcp_port_release() and one in the error path
after device_register(). So the reference on the adapter object is
doubly put, which may lead to a premature free. Fix this by adjusting
the error tag after device_register().

Fixes: f3450c7b9172 ("[SCSI] zfcp: Replace local reference counting with common kref")
Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---

Changelog:

v2: -Improve the patch description.
    -Add a new label 'err_register' to unify code style.

v3: -Improve the names of goto labels.
---
 drivers/s390/scsi/zfcp_aux.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/s390/scsi/zfcp_aux.c b/drivers/s390/scsi/zfcp_aux.c
index df782646e856..ab2f35bc294d 100644
--- a/drivers/s390/scsi/zfcp_aux.c
+++ b/drivers/s390/scsi/zfcp_aux.c
@@ -518,12 +518,12 @@ struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
 	if (port) {
 		put_device(&port->dev);
 		retval = -EEXIST;
-		goto err_out;
+		goto err_put;
 	}
 
 	port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
 	if (!port)
-		goto err_out;
+		goto err_put;
 
 	rwlock_init(&port->unit_list_lock);
 	INIT_LIST_HEAD(&port->unit_list);
@@ -546,7 +546,7 @@ struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
 
 	if (dev_set_name(&port->dev, "0x%016llx", (unsigned long long)wwpn)) {
 		kfree(port);
-		goto err_out;
+		goto err_put;
 	}
 	retval = -EINVAL;
 
@@ -563,7 +563,8 @@ struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
 
 	return port;
 
-err_out:
+err_put:
 	zfcp_ccw_adapter_put(adapter);
+err_out:
 	return ERR_PTR(retval);
 }
-- 
2.17.1
Re: [PATCH] [v3] scsi: zfcp: Fix a double put in zfcp_port_enqueue
Posted by Martin K. Petersen 2 years, 4 months ago
On Sat, 23 Sep 2023 18:37:23 +0800, Dinghao Liu wrote:

> When device_register() fails, zfcp_port_release() will be called
> after put_device(). As a result, zfcp_ccw_adapter_put() will be
> called twice: one in zfcp_port_release() and one in the error path
> after device_register(). So the reference on the adapter object is
> doubly put, which may lead to a premature free. Fix this by adjusting
> the error tag after device_register().
> 
> [...]

Applied to 6.6/scsi-fixes, thanks!

[1/1] scsi: zfcp: Fix a double put in zfcp_port_enqueue
      https://git.kernel.org/mkp/scsi/c/b481f644d917

-- 
Martin K. Petersen	Oracle Linux Engineering
Re: [PATCH] [v3] scsi: zfcp: Fix a double put in zfcp_port_enqueue
Posted by Benjamin Block 2 years, 4 months ago
On Sat, Sep 23, 2023 at 06:37:23PM +0800, Dinghao Liu wrote:
> When device_register() fails, zfcp_port_release() will be called
> after put_device(). As a result, zfcp_ccw_adapter_put() will be
> called twice: one in zfcp_port_release() and one in the error path
> after device_register(). So the reference on the adapter object is
> doubly put, which may lead to a premature free. Fix this by adjusting
> the error tag after device_register().
> 
> Fixes: f3450c7b9172 ("[SCSI] zfcp: Replace local reference counting with common kref")
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> ---
> 
> Changelog:
> 
> v2: -Improve the patch description.
>     -Add a new label 'err_register' to unify code style.
> 
> v3: -Improve the names of goto labels.
> ---
>  drivers/s390/scsi/zfcp_aux.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/s390/scsi/zfcp_aux.c b/drivers/s390/scsi/zfcp_aux.c
> index df782646e856..ab2f35bc294d 100644
> --- a/drivers/s390/scsi/zfcp_aux.c
> +++ b/drivers/s390/scsi/zfcp_aux.c
> @@ -518,12 +518,12 @@ struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
>  	if (port) {
>  		put_device(&port->dev);
>  		retval = -EEXIST;
> -		goto err_out;
> +		goto err_put;
>  	}
>  
>  	port = kzalloc(sizeof(struct zfcp_port), GFP_KERNEL);
>  	if (!port)
> -		goto err_out;
> +		goto err_put;
>  
>  	rwlock_init(&port->unit_list_lock);
>  	INIT_LIST_HEAD(&port->unit_list);
> @@ -546,7 +546,7 @@ struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
>  
>  	if (dev_set_name(&port->dev, "0x%016llx", (unsigned long long)wwpn)) {
>  		kfree(port);
> -		goto err_out;
> +		goto err_put;
>  	}
>  	retval = -EINVAL;
>  
> @@ -563,7 +563,8 @@ struct zfcp_port *zfcp_port_enqueue(struct zfcp_adapter *adapter, u64 wwpn,
>  
>  	return port;
>  
> -err_out:
> +err_put:
>  	zfcp_ccw_adapter_put(adapter);
> +err_out:
>  	return ERR_PTR(retval);
>  }
> -- 
> 2.17.1
> 

Looks good to me. Ideally we would have a stable tag (v2.6.33+) in with the
Fixes tag as well, but I don't think we need to fly an extra round just for
that. We can ask the stable team once this is included upstream.


Acked-by: Benjamin Block <bblock@linux.ibm.com>


Martin, can you please pick this directly?


-- 
Best Regards, Benjamin Block        /        Linux on IBM Z Kernel Development
IBM Deutschland Research & Development GmbH    /   https://www.ibm.com/privacy
Vors. Aufs.-R.: Gregor Pillen         /         Geschäftsführung: David Faller
Sitz der Ges.: Böblingen     /    Registergericht: AmtsG Stuttgart, HRB 243294