Skip to contentSkip to conversation

Fix null pointer deference when calling of_platform_depopulate

PATCH Superseded Ricardo Ribalda Delgado 5 participants 16 messages Apr 22, 2015 – May 26, 2015

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix null pointer deference when calling of_platform_depopulate

of_platform_depopulate can lead to a kernel error when calling release_resource()

of_platform_depopulate()

      of_platform_device_destroy()
        platform_device_unregister(platform_device *pdev)
	  platform_device_del(platform_device *pdev)
	    for (i = 0; i < pdev->num_resources; i++)
	    	      release_resource()

The reason is that it is trying to release a resource that was not allocated via insert_resource

of_platform_populate()
...
of_device_alloc()
pdev = platform_device_alloc()
# set pdev->resource, similar to platform_device_add_resources() of_device_add(platform_device *pdev)
# similar to platform_device_add(), but note there's no # insert_resource() in this path device_add(&pdev->dev)

This set of patches modifies release_resource to check for resource->parent==NULL and throw a warning if there is an error.

base/platform has been fixed for an hypothetical condition where parent is set but the platform is neither MEM or IO.

Then platform_device_alloc has been modified so it supports of and amba devices.

Finally of_device_add has been modified to use platform_device_add().

v1: https://lkml.org/lkml/2015/4/20/435

v2: https://lkml.org/lkml/2015/4/21/99

    https://lkml.org/lkml/2015/4/21/100

Ricardo Ribalda Delgado (4):
kernel/resource: Invalid memory access in __release_resource base/platform: Only insert MEM and IO resources base/platform: Continue on insert_resource() error of/platform: Use platform_device interface

 drivers/base/platform.c | 20 ++++++++++++--------
 drivers/of/platform.c   |  3 ++-
 kernel/resource.c       |  3 +++
 3 files changed, 17 insertions(+), 9 deletions(-)
Signature · 6 lines
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH v3 1/4 kernel/resource: Invalid memory access in __release_resource

When a resource is initialized via of_platform_populate. resource->parent is initialized to NULL via kzalloc. (of_platform_populate->of_device_alloc->of_address_to_resource)

If of_platform_depopulate is called later, resource->parent is accessed (Offset 0x30 of address 0), causing a kernel error.

This patch evaluates resouce->parent before accessing it. If it is not initialized, -EACCESS is returned.

Also a WARN is thrown, so the developer can have a hint about what needs to be fixed.

Fixes:

BUG: unable to handle kernel NULL pointer deference at 0000000000000030
IP: release_resource+0x26/0x90
---
 kernel/resource.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/resource.c +3 -0
