mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhu Wang <wangzhu9@huawei.com>
To: <jejb@linux.ibm.com>, <martin.petersen@oracle.com>,
	<bvanassche@acm.org>, <dan.carpenter@linaro.org>,
	<linux-scsi@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <wangzhu9@huawei.com>
Subject: [PATCH -next] scsi: core: fix double free in raid_component_add()
Date: Sat, 19 Aug 2023 08:03:45 +0000	[thread overview]
Message-ID: <20230819080345.149144-1-wangzhu9@huawei.com> (raw)

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


             reply	other threads:[~2023-08-19  8:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-19  8:03 Zhu Wang [this message]
2023-08-21 19:50 ` Bart Van Assche
2023-08-22 23:53   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230819080345.149144-1-wangzhu9@huawei.com \
    --to=wangzhu9@huawei.com \
    --cc=bvanassche@acm.org \
    --cc=dan.carpenter@linaro.org \
    --cc=jejb@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®