Skip to contentSkip to conversation

EDAC/versalnet: Fix resource leaks and NULL derefs in init_versalnet()

PATCH In review Eric-Terminal 3 participants 14 messages Feb 21 – Feb 26 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 EDAC/versalnet: Fix resource leaks and NULL derefs in init_versalnet()

init_versalnet() has several bugs in its error handling:

- kzalloc() and kmalloc() return values are used without NULL checks, causing a NULL pointer dereference when allocation fails.

- The cleanup loop uses while (i--) which skips the current failing index, leaking the resources already allocated for that slot.

- edac_mc_del_mc() is called unconditionally during unwind, even for controllers that were never registered with edac_mc_add_mc().

- sprintf() is used instead of snprintf() on a fixed-size buffer.

Fix by adding NULL checks for dev and name allocations, replacing while (i--) with for (j = i; j >= 0; j--) to include the failing index, tracking successful edac_mc_add_mc() calls with a bool array, and switching to snprintf().

---
 drivers/edac/versalnet_edac.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)
drivers/edac/versalnet_edac.c +24 -5
index 1a1092793..128e9cd5f 100644
@@ -764,11 +764,12 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 {
 	u32 num_chans, rank, dwidth, config;
 	struct edac_mc_layer layers[2];
+	bool mc_added[NUM_CONTROLLERS] = { };
 	struct mem_ctl_info *mci;
 	struct device *dev;
 	enum dev_type dt;
 	char *name;
-	int rc, i;
+	int rc, i, j;
 
 	for (i = 0; i < NUM_CONTROLLERS; i++) {
 		config = priv->adec[CONF + i * ADEC_NUM];
@@ -813,11 +814,25 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 		priv->dwidth = dt;
 
 		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+		if (!dev) {
+			rc = -ENOMEM;
+			goto err_alloc;
+		}
+
 		dev->release = versal_edac_release;
 		name = kmalloc(32, GFP_KERNEL);
-		sprintf(name, "versal-net-ddrmc5-edac-%d", i);
+		if (!name) {
+			kfree(dev);
+			rc = -ENOMEM;
+			goto err_alloc;
+		}
+
+		snprintf(name, 32, "versal-net-ddrmc5-edac-%d", i);
 		dev->init_name = name;
 		rc = device_register(dev);
+		kfree(name);
+		if (rc)
+			put_device(dev);
 		if (rc)
 			goto err_alloc;
 
@@ -831,21 +846,25 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 			edac_printk(KERN_ERR, EDAC_MC, "Failed to register MC%d with EDAC core\n", i);
 			goto err_alloc;
 		}
+
+		mc_added[i] = true;
 	}
 	return 0;
 
 err_alloc:
-	while (i--) {
-		mci = priv->mci[i];
+	for (j = i; j >= 0; j--) {
+		mci = priv->mci[j];
 		if (!mci)
 			continue;
 
 		if (mci->pdev) {
 			device_unregister(mci->pdev);
-			edac_mc_del_mc(mci->pdev);
+			if (mc_added[j])
+				edac_mc_del_mc(mci->pdev);
 		}
 
 		edac_mc_free(mci);
+		priv->mci[j] = NULL;
 	}
 
 	return rc;
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 1 line · Eric-Terminal

init_versalnet() has several bugs in its error handling:

I'm waiting for a while now for Shubhrajyoti to do something about some outstanding issues there but no move. Might as well remove him from MAINTAINERS and let this driver rot...

Quoted 1 line

Your last name is "Terminal"?!?

Signature · 1 line
Small device. Typos and formatting crap

A message that is not in the archive (sent off-list or not yet received): CAKNPVZA36XvT7R+QWTOTvAeQNA-sZfnG-h5jV3FnKvKOxqiGtA@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

PATCH 1/1 EDAC/versalnet: Fix resource leaks and NULL derefs in init_versalnet()

From: Yufan Chen <[email protected]>

init_versalnet() has several bugs in its error handling:

- kzalloc() and kmalloc() return values are used without NULL checks, causing a NULL pointer dereference when allocation fails.

- The cleanup loop uses while (i--) which skips the current failing index, leaking the resources already allocated for that slot.

- edac_mc_del_mc() is called unconditionally during unwind, even for controllers that were never registered with edac_mc_add_mc().

- sprintf() is used instead of snprintf() on a fixed-size buffer.

Fix by adding NULL checks for dev and name allocations, replacing while (i--) with for (j = i; j >= 0; j--) to include the failing index, tracking successful edac_mc_add_mc() calls with a bool array, and switching to snprintf().

---
 drivers/edac/versalnet_edac.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)
