driver core: platform: fix sw node use-after-free on device release
Subsystems (4)
Johan Hovold
maintainercover driver core: platform: fix sw node use-after-free on device release
Sashiko noted a new use-after-free issue when freeing platform device which has a software node as its primary fw node.
Turns out that can cause trouble also when drivers or subsystems remove software nodes themselves.
Johan
Johan Hovold (2):
driver core: platform: fix sw node use-after-free on device release software node: fix primary software node removal
drivers/base/platform.c | 3 ++- drivers/base/swnode.c | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-)
Signature · 1 line
2.55.0
Johan Hovold
maintainerPATCH 1/2 driver core: platform: fix sw node use-after-free on device release
When a device has a software node, device_remove_software_node() may drop the last reference and free it during platform_device_release(). And if the software node is the primary node then the device fwnode pointer is currently never cleared.
Use a temporary variable to track whether the primary node is a software node to avoid a use-after-free in is_software_node() and having fwnode_handle_put() call a function pointer via poisoned or reallocated memory when the primary node is a software node.
This issue was flagged by Sashiko when reviewing a related fix.
--- drivers/base/platform.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/base/platform.c +2 -1
@@ -598,13 +598,14 @@ static void platform_device_release(struct device *dev) { struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev); +bool primary_is_software_node =is_software_node (dev_fwnode (dev));device_remove_software_node (dev);/* * If the primary firmware node is a software node, its reference count * was already decreased by the call to device_remove_software_node(). */ -if (!is_software_node (dev_fwnode (dev))) +if (!primary_is_software_node)fwnode_handle_put (pa->pdev.dev.fwnode);kfree (pa->pdev.dev.platform_data);kfree (pa->pdev.mfd_cell);
Signature · 1 line
2.55.0
Johan Hovold
maintainerPATCH 2/2 software node: fix primary software node removal
In reply to Johan HovoldWhen removing a software node using device_remove_software_node(), the primary fwnode pointer is never cleared if the software node is the primary node.
Since commit 7d80509ff0da ("driver core: platform: unify release path") this can lead to a use-after-free when freeing a platform device if a driver or subsystem (e.g. mfd) has already removed and freed the node.
Clear the primary fwnode pointer when removing a software node that is the primary node to avoid this.
--- drivers/base/swnode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/base/swnode.c +5 -1
@@ -1110,7 +1110,11 @@ void device_remove_software_node(struct device *dev)if (device_is_registered (dev))software_node_notify_remove (dev); -set_secondary_fwnode (dev,NULL ); +if (is_software_node (dev_fwnode (dev))) +set_primary_fwnode (dev,NULL ); +else +set_secondary_fwnode (dev,NULL ); +swnode_put (swnode); }EXPORT_SYMBOL_GPL (device_remove_software_node);
Signature · 1 line
2.55.0
Quoted 10 lines · Johan Hovold
When a device has a software node,
device_remove_software_node()may drop the last reference and free it duringplatform_device_release(). And if the software node is the primary node then the device fwnode pointer is currently never cleared.Use a temporary variable to track whether the primary node is a software node to avoid a use-after-free in
is_software_node()and havingfwnode_handle_put()call a function pointer via poisoned or reallocated memory when the primary node is a software node.
Quoted 8 lines
This issue was flagged by Sashiko when reviewing a related fix.
Fixes 7d80509ff0da ("driver core: platform: unify release path")Cc [email protected] # 7.3Cc Bartosz Golaszewski <[email protected]>Signed-off-by Johan Hovold <[email protected]>---
Johan Hovold
maintainerRe: [PATCH 2/2] software node: fix primary software node removal
In reply to Johan HovoldQuoted 8 lines · Johan Hovold
When removing a software node using
device_remove_software_node(), the primary fwnode pointer is never cleared if the software node is the primary node.Since commit
7d80509ff0da("driver core: platform: unify release path") this can lead to a use-after-free when freeing a platform device if a driver or subsystem (e.g. mfd) has already removed and freed the node.
Quoted 7 lines
Clear the primary fwnode pointer when removing a software node that is the primary node to avoid this.
Fixes 7d80509ff0da ("driver core: platform: unify release path")Cc [email protected] # 7.3Cc Bartosz Golaszewski <[email protected]>Signed-off-by Johan Hovold <[email protected]>
Sashiko had some comments on this one, will revisit.
The first patch could go in meanwhile.
Johan
Re: (subset) [PATCH 0/2] driver core: platform: fix sw node use-after-free on device release
In reply to Johan HovoldQuoted 1 line · Johan Hovold
[PATCH 0/2] driver core: platform: fix sw node use-after-free on device release
Applied, thanks!
Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
[1/2] driver core: platform: fix sw node use-after-free on device release
commit: c4cd09d29529
The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).
The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.
Johan Hovold
maintainerQuoted 5 lines · Johan Hovold
Sashiko noted a new use-after-free issue when freeing platform device which has a software node as its primary fw node.
Turns out that can cause trouble also when drivers or subsystems remove software nodes themselves.
Hmm. The subtleties of the software node implementation got the better of both the LLM and its operator it seems.
Clearing the secondary node when the device has only a software node does indeed clear the fwnode pointer so there was never any UAF here.
I've dropped the first patch from the driver core testing branch.
Sorry for the noise.
Johan
Johan Hovold
maintainerQuoted 4 lines · Johan Hovold
Quoted 3 lines · Johan Hovold
Sashiko noted a new use-after-free issue when freeing platform device which has a software node as its primary fw node.
Quoted 8 lines
Quoted 2 lines
Turns out that can cause trouble also when drivers or subsystems remove software nodes themselves.
Hmm. The subtleties of the software node implementation got the better of both the LLM and its operator it seems.
Clearing the secondary node when the device has only a software node does indeed clear the fwnode pointer so there was never any UAF here.
Except when the software node is indeed primary... Then the UAF issue is real.
I'll send a v2.
Johan