Skip to contentSkip to conversation

thermal: core: Fix use-after-free in thermal_cooling_device_destroy_sysfs

PATCH In review Dmitry Osipenko 3 participants 3 messages Aug 13, 2018 – Sep 5, 2018 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thermal: core: Fix use-after-free in thermal_cooling_device_destroy_sysfs

This patch fixes use-after-free that was detected by KASAN. The bug is triggered on a CPUFreq driver module unload by freeing 'cdev' on device unregister and then using the freed structure during of the cdev's sysfs data destruction. The solution is to unregister the sysfs at first, then destroy sysfs data and finally release the cooling device.

Cc <[email protected]> # v4.17+
Fixes 8ea229511e06 ("thermal: Add cooling device's statistics in sysfs")
---
 drivers/thermal/thermal_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +2 -1
index 6ab982309e6a..441778100887 100644
@@ -1102,8 +1102,9 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
 	mutex_unlock(&thermal_list_lock);
 
 	ida_simple_remove(&thermal_cdev_ida, cdev->id);
-	device_unregister(&cdev->device);
+	device_del(&cdev->device);
 	thermal_cooling_device_destroy_sysfs(cdev);
+	put_device(&cdev->device);
 }
 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
 
Signature · 1 line
2.18.0
Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Dmitry Osipenko

This patch fixes use-after-free that was detected by KASAN. The bug is triggered on a CPUFreq driver module unload by freeing 'cdev' on device unregister and then using the freed structure during of the cdev's sysfs data destruction. The solution is to unregister the sysfs at first, then destroy sysfs data and finally release the cooling device.

Cc <[email protected]> # v4.17+
Fixes 8ea229511e06 ("thermal: Add cooling device's statistics in sysfs")
---
 drivers/thermal/thermal_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +0 -0
index 6ab982309e6a..441778100887 100644

drivers/thermal/thermal_core.c void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) · line 1109

Quoted 9 lines
drivers/thermal/thermal_core.c +2 -1
@@ -1102,8 +1102,9 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
 	mutex_unlock(&thermal_list_lock);
 
 	ida_simple_remove(&thermal_cdev_ida, cdev->id);
-	device_unregister(&cdev->device);
+	device_del(&cdev->device);
 	thermal_cooling_device_destroy_sysfs(cdev);
+	put_device(&cdev->device);
 }
 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Signature · 1 line
viresh
Acked-by on v1 1/1
#2 lore raw
In reply to Dmitry Osipenko
Quoted 9 lines · Dmitry Osipenko

This patch fixes use-after-free that was detected by KASAN. The bug is triggered on a CPUFreq driver module unload by freeing 'cdev' on device unregister and then using the freed structure during of the cdev's sysfs data destruction. The solution is to unregister the sysfs at first, then destroy sysfs data and finally release the cooling device.

Cc <[email protected]> # v4.17+
Fixes 8ea229511e06 ("thermal: Add cooling device's statistics in sysfs")

Rui, can you please queue this one?

Quoted 22 lines
---
 drivers/thermal/thermal_core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +2 -1
index 6ab982309e6a..441778100887 100644
@@ -1102,8 +1102,9 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
 	mutex_unlock(&thermal_list_lock);
 
 	ida_simple_remove(&thermal_cdev_ida, cdev->id);
-	device_unregister(&cdev->device);
+	device_del(&cdev->device);
 	thermal_cooling_device_destroy_sysfs(cdev);
+	put_device(&cdev->device);
 }
 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
 
Signature · 1 line
2.18.0