drivers/edac/versalnet_edac.c +24 -5
index 1a1092793..128e9cd5f 100644
@@ -764,11 +764,12 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 {
 	u32 num_chans, rank, dwidth, config;
 	struct edac_mc_layer layers[2];
+	bool mc_added[NUM_CONTROLLERS] = { };
 	struct mem_ctl_info *mci;
 	struct device *dev;
 	enum dev_type dt;
 	char *name;
-	int rc, i;
+	int rc, i, j;
 
 	for (i = 0; i < NUM_CONTROLLERS; i++) {
 		config = priv->adec[CONF + i * ADEC_NUM];
@@ -813,11 +814,25 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 		priv->dwidth = dt;
 
 		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+		if (!dev) {
+			rc = -ENOMEM;
+			goto err_alloc;
+		}
+
 		dev->release = versal_edac_release;
 		name = kmalloc(32, GFP_KERNEL);
-		sprintf(name, "versal-net-ddrmc5-edac-%d", i);
+		if (!name) {
+			kfree(dev);
+			rc = -ENOMEM;
+			goto err_alloc;
+		}
+
+		snprintf(name, 32, "versal-net-ddrmc5-edac-%d", i);
 		dev->init_name = name;
 		rc = device_register(dev);
+		kfree(name);
+		if (rc)
+			put_device(dev);
 		if (rc)
 			goto err_alloc;
 
@@ -831,21 +846,25 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 			edac_printk(KERN_ERR, EDAC_MC, "Failed to register MC%d with EDAC core\n", i);
 			goto err_alloc;
 		}
+
+		mc_added[i] = true;
 	}
 	return 0;
 
 err_alloc:
-	while (i--) {
-		mci = priv->mci[i];
+	for (j = i; j >= 0; j--) {
+		mci = priv->mci[j];
 		if (!mci)
 			continue;
 
 		if (mci->pdev) {
 			device_unregister(mci->pdev);
-			edac_mc_del_mc(mci->pdev);
+			if (mc_added[j])
+				edac_mc_del_mc(mci->pdev);
 		}
 
 		edac_mc_free(mci);
+		priv->mci[j] = NULL;
 	}
 
 	return rc;
Signature · 1 line
2.47.3
maintainer
#5 lore raw
Quoted 15 lines · Eric-Terminal

From: Yufan Chen <[email protected]>

init_versalnet() has several bugs in its error handling:

- kzalloc() and kmalloc() return values are used without NULL checks, causing a NULL pointer dereference when allocation fails.

- The cleanup loop uses while (i--) which skips the current failing index, leaking the resources already allocated for that slot.

- edac_mc_del_mc() is called unconditionally during unwind, even for controllers that were never registered with edac_mc_add_mc().

- sprintf() is used instead of snprintf() on a fixed-size buffer.

Quoted 8 lines

Fix by adding NULL checks for dev and name allocations, replacing while (i--) with for (j = i; j >= 0; j--) to include the failing index, tracking successful edac_mc_add_mc() calls with a bool array, and switching to snprintf().

---
drivers/edac/versalnet_edac.c | 29 ++++++++++++++++++++++++-----

So that particular area of stinking goo is being dealt with here:

<https://lore.kernel.org/all/[email protected]/T/#u>

And I'd usually say you can take the current diffs, productize them and send them after testing.

However, testing is the problem here - I highly doubt you have access to the hardware and Shubhrajyoti is probably one of small number of people who can test it.

Except that he's not really moving here - this particular issue has been outstanding for at least three months.

I'll ping him when I get back from vacation to see whether there's still interest in this driver or I can remove it for lack of support.

Oh well.

Signature · 1 line
Small device. Typos and formatting crap
#6 lore raw
Quoted 1 line · Borislav Petkov

And I'd usually say you can take the current diffs, productize them and send them after testing.

Uh, yeah, that’s true — after all, most of us regular developers don’t exactly have an FPGA sitting around at home.

Quoted 1 line

However, testing is the problem here - I highly doubt you have access to the hardware and Shubhrajyoti is probably one of small number of people who can test it.

This is purely a logic bug in the code. What I did was just fix an error-handling path; it doesn’t change the normal execution flow at all. The issue itself can be derived through static analysis of the existing code, so it’s not something that depends on specific hardware behavior.

Quoted 1 line

Except that he's not really moving here - this particular issue has been outstanding for at least three months.

If the driver isn’t actively maintained… well… I can understand that too. Still, this patch is very low risk, and merging it shouldn’t really add to your workload. Maybe straightforward logic fixes like this could be considered based on static review alone? At the very least, it would help improve the baseline code quality. :)

