[PATCH 1/3] nvmet: fix memory leak in configfs

Aleksandr Miloserdov a.miloserdov at yadro.com
Wed Oct 26 01:31:33 PDT 2022


Since model_number is allocated before it needs to be freed before kmemdump_nul.

Reviewed-by: Konstantin Shelekhin <k.shelekhin at yadro.com>
Reviewed-by: Dmitriy Bogdanov <d.bogdanov at yadro.com>
Signed-off-by: Aleksandr Miloserdov <a.miloserdov at yadro.com>
---
 drivers/nvme/target/configfs.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index e34a2896fedb..89242b0f8614 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -1215,6 +1215,7 @@ static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
 		const char *page, size_t count)
 {
 	int pos = 0, len;
+	char *val;
 
 	if (subsys->subsys_discovered) {
 		pr_err("Can't set model number. %s is already assigned\n",
@@ -1237,9 +1238,14 @@ static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
 			return -EINVAL;
 	}
 
-	subsys->model_number = kmemdup_nul(page, len, GFP_KERNEL);
-	if (!subsys->model_number)
+	val = kmemdup_nul(page, len, GFP_KERNEL);
+	if (!val)
 		return -ENOMEM;
+
+	kfree(subsys->model_number);
+
+	subsys->model_number = val;
+
 	return count;
 }
 
-- 
2.37.0




More information about the Linux-nvme mailing list