index 90552aa..b7b270f 100644
@@ -237,6 +237,9 @@ static int __release_resource(struct resource *old)
 {
 	struct resource *tmp, **p;
 
+	if (WARN_ON(!old->parent))
+		return -EINVAL;
+
 	p = &old->parent->child;
 	for (;;) {
 		tmp = *p;
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH v3 1/4] kernel/resource: Invalid memory access in __release_resource

Quoted 26 lines · Ricardo Ribalda Delgado

When a resource is initialized via of_platform_populate. resource->parent is initialized to NULL via kzalloc. (of_platform_populate->of_device_alloc->of_address_to_resource)

If of_platform_depopulate is called later, resource->parent is accessed (Offset 0x30 of address 0), causing a kernel error.

This patch evaluates resouce->parent before accessing it. If it is not initialized, -EACCESS is returned.

Also a WARN is thrown, so the developer can have a hint about what needs to be fixed.

Fixes:

BUG: unable to handle kernel NULL pointer deference at 0000000000000030
IP: release_resource+0x26/0x90
---
 kernel/resource.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/resource.c +0 -0
index 90552aa..b7b270f 100644

kernel/resource.c static int __release_resource(struct resource *old) · line 241

Quoted 5 lines
kernel/resource.c +2 -0
@@ -237,6 +237,9 @@ static int __release_resource(struct resource *old)
 {
 	struct resource *tmp, **p;
 
+	if (WARN_ON(!old->parent))
+		return -EINVAL;

I'm not really a fan of this. The NULL pointer oops is a very good clue all by itself, and it doesn't require any extra code here.

Quoted 6 lines
kernel/resource.c +0 -0
 	p = &old->parent->child;
 	for (;;) {
 		tmp = *p;
Signature · 1 line
2.1.4
#3 lore raw

Re: [PATCH v3 1/4] kernel/resource: Invalid memory access in __release_resource

Hi Bjorn

Quoted 3 lines · Bjorn Helgaas

I'm not really a fan of this. The NULL pointer oops is a very good clue all by itself, and it doesn't require any extra code here.

It is a pointer to 0x30.

For some reason my platform can handle a couple of oops, but if I get a fair amount of them in a short period of time, the whole thing crashes and debugging gets complicated. (no access to dmesg or remote debugging).

Therefore this particular bug gave me a bad time. Now that we have located the root of the problem and solved the problem from the root, this particular error will never be hit. I just wanted to save somebody else's time with this patch.

All that said, if the other patches are applied, my platform works completely fine. So it is completely your call to accept this or not :)

I will resend the patchset without this patch

Thanks!

Signature · 5 lines
Ricardo Ribalda
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

PATCH v3 2/4 base/platform: Only insert MEM and IO resources

In reply to Ricardo Ribalda Delgado

platform_device_del only checks the type of the resource in order to call release_resource.

On the other hand, platform_device_add calls insert_resource for any resource that has a parent.

Make both code branches balanced.

---
 drivers/base/platform.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/base/platform.c +7 -3
index ebf034b..6028681 100644
@@ -341,19 +341,23 @@ int platform_device_add(struct platform_device *pdev)
 
 	for (i = 0; i < pdev->num_resources; i++) {
 		struct resource *p, *r = &pdev->resource[i];
+		unsigned long type = resource_type(r);
 
 		if (r->name == NULL)
 			r->name = dev_name(&pdev->dev);
 
+		if (!(type == IORESOURCE_MEM || type == IORESOURCE_IO))
+			continue;
+
 		p = r->parent;
 		if (!p) {
-			if (resource_type(r) == IORESOURCE_MEM)
+			if (type == IORESOURCE_MEM)
 				p = &iomem_resource;
-			else if (resource_type(r) == IORESOURCE_IO)
+			else if (type == IORESOURCE_IO)
 				p = &ioport_resource;
 		}
 
-		if (p && insert_resource(p, r)) {
+		if (insert_resource(p, r)) {
 			dev_err(&pdev->dev, "failed to claim resource %d\n", i);
 			ret = -EBUSY;
 			goto failed;
Signature · 1 line
2.1.4
#5 lore raw

PATCH v3 3/4 base/platform: Continue on insert_resource() error

In reply to Ricardo Ribalda Delgado

insert_resource() can fail when the resource added overlaps (partially or fully) with another.

Device tree and AMBA devices may contain resources that overlap, so they could not call platform_device_add (Revert "of: use platform_device_add" 02bbde7849e68e193cefaa1885fe0df0f03c9fcd )

On the other hand, device trees are released using platform_device_unregister(). This function calls platform_device_del(), which calls release_resource(), that crashes when the resource has not been added with with insert_resource. This was not an issue when the device tree could not be modified online, but this is not the case anymore.

This patch let the flow continue when there is an insert error, after notifying the user with a dev_err(). r->parent is set to NULL, so the device_del knows that the resource was not added, and therefore it should not be released.

---
 drivers/base/platform.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/base/platform.c +5 -5
index 6028681..8cce2a3 100644
@@ -359,8 +359,7 @@ int platform_device_add(struct platform_device *pdev)
 
 		if (insert_resource(p, r)) {
 			dev_err(&pdev->dev, "failed to claim resource %d\n", i);
-			ret = -EBUSY;
-			goto failed;
+			r->parent = NULL;
 		}
 	}
 
@@ -371,7 +370,6 @@ int platform_device_add(struct platform_device *pdev)
 	if (ret == 0)
 		return ret;
 
- failed:
 	if (pdev->id_auto) {
 		ida_simple_remove(&platform_devid_ida, pdev->id);
 		pdev->id = PLATFORM_DEVID_AUTO;
@@ -381,7 +379,8 @@ int platform_device_add(struct platform_device *pdev)
 		struct resource *r = &pdev->resource[i];
 		unsigned long type = resource_type(r);
 
-		if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
+		if ((type == IORESOURCE_MEM || type == IORESOURCE_IO) &&
+				r->parent)
 			release_resource(r);
 	}
 
@@ -414,7 +413,8 @@ void platform_device_del(struct platform_device *pdev)
 			struct resource *r = &pdev->resource[i];
 			unsigned long type = resource_type(r);
 
-			if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
+			if ((type == IORESOURCE_MEM || type == IORESOURCE_IO) &&
+					r->parent)
 				release_resource(r);
 		}
 	}
Signature · 1 line
2.1.4
#6 lore raw

Re: [PATCH v3 3/4] base/platform: Continue on insert_resource() error

Quoted 6 lines · Ricardo Ribalda Delgado

insert_resource() can fail when the resource added overlaps (partially or fully) with another.

Device tree and AMBA devices may contain resources that overlap, so they could not call platform_device_add (Revert "of: use platform_device_add" 02bbde7849e68e193cefaa1885fe0df0f03c9fcd )

Usual style for referencing a commit is "(see 02bbde7849e6 ('Revert "of:
use platform_device_add"'))".

Quoted 11 lines

On the other hand, device trees are released using platform_device_unregister(). This function calls platform_device_del(), which calls release_resource(), that crashes when the resource has not been added with with insert_resource. This was not an issue when the device tree could not be modified online, but this is not the case anymore.

This patch let the flow continue when there is an insert error, after notifying the user with a dev_err(). r->parent is set to NULL, so the device_del knows that the resource was not added, and therefore it should not be released.

I think you meant platform_device_del()? I don't think device_del() does anything with resources.

drivers/base/platform.c int platform_device_add(struct platform_device *pdev) · line 361

Quoted 13 lines
---
 drivers/base/platform.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/base/platform.c +0 -0
index 6028681..8cce2a3 100644
@@ -359,8 +359,7 @@ int platform_device_add(struct platform_device *pdev)
 
 		if (insert_resource(p, r)) {
 			dev_err(&pdev->dev, "failed to claim resource %d\n", i);

Sounds like we should expect to see this message more in the future, after you change of_platform_device_create_pdata() use platform_device_add(). You might want to use insert_resource_conflict() here so you can include information about *why* we failed to claim the resource. And it would be nice to use %pR for both resources.

drivers/base/platform.c int platform_device_add(struct platform_device *pdev) · line 374

Quoted 11 lines
drivers/base/platform.c +1 -3
-			ret = -EBUSY;
-			goto failed;
+			r->parent = NULL;
 		}
 	}
 
@@ -371,7 +370,6 @@ int platform_device_add(struct platform_device *pdev)
 	if (ret == 0)
 		return ret;
 
- failed:

Might be nice to keep a comment here as a clue that the rest of the function is the failure path.

It's a minor style thing, but I would also remove the "err_out" label and return "ret" directly above rather than branching to "err_out".

Quoted 26 lines
drivers/base/platform.c +4 -2
 	if (pdev->id_auto) {
 		ida_simple_remove(&platform_devid_ida, pdev->id);
 		pdev->id = PLATFORM_DEVID_AUTO;
@@ -381,7 +379,8 @@ int platform_device_add(struct platform_device *pdev)
 		struct resource *r = &pdev->resource[i];
 		unsigned long type = resource_type(r);
 
-		if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
+		if ((type == IORESOURCE_MEM || type == IORESOURCE_IO) &&
+				r->parent)
 			release_resource(r);
 	}
 
@@ -414,7 +413,8 @@ void platform_device_del(struct platform_device *pdev)
 			struct resource *r = &pdev->resource[i];
 			unsigned long type = resource_type(r);
 
-			if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
+			if ((type == IORESOURCE_MEM || type == IORESOURCE_IO) &&
+					r->parent)
 				release_resource(r);
 		}
 	}
