public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: ulpi: fix a double free in ulpi_register_interface()
@ 2025-12-19 15:48 Haoxiang Li
  2025-12-23 14:27 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Haoxiang Li @ 2025-12-19 15:48 UTC (permalink / raw)
  To: heikki.krogerus, gregkh, sean.anderson
  Cc: linux-usb, linux-kernel, Haoxiang Li, stable

If ulpi_register() fails, put_device() is called in ulpi_register(),
kfree() in ulpi_register_interface() will result in a double free.

Also, refactor the device registration sequence to use a unified
put_device() cleanup path, addressing multiple error returns in
ulpi_register().

Found by code review and compiled on ubuntu 20.04.

Fixes: 0a907ee9d95e ("usb: ulpi: Call of_node_put correctly")
Cc: stable@vger.kernel.org
Signed-off-by: Haoxiang Li <lihaoxiang@isrc.iscas.ac.cn>
---
 drivers/usb/common/ulpi.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/common/ulpi.c b/drivers/usb/common/ulpi.c
index 4a2ee447b213..c81a0cb24067 100644
--- a/drivers/usb/common/ulpi.c
+++ b/drivers/usb/common/ulpi.c
@@ -278,6 +278,7 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
 	int ret;
 	struct dentry *root;
 
+	device_initialize(&ulpi->dev);
 	ulpi->dev.parent = dev; /* needed early for ops */
 	ulpi->dev.bus = &ulpi_bus;
 	ulpi->dev.type = &ulpi_dev_type;
@@ -287,19 +288,15 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
 
 	ret = ulpi_of_register(ulpi);
 	if (ret)
-		return ret;
+		goto err_register;
 
 	ret = ulpi_read_id(ulpi);
-	if (ret) {
-		of_node_put(ulpi->dev.of_node);
-		return ret;
-	}
+	if (ret)
+		goto err_register;
 
-	ret = device_register(&ulpi->dev);
-	if (ret) {
-		put_device(&ulpi->dev);
-		return ret;
-	}
+	ret = device_add(&ulpi->dev);
+	if (ret)
+		goto err_register;
 
 	root = debugfs_create_dir(dev_name(&ulpi->dev), ulpi_root);
 	debugfs_create_file("regs", 0444, root, ulpi, &ulpi_regs_fops);
@@ -308,6 +305,10 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
 		ulpi->id.vendor, ulpi->id.product);
 
 	return 0;
+
+err_register:
+	put_device(&ulpi->dev);
+	return ret;
 }
 
 /**
@@ -331,10 +332,8 @@ struct ulpi *ulpi_register_interface(struct device *dev,
 	ulpi->ops = ops;
 
 	ret = ulpi_register(dev, ulpi);
-	if (ret) {
-		kfree(ulpi);
+	if (ret)
 		return ERR_PTR(ret);
-	}
 
 	return ulpi;
 }
-- 
2.25.1


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

* Re: [PATCH] usb: ulpi: fix a double free in ulpi_register_interface()
  2025-12-19 15:48 [PATCH] usb: ulpi: fix a double free in ulpi_register_interface() Haoxiang Li
@ 2025-12-23 14:27 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2025-12-23 14:27 UTC (permalink / raw)
  To: Haoxiang Li
  Cc: heikki.krogerus, sean.anderson, linux-usb, linux-kernel, stable

On Fri, Dec 19, 2025 at 11:48:59PM +0800, Haoxiang Li wrote:
> If ulpi_register() fails, put_device() is called in ulpi_register(),
> kfree() in ulpi_register_interface() will result in a double free.
> 
> Also, refactor the device registration sequence to use a unified
> put_device() cleanup path, addressing multiple error returns in
> ulpi_register().
> 
> Found by code review and compiled on ubuntu 20.04.

"compiled on" doesn't really provide any information, sorry.  Espeically
for a VERY old distro release :(

How was this tested?

> Fixes: 0a907ee9d95e ("usb: ulpi: Call of_node_put correctly")
> Cc: stable@vger.kernel.org
> Signed-off-by: Haoxiang Li <lihaoxiang@isrc.iscas.ac.cn>
> ---
>  drivers/usb/common/ulpi.c | 25 ++++++++++++-------------
>  1 file changed, 12 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/usb/common/ulpi.c b/drivers/usb/common/ulpi.c
> index 4a2ee447b213..c81a0cb24067 100644
> --- a/drivers/usb/common/ulpi.c
> +++ b/drivers/usb/common/ulpi.c
> @@ -278,6 +278,7 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
>  	int ret;
>  	struct dentry *root;
>  
> +	device_initialize(&ulpi->dev);
>  	ulpi->dev.parent = dev; /* needed early for ops */
>  	ulpi->dev.bus = &ulpi_bus;
>  	ulpi->dev.type = &ulpi_dev_type;
> @@ -287,19 +288,15 @@ static int ulpi_register(struct device *dev, struct ulpi *ulpi)
>  
>  	ret = ulpi_of_register(ulpi);
>  	if (ret)
> -		return ret;
> +		goto err_register;
>  
>  	ret = ulpi_read_id(ulpi);
> -	if (ret) {
> -		of_node_put(ulpi->dev.of_node);
> -		return ret;
> -	}
> +	if (ret)
> +		goto err_register;
>  
> -	ret = device_register(&ulpi->dev);

Splitting this up into init/add instead of just register is usually only
done if you _HAVE_ to do that.  I really don't see why that is required
here at all, sorry.  Are you sure this is the correct solution?

thanks,

greg k-h

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

end of thread, other threads:[~2025-12-23 14:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-19 15:48 [PATCH] usb: ulpi: fix a double free in ulpi_register_interface() Haoxiang Li
2025-12-23 14:27 ` Greg KH

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