From: Dmitry Osipenko Date: Mon, 13 Aug 2018 17:14:00 +0000 (+0300) Subject: thermal: core: Fix use-after-free in thermal_cooling_device_destroy_sysfs X-Git-Tag: v4.20-rc1~48^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3c587768271e9c20276522025729e4ebca51583b;p=thirdparty%2Fkernel%2Flinux.git 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: # v4.17+ Fixes: 8ea229511e06 ("thermal: Add cooling device's statistics in sysfs") Signed-off-by: Dmitry Osipenko Acked-by: Viresh Kumar Acked-by: Eduardo Valentin Signed-off-by: Zhang Rui --- diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 6ab982309e6a0..4417781008878 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -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);