Skip to contentSkip to conversation

PCI: dwc: pci-dra7xx: Fix potential NULL dereference in dra7xx_pcie_probe()

PATCH In review Wei Yongjun 4 participants 7 messages Apr 27, 2020 – May 5, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: dwc: pci-dra7xx: Fix potential NULL dereference in dra7xx_pcie_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);

---
 drivers/pci/controller/dwc/pci-dra7xx.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pci/controller/dwc/pci-dra7xx.c +3 -0
index 3b0e58f2de58..7a3d12f7e7d9 100644
@@ -878,6 +878,9 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 	}
 
 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
+	if (!res)
+		return -EINVAL;
+
 	base = devm_ioremap(dev, res->start, resource_size(res));
 	if (!base)
 		return -ENOMEM;
#1 lore raw

Hi,

Quoted 26 lines · Wei Yongjun

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

---
 drivers/pci/controller/dwc/pci-dra7xx.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pci/controller/dwc/pci-dra7xx.c +0 -0
index 3b0e58f2de58..7a3d12f7e7d9 100644

drivers/pci/controller/dwc/pci-dra7xx.c static int __init dra7xx_pcie_probe(struct platform_device *pdev) · line 884

Quoted 7 lines
drivers/pci/controller/dwc/pci-dra7xx.c +3 -0
@@ -878,6 +878,9 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 	}
 
 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
+	if (!res)
+		return -EINVAL;
+
 	base = devm_ioremap(dev, res->start, resource_size(res));

I don't see why this should be devm_ioremap(). It should also have been devm_ioremap_resource() which does the NULL check.

Alternately, how about using devm_platform_ioremap_resource_byname()?

Quoted 7 lines
drivers/pci/controller/dwc/pci-dra7xx.c +0 -0
 	if (!base)
 		return -ENOMEM;
#2 lore raw
Quoted 24 lines · Vignesh Raghavendra

Hi,

Quoted 21 lines · Wei Yongjun

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

---
 drivers/pci/controller/dwc/pci-dra7xx.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 8 lines

drivers/pci/controller/dwc/pci-dra7xx.c static int __init dra7xx_pcie_probe(struct platform_device *pdev) · line 880

Quoted 8 lines
drivers/pci/controller/dwc/pci-dra7xx.c +0 -0
index 3b0e58f2de58..7a3d12f7e7d9 100644
@@ -878,6 +878,9 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 	}
 
 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
                                           ^^^^
Quoted 4 lines

drivers/pci/controller/dwc/pci-dra7xx.c static int __init dra7xx_pcie_probe(struct platform_device *pdev) · line 884

Quoted 4 lines
drivers/pci/controller/dwc/pci-dra7xx.c +3 -0
+	if (!res)
+		return -EINVAL;
+
 	base = devm_ioremap(dev, res->start, resource_size(res));
                            ^^^
Quoted 3 lines

I don't see why this should be devm_ioremap(). It should also have been devm_ioremap_resource() which does the NULL check.

It's different device pointers.

regards,
dan carpenter

#3 lore raw
Quoted 41 lines · Dan Carpenter
Quoted 24 lines · Vignesh Raghavendra

Hi,

Quoted 21 lines · Wei Yongjun

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

---
 drivers/pci/controller/dwc/pci-dra7xx.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 8 lines

drivers/pci/controller/dwc/pci-dra7xx.c static int __init dra7xx_pcie_probe(struct platform_device *pdev) · line 880

Quoted 8 lines
drivers/pci/controller/dwc/pci-dra7xx.c +0 -0
index 3b0e58f2de58..7a3d12f7e7d9 100644
@@ -878,6 +878,9 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 	}
 
 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
                                           ^^^^
Quoted 4 lines

drivers/pci/controller/dwc/pci-dra7xx.c static int __init dra7xx_pcie_probe(struct platform_device *pdev) · line 884

