Skip to contentSkip to conversation

EDAC/mc: fix potential memoryleak in edac_mc_alloc()

PATCH In review Miaohe Lin 1 participant 2 messages Jun 3, 2023 – Jun 21, 2023 EDAC-core

lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 EDAC/mc: fix potential memoryleak in edac_mc_alloc()

When fails to allocate memory for layers or pvt_info, _edac_mc_free() will be called to release the resource of edac mc but mci->dev is not even initialized at that time. _edac_mc_free() will fail to release the allocated memory.

Fixes 0bbb265f7089 ("EDAC/mc: Get rid of silly one-shot struct allocation in edac_mc_alloc()")
---
 drivers/edac/edac_mc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/edac/edac_mc.c +2 -3
index 6faeb2ab3960..89dc5954d19e 100644
@@ -364,6 +364,8 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
 	mci = kzalloc(sizeof(struct mem_ctl_info), GFP_KERNEL);
 	if (!mci)
 		return NULL;
+	mci->dev.release = mci_release;
+	device_initialize(&mci->dev);
 
 	mci->layers = kcalloc(n_layers, sizeof(struct edac_mc_layer), GFP_KERNEL);
 	if (!mci->layers)
@@ -373,9 +375,6 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
 	if (!mci->pvt_info)
 		goto error;
 
-	mci->dev.release = mci_release;
-	device_initialize(&mci->dev);
-
 	/* setup index and various internal pointers */
 	mci->mc_idx = mc_num;
 	mci->tot_dimms = tot_dimms;
Signature · 1 line
2.27.0
author
#1 lore raw
Quoted 4 lines · Miaohe Lin

When fails to allocate memory for layers or pvt_info, _edac_mc_free() will be called to release the resource of edac mc but mci->dev is not even initialized at that time. _edac_mc_free() will fail to release the allocated memory.

friendly ping.. :)

Quoted 31 lines
Fixes 0bbb265f7089 ("EDAC/mc: Get rid of silly one-shot struct allocation in edac_mc_alloc()")
---
 drivers/edac/edac_mc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/edac/edac_mc.c +2 -3
index 6faeb2ab3960..89dc5954d19e 100644
@@ -364,6 +364,8 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
 	mci = kzalloc(sizeof(struct mem_ctl_info), GFP_KERNEL);
 	if (!mci)
 		return NULL;
+	mci->dev.release = mci_release;
+	device_initialize(&mci->dev);
 
 	mci->layers = kcalloc(n_layers, sizeof(struct edac_mc_layer), GFP_KERNEL);
 	if (!mci->layers)
@@ -373,9 +375,6 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
 	if (!mci->pvt_info)
 		goto error;
 
-	mci->dev.release = mci_release;
-	device_initialize(&mci->dev);
-
 	/* setup index and various internal pointers */
 	mci->mc_idx = mc_num;
 	mci->tot_dimms = tot_dimms;