Skip to contentSkip to conversation

kernel/resource: Invalid memory access in __release_resource

PATCH Superseded Ricardo Ribalda Delgado 3 participants 8 messages Apr 21, 2015 – Jun 5, 2015 Device tree

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 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 · 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 v2 2/2 of/platform: Use platform_device interface

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
#2 lore raw

Re: [PATCH 2/2 v2] of/platform: Use platform_device interface

Quoted 7 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 is because some DTs have overlapping resources and doing this would break things. If you look at the git history, this was fixed and then reverted by Grant.

Rob

Quoted 25 lines

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 -2
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;
--
Quoted 2 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

#3 lore raw

Re: [PATCH 2/2 v2] of/platform: Use platform_device interface

Hello Rob

Quoted 6 lines · Rob Herring
Quoted 4 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().

Quoted 7 lines
Quoted 3 lines

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 is because some DTs have overlapping resources and doing this would break things. If you look at the git history, this was fixed and then reverted by Grant.

I cannot find that commit sorry, could you give me the hash or a link to the mailing list?

ricardo@pilix:~/linux$ git shortlog drivers/of/platform.c | grep -i Revert
      Revert "drivers: of: add initialization code for dma reserved memory"

To give a litte context to this patch, the issue started with this conversaion with Bjorn:
https://lkml.org/lkml/2015/4/20/435

What we have today is also wrong, it leads to a null pointer deference (and therefore a whole crash).

If we cannot use platform_device_add, then we cannot use platform_device_destroy :)

Shall I prepare a patch replacing platform_device_destroy()?

Thanks!

#4 lore raw

Re: [PATCH 2/2 v2] of/platform: Use platform_device interface

Quoted 17 lines · Ricardo Ribalda Delgado

Hello Rob

Quoted 6 lines · Rob Herring
Quoted 4 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().

Quoted 7 lines
Quoted 3 lines

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 is because some DTs have overlapping resources and doing this would break things. If you look at the git history, this was fixed and then reverted by Grant.

Quoted 5 lines

I cannot find that commit sorry, could you give me the hash or a link to the mailing list?

ricardo@pilix:~/linux$ git shortlog drivers/of/platform.c | grep -i Revert
      Revert "drivers: of: add initialization code for dma reserved memory"

commit 02bbde7849e68e193cefaa1885fe0df0f03c9fcd

Author: Grant Likely <[email protected]>
Date:   Sun Feb 17 20:03:27 2013 +0000

Revert "of: use platform_device_add"

This reverts commit aac73f34542bc7ae4317928d2eabfeb21d247323. That commit causes two kinds of breakage; it breaks registration of AMBA devices when one of the parent nodes already contains overlapping resource regions, and it breaks calls to request_region() by device drivers in certain conditions where there are overlapping memory regions. Both of these problems can probably be fixed, but it is better to back out the commit and get a proper fix designed before trying again.

Quoted 7 lines

To give a litte context to this patch, the issue started with this conversaion with Bjorn:
https://lkml.org/lkml/2015/4/20/435

Quoted 7 lines

What we have today is also wrong, it leads to a null pointer deference (and therefore a whole crash).

If we cannot use platform_device_add, then we cannot use platform_device_destroy :)

Shall I prepare a patch replacing platform_device_destroy()?

Perhaps we make inserting resource failure non-fatal so by default we can have resources inserted but not break the cases Grant mentioned. Ideally we want to not have new platforms with overlapping resources in the DT.

Rob

#5 lore raw

Re: [PATCH 2/2 v2] of/platform: Use platform_device interface

Quoted 54 lines · Rob Herring
Quoted 17 lines · Ricardo Ribalda Delgado

Hello Rob

Quoted 6 lines · Rob Herring
Quoted 4 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().

Quoted 7 lines
Quoted 3 lines

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 is because some DTs have overlapping resources and doing this would break things. If you look at the git history, this was fixed and then reverted by Grant.

Quoted 5 lines

I cannot find that commit sorry, could you give me the hash or a link to the mailing list?

ricardo@pilix:~/linux$ git shortlog drivers/of/platform.c | grep -i Revert
      Revert "drivers: of: add initialization code for dma reserved memory"

commit 02bbde7849e68e193cefaa1885fe0df0f03c9fcd

Author: Grant Likely <[email protected]>
Date:   Sun Feb 17 20:03:27 2013 +0000

Revert "of: use platform_device_add"

This reverts commit aac73f34542bc7ae4317928d2eabfeb21d247323. That commit causes two kinds of breakage; it breaks registration of AMBA devices when one of the parent nodes already contains overlapping resource regions, and it breaks calls to request_region() by device drivers in certain conditions where there are overlapping memory regions. Both of these problems can probably be fixed, but it is better to back out the commit and get a proper fix designed before trying again.

Quoted 13 lines

To give a litte context to this patch, the issue started with this conversaion with Bjorn:
https://lkml.org/lkml/2015/4/20/435

What we have today is also wrong, it leads to a null pointer deference (and therefore a whole crash).

If we cannot use platform_device_add, then we cannot use platform_device_destroy :)

Quoted 6 lines
Quoted 1 line

Shall I prepare a patch replacing platform_device_destroy()?

Perhaps we make inserting resource failure non-fatal so by default we can have resources inserted but not break the cases Grant mentioned. Ideally we want to not have new platforms with overlapping resources in the DT.

I think I tried that too and then ran in to a problem where drivers will fail if a different device 'owns' the resource.

For example, if device-a and device-b have overlapping regions, device-a gets registered first and claims the region. device-b tries to claim the region, fails, but is allowed to continue anyway. When driver-b tries to bind to device-b, and does a request_resource(), it will fail because device-a already owns it. I don't have a good solution for that other than to completely disable insert_resource() when using devicetree.

g.

#6 lore raw

Re: [PATCH 2/2 v2] of/platform: Use platform_device interface

Hello Grant

Quoted 10 lines · Grant Likely

I think I tried that too and then ran in to a problem where drivers will fail if a different device 'owns' the resource.

For example, if device-a and device-b have overlapping regions, device-a gets registered first and claims the region. device-b tries to claim the region, fails, but is allowed to continue anyway. When driver-b tries to bind to device-b, and does a request_resource(), it will fail because device-a already owns it. I don't have a good solution for that other than to completely disable insert_resource() when using devicetree.

If someone is following this conversation I have already replied in

[PATCH v5 2/4] base/platform: Continue on insert_resource() error

Message-ID: <CAPybu_3gYAZTHHYD7y2MdKFJBwDVyb5a9fwQqEMc+0xmKSTpKg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

Regards!

Quoted 2 lines

g.

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
#7 lore raw

Re: [PATCH 2/2 v2] of/platform: Use platform_device interface

Hello Grant and Rob

Could you take a look to these two patches that I have just sent.

kernel/resource: Add new flag IORESOURCE_SHARED of/platform: Mark all device tree resources as SHARED

I think it fixes Grant problem.

Regards!
--
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