Signature · 1 line
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

Re: [PATCH v3 3/4] base/platform: Continue on insert_resource() error

Hi Bjorn:

Quoted 2 lines · Bjorn Helgaas

Usual style for referencing a commit is "(see 02bbde7849e6 ('Revert "of:
use platform_device_add"'))".

Do you make that reference manually or there is a magic git command for printing it in that style?

Quoted 8 lines

Sounds like we should expect to see this message more in the future, after you change of_platform_device_create_pdata() use platform_device_add(). You might want to use insert_resource_conflict() here so you can include information about *why* we failed to claim the resource. And it would be nice to use %pR for both resources.

.....

Quoted 5 lines
Quoted 2 lines

- failed:

Might be nice to keep a comment here as a clue that the rest of the function is the failure path.

Will prepare a patch with the changes and resend.

Thanks!

Signature · 5 lines
Ricardo Ribalda
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw

Re: [PATCH v3 3/4] base/platform: Continue on insert_resource() error

Quoted 9 lines · Ricardo Ribalda Delgado

Hi Bjorn:

Quoted 2 lines · Bjorn Helgaas

Usual style for referencing a commit is "(see 02bbde7849e6 ('Revert "of:
use platform_device_add"'))".

Do you make that reference manually or there is a magic git command for printing it in that style?