Quoted 4 lines
drivers/pci/controller/dwc/pci-dra7xx.c +3 -0
+	if (!res)
+		return -EINVAL;
+
 	base = devm_ioremap(dev, res->start, resource_size(res));
                            ^^^
Quoted 1 line
Quoted 5 lines
Quoted 2 lines

I don't see why this should be devm_ioremap(). It should also have been devm_ioremap_resource() which does the NULL check.

It's different device pointers.

Sorry, I don't understand this comment... Currently we have:

static int __init dra7xx_pcie_probe(struct platform_device *pdev)

{
        struct device *dev = &pdev->dev;
	...
	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
        base = devm_ioremap(dev, res->start, resource_size(res));
        if (!base)
                return -ENOMEM;

Instead of the proposed patch, what I am asking is:

	base = devm_platform_ioremap_resource_byname(pdev, "ti_conf");
	if (IS_ERR(base))
		return PTR_ERR(base);
Quoted 3 lines

regards,
dan carpenter

#4 lore raw

Oh. Crap. I'm sorry. I misread the code.

regards,
dan carpenter

#5 lore raw

PATCH v2 1/1 PCI: dwc: pci-dra7xx: use devm_platform_ioremap_resource_byname()

In reply to Wei Yongjun

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. Fix it to use devm_platform_ioremap_resource_byname() instead of calling platform_get_resource_byname() and devm_ioremap().

---
v1 -> v2: use devm_platform_ioremap_resource_byname, suggest by Vignesh

---
 drivers/pci/controller/dwc/pci-dra7xx.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/pci/controller/dwc/pci-dra7xx.c +3 -5
index 3b0e58f2de58..6184ebc9392d 100644
@@ -840,7 +840,6 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 	struct phy **phy;
 	struct device_link **link;
 	void __iomem *base;
-	struct resource *res;
 	struct dw_pcie *pci;
 	struct dra7xx_pcie *dra7xx;
 	struct device *dev = &pdev->dev;
@@ -877,10 +876,9 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 		return irq;
 	}
 
-	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
-	base = devm_ioremap(dev, res->start, resource_size(res));
-	if (!base)
-		return -ENOMEM;
+	base = devm_platform_ioremap_resource_byname(pdev, "ti_conf");
+	if (IS_ERR(base))
+		return PTR_ERR(base);
 
 	phy_count = of_property_count_strings(np, "phy-names");
 	if (phy_count < 0) {
#6 lore raw

Re: [PATCH -next v2] PCI: dwc: pci-dra7xx: use devm_platform_ioremap_resource_byname()

Quoted 11 lines · Wei Yongjun

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. Fix it to use devm_platform_ioremap_resource_byname() instead of calling platform_get_resource_byname() and devm_ioremap().

---
v1 -> v2: use devm_platform_ioremap_resource_byname, suggest by Vignesh

---
 drivers/pci/controller/dwc/pci-dra7xx.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

Applied to pci/dwc, thanks.

Lorenzo

Quoted 29 lines
drivers/pci/controller/dwc/pci-dra7xx.c +3 -5
index 3b0e58f2de58..6184ebc9392d 100644
@@ -840,7 +840,6 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 	struct phy **phy;
 	struct device_link **link;
 	void __iomem *base;
-	struct resource *res;
 	struct dw_pcie *pci;
 	struct dra7xx_pcie *dra7xx;
 	struct device *dev = &pdev->dev;
@@ -877,10 +876,9 @@ static int __init dra7xx_pcie_probe(struct platform_device *pdev)
 		return irq;
 	}
 
-	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
-	base = devm_ioremap(dev, res->start, resource_size(res));
-	if (!base)
-		return -ENOMEM;
+	base = devm_platform_ioremap_resource_byname(pdev, "ti_conf");
+	if (IS_ERR(base))
+		return PTR_ERR(base);
 
 	phy_count = of_property_count_strings(np, "phy-names");
 	if (phy_count < 0) {