Skip to contentSkip to conversation

of: Deep-copy names of platform devices

PATCH In review Stepan Moskovchenko 2 participants 3 messages Aug 12, 2014 – Aug 13, 2014

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

PATCH 1/1 of: Deep-copy names of platform devices

When we parse the device tree and allocate platform devices, the 'name' of the newly-created platform_device is set to point to the 'name' field of the 'struct device' embedded within the platform_device. This is dangerous, because the name of the 'struct device' is dynamically allocated. Drivers may call dev_set_name() on the device, which will free and reallocate the name of the device, leaving the 'name' of the platform_device pointing to the now-freed memory.

Furthermore, if the dev_set_name() call is made from a driver's probe() function and a subsequent request results in probe deferral, the dangling 'name' reference may lead to the device being re-probed using the wrong driver.

To mitigate these scenarios, we use kstrdup to perform a deep copy of the device name when assigning the name of the platform_device, so that the platform_device name is unaffected by any calls to dev_set_name() that might made by drivers to rename the embedded 'struct device'.

---
I suppose creating a 'pdev_set_name' API may seem like another possibility, but I feel that dev.name and pdev.name have two different meanings. One is used for device/driver binding purposes, whereas the other serves a more general identification purpose, and is used for things like sysfs. Drivers might want to change dev.name while leaving the pdev.name alone. I guess yet another possibility would be to prohibit calling dev_set_name() on devices created from device tree, but a driver does not necessarily know how a given platform_device was allocated.

 drivers/of/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/device.c +1 -1
index f685e55..fe5f025 100644
@@ -54,7 +54,7 @@ int of_device_add(struct platform_device *ofdev)
 
 	/* name and id have to be set so that the platform bus doesn't get
 	 * confused on matching */
-	ofdev->name = dev_name(&ofdev->dev);
+	ofdev->name = kstrdup(dev_name(&ofdev->dev), GFP_KERNEL);
 	ofdev->id = -1;
 
 	/* device_add will assume that this device is on the same node as

--
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation

#1 lore raw
Quoted 42 lines · Stepan Moskovchenko

When we parse the device tree and allocate platform devices, the 'name' of the newly-created platform_device is set to point to the 'name' field of the 'struct device' embedded within the platform_device. This is dangerous, because the name of the 'struct device' is dynamically allocated. Drivers may call dev_set_name() on the device, which will free and reallocate the name of the device, leaving the 'name' of the platform_device pointing to the now-freed memory.

Furthermore, if the dev_set_name() call is made from a driver's probe() function and a subsequent request results in probe deferral, the dangling 'name' reference may lead to the device being re-probed using the wrong driver.

To mitigate these scenarios, we use kstrdup to perform a deep copy of the device name when assigning the name of the platform_device, so that the platform_device name is unaffected by any calls to dev_set_name() that might made by drivers to rename the embedded 'struct device'.

---
I suppose creating a 'pdev_set_name' API may seem like another possibility, but I feel that dev.name and pdev.name have two different meanings. One is used for device/driver binding purposes, whereas the other serves a more general identification purpose, and is used for things like sysfs. Drivers might want to change dev.name while leaving the pdev.name alone. I guess yet another possibility would be to prohibit calling dev_set_name() on devices created from device tree, but a driver does not necessarily know how a given platform_device was allocated.

drivers/of/device.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/device.c +0 -0
index f685e55..fe5f025 100644

drivers/of/device.c int of_device_add(struct platform_device *ofdev) · line 60

Quoted 8 lines
drivers/of/device.c +1 -1
@@ -54,7 +54,7 @@ int of_device_add(struct platform_device *ofdev)
 
 	/* name and id have to be set so that the platform bus doesn't get
 	 * confused on matching */
-	ofdev->name = dev_name(&ofdev->dev);
+	ofdev->name = kstrdup(dev_name(&ofdev->dev), GFP_KERNEL);
 	ofdev->id = -1;
 
 	/* device_add will assume that this device is on the same node as

Don’t we need to free this is of_device_unregister() now?

- k

Quoted 8 lines

--
The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation

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

Signature · 2 lines
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
#2 lore raw
Quoted 52 lines · Kumar Gala
Quoted 49 lines · Stepan Moskovchenko

When we parse the device tree and allocate platform devices, the 'name' of the newly-created platform_device is set to point to the 'name' field of the 'struct device' embedded within the platform_device. This is dangerous, because the name of the 'struct device' is dynamically allocated. Drivers may call dev_set_name() on the device, which will free and reallocate the name of the device, leaving the 'name' of the platform_device pointing to the now-freed memory.

Furthermore, if the dev_set_name() call is made from a driver's probe() function and a subsequent request results in probe deferral, the dangling 'name' reference may lead to the device being re-probed using the wrong driver.

To mitigate these scenarios, we use kstrdup to perform a deep copy of the device name when assigning the name of the platform_device, so that the platform_device name is unaffected by any calls to dev_set_name() that might made by drivers to rename the embedded 'struct device'.

---
I suppose creating a 'pdev_set_name' API may seem like another possibility, but I feel that dev.name and pdev.name have two different meanings. One is used for device/driver binding purposes, whereas the other serves a more general identification purpose, and is used for things like sysfs. Drivers might want to change dev.name while leaving the pdev.name alone. I guess yet another possibility would be to prohibit calling dev_set_name() on devices created from device tree, but a driver does not necessarily know how a given platform_device was allocated.

drivers/of/device.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/device.c +1 -1
index f685e55..fe5f025 100644
@@ -54,7 +54,7 @@ int of_device_add(struct platform_device *ofdev)
 
 	/* name and id have to be set so that the platform bus doesn't get
 	 * confused on matching */
-	ofdev->name = dev_name(&ofdev->dev);
+	ofdev->name = kstrdup(dev_name(&ofdev->dev), GFP_KERNEL);
 	ofdev->id = -1;
 
Quoted 6 lines

drivers/of/device.c int of_device_add(struct platform_device *ofdev) · line 60

Quoted 1 line
drivers/of/device.c +0 -0
 	/* device_add will assume that this device is on the same node as

Don’t we need to free this is of_device_unregister() now?

- k

Argh. I was confused by the asymmetric naming of the APIs, but after digging through the callers outside from drivers/of/, it looks like things do eventually filter down to of_device_add() in all the cases I could find. So yes, this does need to be fixed. Expect v2 soon.

Thanks
Steve

Signature · 2 lines
 The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
 hosted by The Linux Foundation