I used to do it mostly by hand, but thanks to your prompting, I fiddled around and came up with this alias:

gsr is aliased to `git show -s --abbrev-commit --abbrev=12 --pretty=format:"%h (\"%s\")%n"'

Bjorn

#9 lore raw

Re: [PATCH v3 3/4] base/platform: Continue on insert_resource() error

Quoted 8 lines · Bjorn Helgaas
Quoted 7 lines · Ricardo Ribalda Delgado

Hi Bjorn:

Quoted 2 lines · Bjorn Helgaas

Usual style for referencing a commit is "(see 02bbde7849e6 ('Revert "of:
use platform_device_add"'))".

Quoted 7 lines
Quoted 2 lines

Do you make that reference manually or there is a magic git command for printing it in that style?

I used to do it mostly by hand, but thanks to your prompting, I fiddled around and came up with this alias:

gsr is aliased to `git show -s --abbrev-commit --abbrev=12 --pretty=format:"%h (\"%s\")%n"'

There's also git log --format=fixes, though that includes the "Fixes: " prefix. That works with git show as well.

Thierry

#10 lore raw

PATCH v3 4/4 of/platform: Use platform_device interface

In reply to Ricardo Ribalda Delgado

of_platform_device_create_pdata() was using of_device_add() to create the devices, but of_platform_device_destroy was using of_platform_device_destroy().

of_device_add(), do not call insert_resource(), which initializes the parent field of the resource structure, needed by release_resource(), called by of_platform_device_destroy().

This leads to a NULL pointer deference.

This patch, replaces of_device_add() with platform_device_data().

---
 drivers/of/platform.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/of/platform.c +2 -1
