[PATCH] fsi: Fix refcount leak in slave init error path

Guangshuo Li posted 1 patch 4 hours ago
drivers/fsi/fsi-core.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH] fsi: Fix refcount leak in slave init error path
Posted by Guangshuo Li 4 hours ago
After device_initialize(), the lifetime of slave is expected to be
managed through the device core reference counting. In the
cdev_device_add() failure path, slave and its associated resources are
freed directly, rather than releasing the device reference with
put_device(). This may leave the reference count of the embedded struct
device unbalanced, resulting in a refcount leak and potentially leading
to a use-after-free.

A possible fix would be to use put_device() in the failure path and let
fsi_slave_release() handle the final cleanup.

Fixes: d1dcd6782576 ("fsi: Add cfam char devices")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/fsi/fsi-core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/fsi/fsi-core.c b/drivers/fsi/fsi-core.c
index c6c115993ebc..f447dd53db62 100644
--- a/drivers/fsi/fsi-core.c
+++ b/drivers/fsi/fsi-core.c
@@ -1084,7 +1084,7 @@ static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
 	rc = cdev_device_add(&slave->cdev, &slave->dev);
 	if (rc) {
 		dev_err(&slave->dev, "Error %d creating slave device\n", rc);
-		goto err_free_ida;
+		goto err_put_dev;
 	}
 
 	/* Now that we have the cdev registered with the core, any fatal
@@ -1110,8 +1110,9 @@ static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
 
 	return 0;
 
-err_free_ida:
-	fsi_free_minor(slave->dev.devt);
+err_put_dev:
+	put_device(&slave->dev);
+	return rc;
 err_free:
 	of_node_put(slave->dev.of_node);
 	kfree(slave);
-- 
2.43.0