Skip to contentSkip to conversation

scsi: scsi_debug: fix possible name leak in sdebug_add_host_helper()

PATCH Applied Yang Yingliang 2 participants 3 messages Nov 12, 2022 – Dec 1, 2022 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: scsi_debug: fix possible name leak in sdebug_add_host_helper()

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it needs be freed, when device_register() returns error.

As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup(), and sdbg_host is freed in sdebug_release_adapter().

When the device release is not set, it means the device is not initialized, we can not call put_device(), in this case, use kfree() to free memory.

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
 drivers/scsi/scsi_debug.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/scsi/scsi_debug.c +4 -1
index 697fc57bc711..104888bed801 100644
@@ -7329,7 +7329,10 @@ static int sdebug_add_host_helper(int per_host_idx)
 		kfree(sdbg_devinfo->zstate);
 		kfree(sdbg_devinfo);
 	}
-	kfree(sdbg_host);
+	if (sdbg_host->dev.release)
+		put_device(&sdbg_host->dev);
+	else
+		kfree(sdbg_host);
 	pr_warn("%s: failed, errno=%d\n", __func__, -error);
 	return error;
 }
Signature · 1 line
2.25.1
#1 lore raw

Yang,

Quoted 3 lines

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it needs be freed, when device_register() returns error.

Applied to 6.2/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#2 lore raw
In reply to Yang Yingliang
Quoted 10 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it needs be freed, when device_register() returns error.

As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup(), and sdbg_host is freed in sdebug_release_adapter().

[...]

Applied to 6.2/scsi-queue, thanks!

[1/1] scsi: scsi_debug: fix possible name leak in sdebug_add_host_helper()

      https://git.kernel.org/mkp/scsi/c/e6d773f93a49
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering