[-next] gpio: ath79: Fix potential NULL dereference in ath79_gpio_probe()

Message ID 1515047483-168600-1-git-send-email-weiyongjun1@huawei.com
State New
Headers show
Series
  • [-next] gpio: ath79: Fix potential NULL dereference in ath79_gpio_probe()
Related show

Commit Message

Wei Yongjun Jan. 4, 2018, 6:31 a.m.
platform_get_resource() may return NULL, add proper
check to avoid potential NULL dereferencing.

This is detected by Coccinelle semantic patch.

@@
expression pdev, res, n, t, e, e1, e2;
@@

res = platform_get_resource(pdev, t, n);
+ if (!res)
+   return -EINVAL;
... when != res == NULL
e = devm_ioremap(e1, res->start, e2);

Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
---
 drivers/gpio/gpio-ath79.c | 2 ++
 1 file changed, 2 insertions(+)




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

Comments

Alban Jan. 5, 2018, 2:06 p.m. | #1
On Thu, 4 Jan 2018 06:31:23 +0000
Wei Yongjun <weiyongjun1@huawei.com> wrote:

> platform_get_resource() may return NULL, add proper
> check to avoid potential NULL dereferencing.
> 
> This is detected by Coccinelle semantic patch.
> 
> @@
> expression pdev, res, n, t, e, e1, e2;
> @@
> 
> res = platform_get_resource(pdev, t, n);
> + if (!res)
> +   return -EINVAL;
> ... when != res == NULL
> e = devm_ioremap(e1, res->start, e2);
> 
> Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
> ---
>  drivers/gpio/gpio-ath79.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/gpio/gpio-ath79.c b/drivers/gpio/gpio-ath79.c
> index a757171..0bebcba 100644
> --- a/drivers/gpio/gpio-ath79.c
> +++ b/drivers/gpio/gpio-ath79.c
> @@ -258,6 +258,8 @@ static int ath79_gpio_probe(struct platform_device *pdev)
>  	}
>  
>  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	if (!res)
> +		return -EINVAL;
>  	ctrl->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
>  	if (!ctrl->base)
>  		return -ENOMEM;

This fix is needed, however this patch is not against the current
version. The current version use res in a call to devm_ioremap_nocache()
and not devm_ioremap().

Alban

Patch

diff --git a/drivers/gpio/gpio-ath79.c b/drivers/gpio/gpio-ath79.c
index a757171..0bebcba 100644
--- a/drivers/gpio/gpio-ath79.c
+++ b/drivers/gpio/gpio-ath79.c
@@ -258,6 +258,8 @@  static int ath79_gpio_probe(struct platform_device *pdev)
 	}
 
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -EINVAL;
 	ctrl->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
 	if (!ctrl->base)
 		return -ENOMEM;