2020-07-14 02:34:45

by Xu Wang

[permalink] [raw]
Subject: [PATCH] net: xilinx: fix potential NULL dereference in temac_probe()

platform_get_resource() may return NULL, add proper
check to avoid potential NULL dereferencing.

Signed-off-by: Xu Wang <[email protected]>
---
drivers/net/ethernet/xilinx/ll_temac_main.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/net/ethernet/xilinx/ll_temac_main.c b/drivers/net/ethernet/xilinx/ll_temac_main.c
index 929244064abd..85a767fa2ecf 100644
--- a/drivers/net/ethernet/xilinx/ll_temac_main.c
+++ b/drivers/net/ethernet/xilinx/ll_temac_main.c
@@ -1408,6 +1408,8 @@ static int temac_probe(struct platform_device *pdev)

/* map device registers */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res)
+ return -EINVAL;
lp->regs = devm_ioremap(&pdev->dev, res->start,
resource_size(res));
if (!lp->regs) {
@@ -1503,6 +1505,8 @@ static int temac_probe(struct platform_device *pdev)
} else if (pdata) {
/* 2nd memory resource specifies DMA registers */
res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+ if (!res)
+ return -EINVAL;
lp->sdma_regs = devm_ioremap(&pdev->dev, res->start,
resource_size(res));
if (!lp->sdma_regs) {
--
2.17.1


2020-07-14 02:58:36

by Florian Fainelli

[permalink] [raw]
Subject: Re: [PATCH] net: xilinx: fix potential NULL dereference in temac_probe()



On 7/13/2020 7:23 PM, Xu Wang wrote:
> platform_get_resource() may return NULL, add proper
> check to avoid potential NULL dereferencing.
>
> Signed-off-by: Xu Wang <[email protected]>
> ---

If you use devm_ioremap_resource() you can remove the !res check
entirely which would be equally acceptable as a fix.

> drivers/net/ethernet/xilinx/ll_temac_main.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/net/ethernet/xilinx/ll_temac_main.c b/drivers/net/ethernet/xilinx/ll_temac_main.c
> index 929244064abd..85a767fa2ecf 100644
> --- a/drivers/net/ethernet/xilinx/ll_temac_main.c
> +++ b/drivers/net/ethernet/xilinx/ll_temac_main.c
> @@ -1408,6 +1408,8 @@ static int temac_probe(struct platform_device *pdev)
>
> /* map device registers */
> res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + if (!res)
> + return -EINVAL;
> lp->regs = devm_ioremap(&pdev->dev, res->start,
> resource_size(res));
> if (!lp->regs) {
> @@ -1503,6 +1505,8 @@ static int temac_probe(struct platform_device *pdev)
> } else if (pdata) {
> /* 2nd memory resource specifies DMA registers */
> res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
> + if (!res)
> + return -EINVAL;
> lp->sdma_regs = devm_ioremap(&pdev->dev, res->start,
> resource_size(res));
> if (!lp->sdma_regs) {
>

--
Florian

2020-07-14 09:46:40

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH] net: xilinx: fix potential NULL dereference in temac_probe()

> If you use devm_ioremap_resource() you can remove the !res check
> entirely which would be equally acceptable as a fix.

Would you like to use the wrapper function “devm_platform_get_and_ioremap_resource” then?
https://elixir.bootlin.com/linux/v5.8-rc4/source/drivers/base/platform.c#L66
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/platform.c?id=0dc589da873b58b70f4caf4b070fb0cf70fdd1dc#n66

Regards,
Markus