diff mbox series

[v1] soc: bcm: Check for NULL return of devm_kzalloc()

Message ID 1648110940-4684-1-git-send-email-unSimple1993@163.com (mailing list archive)
State Handled Elsewhere
Headers show
Series [v1] soc: bcm: Check for NULL return of devm_kzalloc() | expand

Commit Message

QintaoShen March 24, 2022, 8:35 a.m. UTC
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.

Signed-off-by: QintaoShen <unSimple1993@163.com>
---
 drivers/soc/bcm/bcm63xx/bcm-pmb.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Florian Fainelli March 24, 2022, 5:04 p.m. UTC | #1
+Rafal,

On 3/24/22 01:35, QintaoShen wrote:
> As the potential failuer of allocation, devm_kzalloc() may return NULL.

s/failuer/failure/

> 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.
> 
> Signed-off-by: QintaoShen <unSimple1993@163.com>

Fixes: 8bcac4011ebe ("soc: bcm: add PM driver for Broadcom's PMB")

> ---
>   drivers/soc/bcm/bcm63xx/bcm-pmb.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/soc/bcm/bcm63xx/bcm-pmb.c b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
> index 7bbe46e..55bf389 100644
> --- a/drivers/soc/bcm/bcm63xx/bcm-pmb.c
> +++ b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
> @@ -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.

>   
>   		pd->pmb = pmb;
>   		pd->data = e;
Sergey Shtylyov March 25, 2022, 9:02 a.m. UTC | #2
Hello!

   s/return/result/ in the subject.

On 3/24/22 11:35 AM, QintaoShen wrote:

> 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.

> Therefore, it is better to check the return value of devm_kzalloc() to avoid this confusion.
> 
> Signed-off-by: QintaoShen <unSimple1993@163.com>
> ---
>  drivers/soc/bcm/bcm63xx/bcm-pmb.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/soc/bcm/bcm63xx/bcm-pmb.c b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
> index 7bbe46e..55bf389 100644
> --- a/drivers/soc/bcm/bcm63xx/bcm-pmb.c
> +++ b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
> @@ -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.

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

MBR, Sergey
QintaoShen March 28, 2022, 4:13 a.m. UTC | #3
OK. Here is the new version.

Signed-off-by: QintaoShen <unSimple1993@163.com>
---
  drivers/soc/bcm/bcm63xx/bcm-pmb.c | 3 +++
  1 file changed, 3 insertions(+)

diff --git a/drivers/soc/bcm/bcm63xx/bcm-pmb.c 
b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
index 7bbe46e..cc20ffb 100644
--- a/drivers/soc/bcm/bcm63xx/bcm-pmb.c
+++ b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
@@ -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;
Florian Fainelli April 19, 2022, 6:33 p.m. UTC | #4
On 3/24/2022 1:35 AM, QintaoShen wrote:
> 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.
> 
> Signed-off-by: QintaoShen <unSimple1993@163.com>

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.
diff mbox series

Patch

diff --git a/drivers/soc/bcm/bcm63xx/bcm-pmb.c b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
index 7bbe46e..55bf389 100644
--- a/drivers/soc/bcm/bcm63xx/bcm-pmb.c
+++ b/drivers/soc/bcm/bcm63xx/bcm-pmb.c
@@ -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;