index a01f57c..f011f57 100644
@@ -183,8 +183,9 @@ static struct platform_device *of_platform_device_create_pdata(
 	dev->dev.bus = &platform_bus_type;
 	dev->dev.platform_data = platform_data;
 	of_dma_configure(&dev->dev, dev->dev.of_node);
+	dev->name = dev_name(&dev->dev);
 
-	if (of_device_add(dev) != 0) {
+	if (platform_device_add(dev) != 0) {
 		of_dma_deconfigure(&dev->dev);
 		platform_device_put(dev);
 		goto err_clear_flag;
Signature · 1 line
2.1.4
#11 lore raw

Re: [PATCH v3 4/4] of/platform: Use platform_device interface

Quoted 11 lines · Ricardo Ribalda Delgado

of_platform_device_create_pdata() was using of_device_add() to create the devices, but of_platform_device_destroy was using of_platform_device_destroy().

of_device_add(), do not call insert_resource(), which initializes the parent field of the resource structure, needed by release_resource(), called by of_platform_device_destroy().

This leads to a NULL pointer deference.

This patch, replaces of_device_add() with platform_device_data().

This doesn't match the change.

Quoted 11 lines
---
 drivers/of/platform.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/of/platform.c +0 -0
index a01f57c..f011f57 100644

drivers/of/platform.c static struct platform_device *of_platform_device_create_pdata( · line 188

Quoted 7 lines
drivers/of/platform.c +2 -1
@@ -183,8 +183,9 @@ static struct platform_device *of_platform_device_create_pdata(
        dev->dev.bus = &platform_bus_type;
        dev->dev.platform_data = platform_data;
        of_dma_configure(&dev->dev, dev->dev.of_node);
+       dev->name = dev_name(&dev->dev);
 
-       if (of_device_add(dev) != 0) {
+       if (platform_device_add(dev) != 0) {

Can't we now remove of_device_add?

Quoted 6 lines
drivers/of/platform.c +0 -1
                of_dma_deconfigure(&dev->dev);
                platform_device_put(dev);
                goto err_clear_flag;
--

2.1.4

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw

Re: [PATCH v3 4/4] of/platform: Use platform_device interface

Hello Rob

Quoted 3 lines · Rob Herring
Quoted 1 line

This patch, replaces of_device_add() with platform_device_data().

This doesn't match the change.

Thanks for catching it up. Will fix it and resend

Quoted 4 lines

drivers/of/platform.c static struct platform_device *of_platform_device_create_pdata( · line 188

Quoted 2 lines
drivers/of/platform.c +1 -1
-       if (of_device_add(dev) != 0) {
+       if (platform_device_add(dev) != 0) {

Can't we now remove of_device_add?

Now it is only used by ibmebus

ricardo@neopili:~/linux-upstream$ git grep of_device_add
arch/powerpc/kernel/ibmebus.c:  ret = of_device_add(dev);
drivers/of/device.c:int of_device_add(struct platform_device *ofdev)
drivers/of/device.c:    return of_device_add(pdev);
include/linux/of_device.h:extern int of_device_add(struct
platform_device *pdev);

I think it can also use platform_device_add. I will prepare a patch to finally remove of_device_add()

Thanks!

Signature · 1 line
Ricardo Ribalda
#13 lore raw

Re: [PATCH v3 4/4] of/platform: Use platform_device interface

Hello Rob

Quoted 3 lines · Ricardo Ribalda Delgado

I think it can also use platform_device_add. I will prepare a patch to finally remove of_device_add()

I will post right away an updated version of this patchset, and then I will prepare another one to remove of_device_add() completely. I already have two patches, but I have to figure out a proper way to test it.

Thanks!

Signature · 5 lines
Ricardo Ribalda
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#14 lore raw

Re: [PATCH v3.1 4/4] of/platform: Use platform_device interface

In reply to Ricardo Ribalda Delgado
Quoted 9 lines · Ricardo Ribalda Delgado

of_platform_device_create_pdata() was using of_device_add() to create the devices, but of_platform_device_destroy was using platform_device_unregister() to free them.

of_device_add(), do not call insert_resource(), which initializes the parent field of the resource structure, needed by release_resource(), called by of_platform_device_destroy(). This leads to a NULL pointer deference.

Quoted 8 lines

Instead of fixing the NULL pointer deference, what could hide other bugs, this patch, replaces of_device_add() with platform_device_data().

---

v3.1 Fix comments by Rob Herring, thanks!

3.1?

Please resend the whole series, this is a mess, I can't find where this goes at all...

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw

Re: [PATCH v3.1 4/4] of/platform: Use platform_device interface

Hello Greg

Sorry for the mess. I did not want to spam the mailing list too much.

Repacking and resending. Thanks!

Quoted 25 lines · Greg Kroah-Hartman
Quoted 9 lines · Ricardo Ribalda Delgado

of_platform_device_create_pdata() was using of_device_add() to create the devices, but of_platform_device_destroy was using platform_device_unregister() to free them.

of_device_add(), do not call insert_resource(), which initializes the parent field of the resource structure, needed by release_resource(), called by of_platform_device_destroy(). This leads to a NULL pointer deference.

Quoted 8 lines

Instead of fixing the NULL pointer deference, what could hide other bugs, this patch, replaces of_device_add() with platform_device_data().

---

v3.1 Fix comments by Rob Herring, thanks!

3.1?

Please resend the whole series, this is a mess, I can't find where this goes at all...

greg k-h

Signature · 5 lines
Ricardo Ribalda
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html