Skip to contentSkip to conversation

ipmi: kcs_bmc: handle devm_kasprintf() failure case

PATCH In review Nicholas Mc Guire 3 participants 4 messages Nov 21, 2018 – Nov 28, 2018 IPMI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipmi: kcs_bmc: handle devm_kasprintf() failure case

devm_kasprintf() may return NULL if internal allocation failed so this assignment is not safe. Moved the error exit path and added the !NULL which then allows the devres manager to take care of cleanup.

Fixes cd2315d471f4 ("ipmi: kcs_bmc: don't change device name")

---

Problem located with experimental coccinelle script

Patch was compile tested with: aspeed_g5_defconfig (implies CONFIG_IPMI_KCS_BMC=y)

Patch is against 4.20-rc3 (localversion-next is next-20181121)

 drivers/char/ipmi/kcs_bmc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/char/ipmi/kcs_bmc.c +3 -2
index e6124bd..86d29d2 100644
@@ -440,12 +440,13 @@ struct kcs_bmc *kcs_bmc_alloc(struct device *dev, int sizeof_priv, u32 channel)
 	kcs_bmc->data_in = devm_kmalloc(dev, KCS_MSG_BUFSIZ, GFP_KERNEL);
 	kcs_bmc->data_out = devm_kmalloc(dev, KCS_MSG_BUFSIZ, GFP_KERNEL);
 	kcs_bmc->kbuffer = devm_kmalloc(dev, KCS_MSG_BUFSIZ, GFP_KERNEL);
-	if (!kcs_bmc->data_in || !kcs_bmc->data_out || !kcs_bmc->kbuffer)
-		return NULL;
 
 	kcs_bmc->miscdev.minor = MISC_DYNAMIC_MINOR;
 	kcs_bmc->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "%s%u",
 					       DEVICE_NAME, channel);
+	if (!kcs_bmc->data_in || !kcs_bmc->data_out || !kcs_bmc->kbuffer ||
+	    !kcs_bmc->miscdev.name)
+		return NULL;
 	kcs_bmc->miscdev.fops = &kcs_bmc_fops;
 
 	return kcs_bmc;
Signature · 1 line
2.1.4
#1 lore raw
Quoted 3 lines · Nicholas Mc Guire

devm_kasprintf() may return NULL if internal allocation failed so this assignment is not safe. Moved the error exit path and added the !NULL which then allows the devres manager to take care of cleanup.

Added the original author.  This looks correct to me, I've included it, but I would like Haiyue to comment, if possible.

Thanks,

-corey

Quoted 34 lines
Fixes cd2315d471f4 ("ipmi: kcs_bmc: don't change device name")

---

Problem located with experimental coccinelle script

Patch was compile tested with: aspeed_g5_defconfig (implies CONFIG_IPMI_KCS_BMC=y)

Patch is against 4.20-rc3 (localversion-next is next-20181121)

  drivers/char/ipmi/kcs_bmc.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/char/ipmi/kcs_bmc.c +3 -2
index e6124bd..86d29d2 100644
@@ -440,12 +440,13 @@ struct kcs_bmc *kcs_bmc_alloc(struct device *dev, int sizeof_priv, u32 channel)
  	kcs_bmc->data_in = devm_kmalloc(dev, KCS_MSG_BUFSIZ, GFP_KERNEL);
  	kcs_bmc->data_out = devm_kmalloc(dev, KCS_MSG_BUFSIZ, GFP_KERNEL);
  	kcs_bmc->kbuffer = devm_kmalloc(dev, KCS_MSG_BUFSIZ, GFP_KERNEL);
-	if (!kcs_bmc->data_in || !kcs_bmc->data_out || !kcs_bmc->kbuffer)
-		return NULL;
 
  	kcs_bmc->miscdev.minor = MISC_DYNAMIC_MINOR;
  	kcs_bmc->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "%s%u",
  					       DEVICE_NAME, channel);
+	if (!kcs_bmc->data_in || !kcs_bmc->data_out || !kcs_bmc->kbuffer ||
+	    !kcs_bmc->miscdev.name)
+		return NULL;
  	kcs_bmc->miscdev.fops = &kcs_bmc_fops;
 
  	return kcs_bmc;
Reviewed-by
#2 lore raw

[Resend for wrong reply HTML format mail]

Great check for making kcs_bmc module be more stable and handle things gracefully.

My tag if needed.

Quoted 16 lines · Corey Minyard
Quoted 3 lines · Nicholas Mc Guire

devm_kasprintf() may return NULL if internal allocation failed so this assignment is not safe. Moved the error exit path and added the !NULL which then allows the devres manager to take care of cleanup.

Added the original author.  This looks correct to me, I've included it, but I would like Haiyue to comment, if possible.

Thanks,

-corey

Quoted 2 lines
Fixes cd2315d471f4 ("ipmi: kcs_bmc: don't change device name")
#3 lore raw
Quoted 7 lines · Haiyue

[Resend for wrong reply HTML format mail]

Great check for making kcs_bmc module be more stable and handle things gracefully.

My tag if needed.

Thanks for the review, it's included.

-corey

Quoted 23 lines · Corey Minyard
Quoted 6 lines · Nicholas Mc Guire
Quoted 3 lines

devm_kasprintf() may return NULL if internal allocation failed so this assignment is not safe. Moved the error exit path and added the !NULL which then allows the devres manager to take care of cleanup.

Quoted 10 lines

Added the original author.  This looks correct to me, I've included it, but I would like Haiyue to comment, if possible.

Thanks,

-corey

Quoted 2 lines
Fixes cd2315d471f4 ("ipmi: kcs_bmc: don't change device name")

_______________________________________________
Openipmi-developer mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openipmi-developer