watchdog: core: assign parent with devm registration
PATCH 1/1 watchdog: core: assign parent with devm registration
If the user did not pass a parent in the struct watchdog_device then use the device used for devres as parent.
This is quite intuitive and can help avoiding having to assign parent explicitly in every driver using devres to add the watchdog_device.
---
Linus: hope you don't mind me flat out copying the description from
https://lore.kernel.org/all/[email protected]/
drivers/watchdog/watchdog_core.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/watchdog/watchdog_core.c +7 -0
@@ -440,6 +440,13 @@ int devm_watchdog_register_device(struct device *dev,if (!rcwdd)return -ENOMEM ; +/* +* We are passing the devres device here so if the user did not pass +* another parent, it's this one. +*/ +if (!wdd->parent) + wdd->parent = dev; + ret =watchdog_register_device (wdd);if (!ret) { *rcwdd = wdd;
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Modifying the caller-provided `struct watchdog_device` by implicitly setting `wdd->parent = dev`…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Modifying the caller-provided struct watchdog_device by implicitly setting wdd->parent = dev causes a dangling pointer and use-after-free vulnerability for drivers using statically or globally allocated watchdog devices. --
commit cdc03b5b413f4bd1a9d27c7f13f33dd6b209e2ba
Author: Rosen Penev <[email protected]>
watchdog: core: assign parent with devm registration
This commit modifies devm_watchdog_register_device() to automatically assign the devres device as the parent of the watchdog_device if one was not explicitly provided. This aims to reduce boilerplate in drivers using devres.
drivers/watchdog/watchdog_core.c int devm_watchdog_register_device(struct device *dev, · line 448
Quoted 13 lines
drivers/watchdog/watchdog_core.c +6
-0@@ -440,6 +440,13 @@ int devm_watchdog_register_device(struct device *dev,if (!rcwdd)return -ENOMEM ; +/* +* We are passing the devres device here so if the user did not pass +* another parent, it's this one. +*/ +if (!wdd->parent) + wdd->parent = dev;
[Severity: High]
If a driver passes a statically allocated struct watchdog_device (such as pic32-wdt or pic32-dmt) and does not explicitly initialize the parent field, the first probe will modify the global structure to point to the newly allocated struct device.
If the platform device is subsequently removed (for example, via a device tree overlay removal), the struct device is freed, but the global wdd->parent pointer remains dirty and points to the freed memory.
Upon a subsequent re-probe, the if (!wdd->parent) check evaluates to false, skipping the update.
Could this cause a use-after-free when the core passes the stale, freed pointer to watchdog_register_device() and eventually device_create_with_groups() during sysfs node creation?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Guenter Roeck
maintainerQuoted 19 lines · Rosen Penev
If the user did not pass a parent in the struct
watchdog_devicethen use the device used for devres as parent.This is quite intuitive and can help avoiding having to assign parent explicitly in every driver using devres to add the
watchdog_device.Signed-off-by Rosen Penev <[email protected]>---
Linus: hope you don't mind me flat out copying the description from
https://lore.kernel.org/all/[email protected]/drivers/watchdog/watchdog_core.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/watchdog/watchdog_core.c +0
-0
drivers/watchdog/watchdog_core.c int devm_watchdog_register_device(struct device *dev, · line 449
Quoted 10 lines
drivers/watchdog/watchdog_core.c +7
-0@@ -440,6 +440,13 @@ int devm_watchdog_register_device(struct device *dev,if (!rcwdd)return -ENOMEM ; +/* +* We are passing the devres device here so if the user did not pass +* another parent, it's this one. +*/ +if (!wdd->parent) + wdd->parent = dev; +
I understand that you consider it to be garbage, but in my opinion Sashiko has a point. If the watchdog device data structure is static, wdd->parent will be retained over multiple remove/reinsert instantiations of the driver, which in turn would result in subsequent UAF. That means I can not accept your patch.
Guenter
Quoted 4 lines · Guenter Roeck
I understand that you consider it to be garbage, but in my opinion Sashiko has a point. If the watchdog device data structure is static,
wdd->parentwill be retained over multiple remove/reinsert instantiations of the driver, which in turn would result in subsequent UAF. That means I can not accept your patch.
Hm! I wonder if that is a problem for my patch as well. We do have GPIO dongles that can be plugged in/out repeatedly....
Yours,
Linus Walleij