diff mbox

reset: hisilicon: fix potential NULL pointer dereference

Message ID 20180719015845.GA10768@embeddedor.com (mailing list archive)
State Changes Requested, archived
Headers show

Commit Message

Gustavo A. R. Silva July 19, 2018, 1:58 a.m. UTC
There is a potential execution path in which function
platform_get_resource() returns NULL. If this happens,
we will end up having a NULL pointer dereference.

Fix this by adding asanity check in order to avoid a
NULL pointer dereference.

This code was detected with the help of Coccinelle.

Cc: stable@vger.kernel.org
Fixes: 97b7129cd2af ("reset: hisilicon: change the definition of hisi_reset_init")
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 drivers/clk/hisilicon/reset.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Stephen Boyd July 25, 2018, 11:45 p.m. UTC | #1
Quoting Gustavo A. R. Silva (2018-07-18 18:58:45)
> There is a potential execution path in which function
> platform_get_resource() returns NULL. If this happens,
> we will end up having a NULL pointer dereference.
> 
> Fix this by adding asanity check in order to avoid a
> NULL pointer dereference.
> 
> This code was detected with the help of Coccinelle.
> 
> Cc: stable@vger.kernel.org
> Fixes: 97b7129cd2af ("reset: hisilicon: change the definition of hisi_reset_init")
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>  drivers/clk/hisilicon/reset.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/clk/hisilicon/reset.c b/drivers/clk/hisilicon/reset.c
> index 2a5015c..5dfb48b 100644
> --- a/drivers/clk/hisilicon/reset.c
> +++ b/drivers/clk/hisilicon/reset.c
> @@ -109,6 +109,9 @@ struct hisi_reset_controller *hisi_reset_init(struct platform_device *pdev)
>                 return NULL;
>  
>         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +       if (!res)
> +               return NULL;
> +
>         rstc->membase = devm_ioremap(&pdev->dev,
>                                 res->start, resource_size(res));

Why can't we use devm_ioremap_resource() here instead?

--
To unsubscribe from this list: send the line "unsubscribe linux-clk" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Gustavo A. R. Silva July 26, 2018, 12:25 a.m. UTC | #2
Hi Stephen,

On 07/25/2018 06:45 PM, Stephen Boyd wrote:
> Quoting Gustavo A. R. Silva (2018-07-18 18:58:45)
>> There is a potential execution path in which function
>> platform_get_resource() returns NULL. If this happens,
>> we will end up having a NULL pointer dereference.
>>
>> Fix this by adding asanity check in order to avoid a
>> NULL pointer dereference.
>>
>> This code was detected with the help of Coccinelle.
>>
>> Cc: stable@vger.kernel.org
>> Fixes: 97b7129cd2af ("reset: hisilicon: change the definition of hisi_reset_init")
>> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
>> ---
>>  drivers/clk/hisilicon/reset.c | 3 +++
>>  1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/clk/hisilicon/reset.c b/drivers/clk/hisilicon/reset.c
>> index 2a5015c..5dfb48b 100644
>> --- a/drivers/clk/hisilicon/reset.c
>> +++ b/drivers/clk/hisilicon/reset.c
>> @@ -109,6 +109,9 @@ struct hisi_reset_controller *hisi_reset_init(struct platform_device *pdev)
>>                 return NULL;
>>  
>>         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>> +       if (!res)
>> +               return NULL;
>> +
>>         rstc->membase = devm_ioremap(&pdev->dev,
>>                                 res->start, resource_size(res));
> 
> Why can't we use devm_ioremap_resource() here instead?
> 

You're right. I think we can perfectly use devm_ioremap_resource here and remove the null check.

I'll send patch for this shortly.

Thanks
--
Gustavo


--
To unsubscribe from this list: send the line "unsubscribe linux-clk" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/clk/hisilicon/reset.c b/drivers/clk/hisilicon/reset.c
index 2a5015c..5dfb48b 100644
--- a/drivers/clk/hisilicon/reset.c
+++ b/drivers/clk/hisilicon/reset.c
@@ -109,6 +109,9 @@  struct hisi_reset_controller *hisi_reset_init(struct platform_device *pdev)
 		return NULL;
 
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return NULL;
+
 	rstc->membase = devm_ioremap(&pdev->dev,
 				res->start, resource_size(res));
 	if (!rstc->membase)