Anyway, enjoy your vacation.

maintainer Reviewed-by
#7 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 10 lines

-----Original Message-----
From: Eric_Terminal <[email protected]>
Sent: Monday, February 23, 2026 8:28 PM
To: Borislav Petkov <[email protected]>
Cc: Datta, Shubhrajyoti <[email protected]>; Tony Luck <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH] EDAC/versalnet: Fix resource leaks and NULL derefs in init_versalnet()

Quoted 8 lines

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 1 line · Borislav Petkov

And I'd usually say you can take the current diffs, productize them and send

them after testing.

I was out last week because of a personal emergency. My apologies on the delay. BTW I was able to test the patch.

Feel free to add my

However are you planning to send a v2 correcting the small nits like name etc?

#8 lore raw

PATCH v2 1/1 EDAC/versalnet: Fix resource leaks and NULL derefs in init_versalnet()

From: Yufan Chen <[email protected]>

init_versalnet() has several bugs in its error handling. kzalloc() and kmalloc() return values are used without NULL checks, causing a NULL pointer dereference when allocation fails. The cleanup loop uses while (i--) which skips the current failing index, leaking the resources already allocated for that slot. edac_mc_del_mc() is called unconditionally during unwind, even for controllers that were never registered with edac_mc_add_mc(). Also, sprintf() is used instead of snprintf() on a fixed-size buffer.

Fix by adding NULL checks for dev and name allocations, replacing while (i--) with for (j = i; j >= 0; j--) to include the failing index, tracking successful edac_mc_add_mc() calls with a bool array, and switching to snprintf().

---
v2: Correct Signed-off-by name and add Reviewed-by tag. Fix commit message formatting.

 drivers/edac/versalnet_edac.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)
drivers/edac/versalnet_edac.c +24 -5
index 1a1092793..128e9cd5f 100644
@@ -764,11 +764,12 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 {
 	u32 num_chans, rank, dwidth, config;
 	struct edac_mc_layer layers[2];
+	bool mc_added[NUM_CONTROLLERS] = { };
 	struct mem_ctl_info *mci;
 	struct device *dev;
 	enum dev_type dt;
 	char *name;
-	int rc, i;
+	int rc, i, j;
 
 	for (i = 0; i < NUM_CONTROLLERS; i++) {
 		config = priv->adec[CONF + i * ADEC_NUM];
@@ -813,11 +814,25 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 		priv->dwidth = dt;
 
 		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+		if (!dev) {
+			rc = -ENOMEM;
+			goto err_alloc;
+		}
+
 		dev->release = versal_edac_release;
 		name = kmalloc(32, GFP_KERNEL);
-		sprintf(name, "versal-net-ddrmc5-edac-%d", i);
+		if (!name) {
+			kfree(dev);
+			rc = -ENOMEM;
+			goto err_alloc;
+		}
+
+		snprintf(name, 32, "versal-net-ddrmc5-edac-%d", i);
 		dev->init_name = name;
 		rc = device_register(dev);
+		kfree(name);
+		if (rc)
+			put_device(dev);
 		if (rc)
 			goto err_alloc;
 
@@ -831,21 +846,25 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 			edac_printk(KERN_ERR, EDAC_MC, "Failed to register MC%d with EDAC core\n", i);
 			goto err_alloc;
 		}
+
+		mc_added[i] = true;
 	}
 	return 0;
 
 err_alloc:
