linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc()
@ 2022-03-24  8:35 QintaoShen
  2022-03-24 17:04 ` Florian Fainelli
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: QintaoShen @ 2022-03-24  8:35 UTC (permalink / raw)
  To: f.fainelli; +Cc: bcm-kernel-feedback-list, linux-mips, linux-kernel, 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.

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);
+		if (!pd)
+			return -ENOMEM;
 
 		pd->pmb = pmb;
 		pd->data = e;
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc()
  2022-03-24  8:35 [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc() QintaoShen
@ 2022-03-24 17:04 ` Florian Fainelli
  2022-03-25  9:02 ` Sergey Shtylyov
  2022-04-19 18:33 ` Florian Fainelli
  2 siblings, 0 replies; 5+ messages in thread
From: Florian Fainelli @ 2022-03-24 17:04 UTC (permalink / raw)
  To: QintaoShen, Rafał Miłecki
  Cc: bcm-kernel-feedback-list, linux-mips, linux-kernel

+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;


-- 
Florian

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc()
  2022-03-24  8:35 [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc() QintaoShen
  2022-03-24 17:04 ` Florian Fainelli
@ 2022-03-25  9:02 ` Sergey Shtylyov
  2022-03-28  4:13   ` QintaoShen
  2022-04-19 18:33 ` Florian Fainelli
  2 siblings, 1 reply; 5+ messages in thread
From: Sergey Shtylyov @ 2022-03-25  9:02 UTC (permalink / raw)
  To: QintaoShen, f.fainelli; +Cc: bcm-kernel-feedback-list, linux-mips, linux-kernel

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc()
  2022-03-25  9:02 ` Sergey Shtylyov
@ 2022-03-28  4:13   ` QintaoShen
  0 siblings, 0 replies; 5+ messages in thread
From: QintaoShen @ 2022-03-28  4:13 UTC (permalink / raw)
  To: Sergey Shtylyov
  Cc: bcm-kernel-feedback-list, f.fainelli, linux-mips, linux-kernel

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;
-- 
2.7.4

在 2022/3/25 下午5:02, Sergey Shtylyov 写道:
> 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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc()
  2022-03-24  8:35 [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc() QintaoShen
  2022-03-24 17:04 ` Florian Fainelli
  2022-03-25  9:02 ` Sergey Shtylyov
@ 2022-04-19 18:33 ` Florian Fainelli
  2 siblings, 0 replies; 5+ messages in thread
From: Florian Fainelli @ 2022-04-19 18:33 UTC (permalink / raw)
  To: QintaoShen, f.fainelli; +Cc: bcm-kernel-feedback-list, linux-mips, linux-kernel



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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-04-19 18:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24  8:35 [PATCH v1] soc: bcm: Check for NULL return of devm_kzalloc() QintaoShen
2022-03-24 17:04 ` Florian Fainelli
2022-03-25  9:02 ` Sergey Shtylyov
2022-03-28  4:13   ` QintaoShen
2022-04-19 18:33 ` Florian Fainelli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).