Skip to contentSkip to conversation

soc: bcm: Check for NULL return of devm_kzalloc()

PATCH Applied QintaoShen 3 participants 5 messages Mar 24, 2022 – Apr 19, 2022 MIPS

lore
Share & tools
lore

QintaoShen

author
#0 lore raw

PATCH 1/1 soc: bcm: Check for NULL return of devm_kzalloc()

As the potential failuer of allocation, devm_kzalloc() may return NULL. Then the 'pd->pmb' and the follow lines of code may bring null pointer dereference.

Therefore, it is better to check the return value of devm_kzalloc() to avoid this confusion.

---
 drivers/soc/bcm/bcm63xx/bcm-pmb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/soc/bcm/bcm63xx/bcm-pmb.c +2 -0
index 7bbe46e..55bf389 100644
@@ -311,6 +311,8 @@ static int bcm_pmb_probe(struct platform_device *pdev)
 
 	for (e = table; e->name; e++) {
 		struct bcm_pmb_pm_domain *pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
+		if (!pd)
+			return -ENOMEM;
 
 		pd->pmb = pmb;
 		pd->data = e;
Signature · 1 line
2.7.4
#1 lore raw

+Rafal,

Quoted 1 line · QintaoShen

As the potential failuer of allocation, devm_kzalloc() may return NULL.

s/failuer/failure/

Quoted 5 lines

Then the 'pd->pmb' and the follow lines of code may bring null pointer dereference.

Therefore, it is better to check the return value of devm_kzalloc() to avoid this confusion.

Fixes 8bcac4011ebe ("soc: bcm: add PM driver for Broadcom's PMB")
Quoted 9 lines
---
  drivers/soc/bcm/bcm63xx/bcm-pmb.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/soc/bcm/bcm63xx/bcm-pmb.c +0 -0
index 7bbe46e..55bf389 100644

drivers/soc/bcm/bcm63xx/bcm-pmb.c static int bcm_pmb_probe(struct platform_device *pdev) · line 315

Quoted 5 lines
drivers/soc/bcm/bcm63xx/bcm-pmb.c +2 -0
@@ -311,6 +311,8 @@ static int bcm_pmb_probe(struct platform_device *pdev)
 
  	for (e = table; e->name; e++) {
  		struct bcm_pmb_pm_domain *pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
+		if (!pd)
+			return -ENOMEM;

I am of two minds as to what the appropriate behavior could be here, we could equally use an:

		if (!pd)
			continue;

or do what you are doing.

Quoted 3 lines
drivers/soc/bcm/bcm63xx/bcm-pmb.c +0 -0
  		pd->pmb = pmb;
  		pd->data = e;
Signature · 1 line
Florian
#2 lore raw
In reply to QintaoShen

Hello!

s/return/result/ in the subject.

Quoted 2 lines · QintaoShen

As the potential failuer of allocation, devm_kzalloc() may return NULL. Then the 'pd->pmb' and the follow lines of code may bring null pointer dereference.

Following.

Quoted 12 lines

Therefore, it is better to check the return value of devm_kzalloc() to avoid this confusion.

---
 drivers/soc/bcm/bcm63xx/bcm-pmb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/soc/bcm/bcm63xx/bcm-pmb.c +0 -0
index 7bbe46e..55bf389 100644

drivers/soc/bcm/bcm63xx/bcm-pmb.c static int bcm_pmb_probe(struct platform_device *pdev) · line 313

Quoted 3 lines
drivers/soc/bcm/bcm63xx/bcm-pmb.c +0 -0
@@ -311,6 +311,8 @@ static int bcm_pmb_probe(struct platform_device *pdev)
 
 	for (e = table; e->name; e++) {
 		struct bcm_pmb_pm_domain *pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);

Please keep an empty line after declaration.

drivers/soc/bcm/bcm63xx/bcm-pmb.c static int bcm_pmb_probe(struct platform_device *pdev) · line 318

Quoted 5 lines
drivers/soc/bcm/bcm63xx/bcm-pmb.c +2 -0
+		if (!pd)
+			return -ENOMEM;
 
 		pd->pmb = pmb;
 		pd->data = e;

MBR, Sergey

author
#3 lore raw

OK. Here is the new version.

---
 drivers/soc/bcm/bcm63xx/bcm-pmb.c | 3 +++

 1 file changed, 3 insertions(+)
drivers/soc/bcm/bcm63xx/bcm-pmb.c +0 -0
index 7bbe46e..cc20ffb 100644
@@ -312,6 +312,9 @@ static int bcm_pmb_probe(struct platform_device *pdev)

     for (e = table; e->name; e++) {
         struct bcm_pmb_pm_domain *pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);

+        if (!pd)
+            return -ENOMEM;
+
         pd->pmb = pmb;
         pd->data = e;
         pd->genpd.name = e->name;

Signature · 1 line
2.7.4
Quoted 33 lines · Sergey Shtylyov

Hello!

s/return/result/ in the subject.

Quoted 2 lines · QintaoShen

As the potential failuer of allocation, devm_kzalloc() may return NULL. Then the 'pd->pmb' and the follow lines of code may bring null pointer dereference.

Following.

Quoted 12 lines

Therefore, it is better to check the return value of devm_kzalloc() to avoid this confusion.

---
  drivers/soc/bcm/bcm63xx/bcm-pmb.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/soc/bcm/bcm63xx/bcm-pmb.c +0 -0
index 7bbe46e..55bf389 100644

drivers/soc/bcm/bcm63xx/bcm-pmb.c static int bcm_pmb_probe(struct platform_device *pdev) · line 313

Quoted 3 lines
drivers/soc/bcm/bcm63xx/bcm-pmb.c +0 -0
@@ -311,6 +311,8 @@ static int bcm_pmb_probe(struct platform_device *pdev)
 
  	for (e = table; e->name; e++) {
  		struct bcm_pmb_pm_domain *pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);

Please keep an empty line after declaration.

drivers/soc/bcm/bcm63xx/bcm-pmb.c static int bcm_pmb_probe(struct platform_device *pdev) · line 318

Quoted 5 lines
drivers/soc/bcm/bcm63xx/bcm-pmb.c +2 -0
+		if (!pd)
+			return -ENOMEM;
 
  		pd->pmb = pmb;
  		pd->data = e;

MBR, Sergey

#4 lore raw
In reply to QintaoShen
Quoted 6 lines · QintaoShen

As the potential failuer of allocation, devm_kzalloc() may return NULL. Then the 'pd->pmb' and the follow lines of code may bring null pointer dereference.

Therefore, it is better to check the return value of devm_kzalloc() to avoid this confusion.

Applied to https://github.com/Broadcom/stblinux/commits/drivers/next, thanks!

I modified your patch to have a Fixes: tag as replied, and follow Sergey's style recommendation.

Signature · 1 line
Florian