-	while (i--) {
-		mci = priv->mci[i];
+	for (j = i; j >= 0; j--) {
+		mci = priv->mci[j];
 		if (!mci)
 			continue;
 
 		if (mci->pdev) {
 			device_unregister(mci->pdev);
-			edac_mc_del_mc(mci->pdev);
+			if (mc_added[j])
+				edac_mc_del_mc(mci->pdev);
 		}
 
 		edac_mc_free(mci);
+		priv->mci[j] = NULL;
 	}
 
 	return rc;
Signature · 1 line
2.47.3
maintainer
#9 lore raw
Quoted 16 lines · Eric-Terminal

From: Yufan Chen <[email protected]>

init_versalnet() has several bugs in its error handling. kzalloc() and kmalloc() return values are used without NULL checks, causing a NULL pointer dereference when allocation fails. The cleanup loop uses while (i--) which skips the current failing index, leaking the resources already allocated for that slot. edac_mc_del_mc() is called unconditionally during unwind, even for controllers that were never registered with edac_mc_add_mc(). Also, sprintf() is used instead of snprintf() on a fixed-size buffer.

Fix by adding NULL checks for dev and name allocations, replacing while (i--) with for (j = i; j >= 0; j--) to include the failing index, tracking successful edac_mc_add_mc() calls with a bool array, and switching to snprintf().

Quoted 7 lines

---
v2: Correct Signed-off-by name and add Reviewed-by tag. Fix commit message formatting.

 drivers/edac/versalnet_edac.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

No, thanks, we'll do the proper cleanup here:

https://lore.kernel.org/all/[email protected]/T/#u

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#10 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 37 lines

-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: Thursday, February 26, 2026 6:30 PM
To: Eric-Terminal <[email protected]>
Cc: Datta, Shubhrajyoti <[email protected]>; Tony Luck <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH v2] EDAC/versalnet: Fix resource leaks and NULL derefs in init_versalnet()

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 16 lines · Eric-Terminal

From: Yufan Chen <[email protected]>

init_versalnet() has several bugs in its error handling. kzalloc() and kmalloc() return values are used without NULL checks, causing a NULL pointer dereference when allocation fails. The cleanup loop uses while (i--) which skips the current failing index, leaking the resources already allocated for that slot. edac_mc_del_mc() is called unconditionally during unwind, even for controllers that were never registered with edac_mc_add_mc(). Also, sprintf() is used instead of snprintf() on a fixed-size buffer.

Fix by adding NULL checks for dev and name allocations, replacing while (i--) with for (j = i; j >= 0; j--) to include the failing index, tracking successful edac_mc_add_mc() calls with a bool array, and switching to snprintf().

Quoted 4 lines

---
v2: Correct Signed-off-by name and add Reviewed-by tag. Fix commit message

formatting.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines
 drivers/edac/versalnet_edac.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

No, thanks, we'll do the proper cleanup here:

https://lore.kernel.org/all/20251104093932.3838876-1-
[email protected]/T/#u

My apologies somehow I had missed it.
Tested it on the hardware.

Quoted 6 lines

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

#11 lore raw
In reply to a message not in the archive
Quoted 3 lines · Borislav Petkov

First of all,

do not top-post when replying to community mails.

