Skip to contentSkip to conversation

pstore/ram: Return directly after a failed kasprintf() call in ramoops_init_prz()

PATCH In review Markus Elfring 3 participants 4 messages Jan 17, 2024 – Jan 18, 2024

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

PATCH 1/1 pstore/ram: Return directly after a failed kasprintf() call in ramoops_init_prz()

From: Markus Elfring <[email protected]>
Date: Wed, 17 Jan 2024 21:09:22 +0100

The result from a call of the function “kasprintf” was passed to a subsequent function call without checking for a null pointer before (according to a memory allocation failure).
This issue was detected by using the Coccinelle software.

Thus return directly after a failed kasprintf() call.

Fixes 1227daa43bce1 ("pstore/ram: Clarify resource reservation labels")
---
 fs/pstore/ram.c | 3 +++
 1 file changed, 3 insertions(+)
fs/pstore/ram.c +3 -0
index 88b34fdbf759..1a673a4af17c 100644
@@ -595,6 +595,9 @@ static int ramoops_init_prz(const char *name,
 	}
 
 	label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
+	if (!label)
+		return -ENOMEM;
+
 	*prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
 				  cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
 	kfree(label);

--
2.43.0

#1 lore raw

Hi Markus,
Thanks for your patch.

Quoted 21 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 17 Jan 2024 21:09:22 +0100

The result from a call of the function “kasprintf” was passed to a subsequent function call without checking for a null pointer before (according to a memory allocation failure).
This issue was detected by using the Coccinelle software.

Thus return directly after a failed kasprintf() call.

Fixes 1227daa43bce1 ("pstore/ram: Clarify resource reservation labels")
---
  fs/pstore/ram.c | 3 +++
  1 file changed, 3 insertions(+)
fs/pstore/ram.c +0 -0
index 88b34fdbf759..1a673a4af17c 100644

fs/pstore/ram.c static int ramoops_init_prz(const char *name, · line 600

Quoted 6 lines
fs/pstore/ram.c +3 -0
@@ -595,6 +595,9 @@ static int ramoops_init_prz(const char *name,
  	}
 
  	label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
+	if (!label)
+		return -ENOMEM;
+

This part looks good to me.

Commit 1227daa43bce1 ("pstore/ram: Clarify resource reservation labels") introduce another two more kasprintf in the ramoops_init_przs.

Could you fix it together?

Quoted 6 lines
fs/pstore/ram.c +0 -1
  	*prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
  				  cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
  	kfree(label);
--

2.43.0

Signature · 2 lines
Thanks,
  Kunwu
#2 lore raw
In reply to Markus Elfring
Quoted 21 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 17 Jan 2024 21:09:22 +0100

The result from a call of the function “kasprintf” was passed to a subsequent function call without checking for a null pointer before (according to a memory allocation failure).
This issue was detected by using the Coccinelle software.

Thus return directly after a failed kasprintf() call.

Fixes 1227daa43bce1 ("pstore/ram: Clarify resource reservation labels")
---
 fs/pstore/ram.c | 3 +++
 1 file changed, 3 insertions(+)
fs/pstore/ram.c +0 -0
index 88b34fdbf759..1a673a4af17c 100644

fs/pstore/ram.c static int ramoops_init_prz(const char *name, · line 603

Quoted 9 lines
fs/pstore/ram.c +3 -0
@@ -595,6 +595,9 @@ static int ramoops_init_prz(const char *name,
 	}
 
 	label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
+	if (!label)
+		return -ENOMEM;
+
 	*prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
 				  cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
 	kfree(label);

This patch is fine as a clean up, but I think it's useful to say that if you pass a NULL label to persistent_ram_new() then it will return an error. It won't crash. So this patch is a nice cleanup but it's not a bug fix.

regards,
dan carpenter

#3 lore raw
Quoted 4 lines
Quoted 4 lines

The result from a call of the function “kasprintf” was passed to a subsequent function call without checking for a null pointer before (according to a memory allocation failure).
This issue was detected by using the Coccinelle software.

…

Quoted 4 lines
Quoted 4 lines

+++ b/fs/pstore/ram.c

(quoted diff) +0 -0
@@ -595,6 +595,9 @@ static int ramoops_init_prz(const char *name,
 	}
 
Quoted 11 lines

fs/pstore/ram.c static int ramoops_init_prz(const char *name, · line 603

Quoted 7 lines
fs/pstore/ram.c +3 -0
 	label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
+	if (!label)
+		return -ENOMEM;
+
 	*prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
 				  cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
 	kfree(label);

This patch is fine as a clean up, but I think it's useful to say that if you pass a NULL label to persistent_ram_new() then it will return an error.

…

Will it become helpful to annotate the corresponding function input parameter for null pointer tolerance anyhow?

Regards,
Markus