2020-12-04 08:32:56

by Zhang Changzhong

[permalink] [raw]
Subject: [PATCH] memory: jz4780_nemc: Fix potential NULL dereference in jz4780_nemc_probe()

platform_get_resource() may fail and return NULL, so we should
better check it's return value to avoid a NULL pointer dereference
a bit later in the code.

This is detected by Coccinelle semantic patch.

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

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

Fixes: 911a88829725 ("memory: jz4780-nemc: driver for the NEMC on JZ4780 SoCs")
Signed-off-by: Zhang Changzhong <[email protected]>
---
drivers/memory/jz4780-nemc.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/memory/jz4780-nemc.c b/drivers/memory/jz4780-nemc.c
index 3ec5cb0..465ea92 100644
--- a/drivers/memory/jz4780-nemc.c
+++ b/drivers/memory/jz4780-nemc.c
@@ -291,6 +291,8 @@ static int jz4780_nemc_probe(struct platform_device *pdev)
nemc->dev = dev;

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res)
+ return -EINVAL;

/*
* The driver currently only uses the registers up to offset
--
2.9.5


2020-12-04 10:14:26

by Paul Cercueil

[permalink] [raw]
Subject: Re: [PATCH] memory: jz4780_nemc: Fix potential NULL dereference in jz4780_nemc_probe()

Hi Zhang,

Le ven. 4 d?c. 2020 ? 16:31, Zhang Changzhong
<[email protected]> a ?crit :
> platform_get_resource() may fail and return NULL, so we should
> better check it's return value to avoid a NULL pointer dereference
> a bit later in the code.
>
> This is detected by Coccinelle semantic patch.
>
> @@
> expression pdev, res, n, t, e, e1, e2;
> @@
>
> res = \(platform_get_resource\|platform_get_resource_byname\)(pdev, t,
> n);
> + if (!res)
> + return -EINVAL;
> ... when != res == NULL
> e = devm_ioremap(e1, res->start, e2);
>
> Fixes: 911a88829725 ("memory: jz4780-nemc: driver for the NEMC on
> JZ4780 SoCs")
> Signed-off-by: Zhang Changzhong <[email protected]>

Acked-by: Paul Cercueil <[email protected]>

Cheers,
-Paul

> ---
> drivers/memory/jz4780-nemc.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/memory/jz4780-nemc.c
> b/drivers/memory/jz4780-nemc.c
> index 3ec5cb0..465ea92 100644
> --- a/drivers/memory/jz4780-nemc.c
> +++ b/drivers/memory/jz4780-nemc.c
> @@ -291,6 +291,8 @@ static int jz4780_nemc_probe(struct
> platform_device *pdev)
> nemc->dev = dev;
>
> res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + if (!res)
> + return -EINVAL;
>
> /*
> * The driver currently only uses the registers up to offset
> --
> 2.9.5
>


2020-12-05 15:52:40

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] memory: jz4780_nemc: Fix potential NULL dereference in jz4780_nemc_probe()

On Fri, 4 Dec 2020 16:31:57 +0800, Zhang Changzhong wrote:
> platform_get_resource() may fail and return NULL, so we should
> better check it's return value to avoid a NULL pointer dereference
> a bit later in the code.
>
> This is detected by Coccinelle semantic patch.
>
> @@
> expression pdev, res, n, t, e, e1, e2;
> @@
>
> [...]

Applied, thanks!

[1/1] memory: jz4780_nemc: Fix potential NULL dereference in jz4780_nemc_probe()
commit: 4bfa07300b9334b487ed4f3d4901c35ebb31b7ca

Best regards,
--
Krzysztof Kozlowski <[email protected]>