My apologies for the formatting errors and for accidentally taking this discussion off-list. I'm a newcomer and still learning the etiquette.

Quoted 3 lines

Then, when you reply, hit reply-to-all in your mail client. The discussions we have are not private ones but are documented on public mailing lists for a reason.

Understood. I have added the mailing lists back to CC and will ensure all future technical discussions remain public.

Quoted 7 lines

Then, about this particular question:

"...then you just add a line saying:

using your real name (sorry, no pseudonyms or anonymous contributions.)"

I see. Thank you for the clarification. My real name is Yufan Chen.

I have thoroughly read the submission documentation and have just resubmitted the patch as V2 with the corrected

Best regards,
Yufan Chen

maintainer
#12 lore raw
Quoted 2 lines · Eric_Terminal

I have thoroughly read the submission documentation and have just resubmitted the patch as V2 with the corrected

In my not-so-short experience no new submitter just reads all of the submitting-patches document without complaining at least a little and replies with a new version of the patch - all that in three hours, and all that in way too polished English.

If I look at the actual diff, am I going to find a bunch of nonsensical AI slop and am I going to waste my time talking to an AI bot as a result?

#13 lore raw
Quoted 2 lines · Borislav Petkov

In my not-so-short experience no new submitter just reads all of the submitting-patches document without complaining at least a little and replies with a new version of the patch - all that in three hours, and all that in way too polished English.

To explain the "way too polished English": English is not my native language. Because I want to show proper respect to the kernel community, I relied on translation and grammar tools to make sure my replies were polite and appropriate. That is why the text might look artificially perfect.

As for not complaining: I don't know how other submitters do it, but as a newcomer, I honestly didn't dare to complain on the mailing list. I simply assumed that complaining would leave a bad impression and might get my patch rejected. So I just tried my best to read the documentation and follow the rules as quickly as possible.

Regarding the speed of sending a "new version" in three hours: it wasn't a rewrite of the patch. I merely updated the Signed-off-by tag to my real name, re-signed it with patatt, and sent it out. The code itself didn't change at all, which is why it was fast.

Quoted 1 line

If I look at the actual diff, am I going to find a bunch of nonsensical AI slop and am I going to waste my time talking to an AI bot as a result?

If I were a fully automated AI bot, I probably wouldn't have made the very human mistakes of top-posting and forgetting to "Reply-to-all" in my email before the last one. :)

I completely understand your concern about AI-generated spam.
However, the NULL pointer dereferences and memory leaks in init_versalnet() are genuine logical flaws. I hope you can take a brief moment to review the actual v2 diff. If there is any technical issue with my code, I am more than happy to learn and correct it.

Best regards,

Yufan Chen (Eric Terminal)

maintainer
#14 lore raw
Quoted 5 lines · Eric_Terminal

To explain the "way too polished English": English is not my native language. Because I want to show proper respect to the kernel community, I relied on translation and grammar tools to make sure my replies were polite and appropriate. That is why the text might look artificially perfect.

Such quality must come from a LLM-like tool...

Quoted 5 lines

As for not complaining: I don't know how other submitters do it, but as a newcomer, I honestly didn't dare to complain on the mailing list. I simply assumed that complaining would leave a bad impression and might get my patch rejected. So I just tried my best to read the documentation and follow the rules as quickly as possible.

You should never aim for "quickly" but for correctly. Never rush your replies.

Quoted 1 line

If I were a fully automated AI bot, I probably

So you are using some AI to generate your mails? You should state that in your commit message.

But I'll take a look at your patch first...

Signature · 1 line
Small device. Typos and formatting crap
#15 lore raw
Quoted 1 line · Borislav Petkov

So you are using some AI to generate your mails? You should state that in your commit message.

Okay, I get it. I indeed used AI to polish my emails... it even deleted all my emojis. If I wrote directly, it would probably sound like this. After all, my own English isn't as "inspired" as the AI's.

Do I need to resubmit and declare this in the commit message?