public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH -next] scsi: core: fix double free in raid_component_add()
@ 2023-08-19  8:03 Zhu Wang
  2023-08-21 19:50 ` Bart Van Assche
  0 siblings, 1 reply; 3+ messages in thread
From: Zhu Wang @ 2023-08-19  8:03 UTC (permalink / raw)
  To: jejb, martin.petersen, bvanassche, dan.carpenter, linux-scsi,
	linux-kernel
  Cc: wangzhu9

Previous commit 04b5b5cb0136 ("scsi: core: Fix possible memory leak if
device_add() fails") introduced a double free when device_add() failed.
When device_add() failed, the put_device(&rc->dev) is called, which will
call raid_component_release(), and this function will call
put_device(rc->dev.parent) and kfree(rc), but in the error path of
raid_component_release() above two functions are called again, so 'rc' are
freed twice.

We do not just revert the patch, since the memory allocated by
dev_set_name() is not released in the error path, it should be released
through calling put_device(&rc->dev) which will call kobject_cleanup(),
and the device name will be released in it. Though the commit 04b5b5cb0136
("scsi: core: Fix possible memory leak if device_add() fails") fixed the
memory leak, it didn't consider the double free problem. We removed
put_device(rc->dev.parent) and kfree(rc) in the error path, and we moved
put_device(&rc->dev) after rd->component-count--, since after
put_device(&rc->dev) is called, 'rc' is freed, so list_del(&rc->node)
cannot be called again.

Fixes: 04b5b5cb0136 ("scsi: core: Fix possible memory leak if device_add() fails")
Signed-off-by: Zhu Wang <wangzhu9@huawei.com>
---
 drivers/scsi/raid_class.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/scsi/raid_class.c b/drivers/scsi/raid_class.c
index 711252e52d8e..86ed1f66d749 100644
--- a/drivers/scsi/raid_class.c
+++ b/drivers/scsi/raid_class.c
@@ -248,11 +248,9 @@ int raid_component_add(struct raid_template *r,struct device *raid_dev,
 	return 0;
 
 err_out:
-	put_device(&rc->dev);
 	list_del(&rc->node);
 	rd->component_count--;
-	put_device(component_dev);
-	kfree(rc);
+	put_device(&rc->dev);
 	return err;
 }
 EXPORT_SYMBOL(raid_component_add);
-- 
2.34.1


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

end of thread, other threads:[~2023-08-22 23:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-19  8:03 [PATCH -next] scsi: core: fix double free in raid_component_add() Zhu Wang
2023-08-21 19:50 ` Bart Van Assche
2023-08-22 23:53   ` kernel test robot

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