diff mbox series

thermal: core: fix some possible name leaks in error paths

Message ID 20221115091945.921469-1-yangyingliang@huawei.com (mailing list archive)
State Mainlined, archived
Headers show
Series thermal: core: fix some possible name leaks in error paths | expand

Commit Message

Yang Yingliang Nov. 15, 2022, 9:19 a.m. UTC
In some error paths before device_register(), the names allocated
by dev_set_name() are not freed. Move dev_set_name() front to
device_register(), so the name can be freed while calling
put_device().

Fixes: 1dd7128b839f ("thermal/core: Fix null pointer dereference in thermal_release()")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/thermal/thermal_core.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

Comments

Rafael J. Wysocki Nov. 25, 2022, 6:53 p.m. UTC | #1
On Tue, Nov 15, 2022 at 10:21 AM Yang Yingliang
<yangyingliang@huawei.com> wrote:
>
> In some error paths before device_register(), the names allocated
> by dev_set_name() are not freed. Move dev_set_name() front to
> device_register(), so the name can be freed while calling
> put_device().
>
> Fixes: 1dd7128b839f ("thermal/core: Fix null pointer dereference in thermal_release()")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
>  drivers/thermal/thermal_core.c | 18 ++++++++++--------
>  1 file changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 117eeaf7dd24..46c7adf0a1fc 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -883,10 +883,6 @@ __thermal_cooling_device_register(struct device_node *np,
>         cdev->id = ret;
>         id = ret;
>
> -       ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
> -       if (ret)
> -               goto out_ida_remove;
> -
>         cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
>         if (!cdev->type) {
>                 ret = -ENOMEM;
> @@ -901,6 +897,11 @@ __thermal_cooling_device_register(struct device_node *np,
>         cdev->device.class = &thermal_class;
>         cdev->devdata = devdata;
>         thermal_cooling_device_setup_sysfs(cdev);
> +       ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
> +       if (ret) {
> +               thermal_cooling_device_destroy_sysfs(cdev);
> +               goto out_ida_remove;
> +       }
>         ret = device_register(&cdev->device);
>         if (ret)
>                 goto out_kfree_type;
> @@ -1234,10 +1235,6 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
>         tz->id = id;
>         strscpy(tz->type, type, sizeof(tz->type));
>
> -       result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
> -       if (result)
> -               goto remove_id;
> -
>         if (!ops->critical)
>                 ops->critical = thermal_zone_device_critical;
>
> @@ -1260,6 +1257,11 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
>         /* A new thermal zone needs to be updated anyway. */
>         atomic_set(&tz->need_update, 1);
>
> +       result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
> +       if (result) {
> +               thermal_zone_destroy_device_groups(tz);
> +               goto remove_id;
> +       }
>         result = device_register(&tz->device);
>         if (result)
>                 goto release_device;
> --

Applied as 6.2 material, but please note that I had to rebase it, so
please check the result in my bleeding-edge branch.
diff mbox series

Patch

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 117eeaf7dd24..46c7adf0a1fc 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -883,10 +883,6 @@  __thermal_cooling_device_register(struct device_node *np,
 	cdev->id = ret;
 	id = ret;
 
-	ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
-	if (ret)
-		goto out_ida_remove;
-
 	cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
 	if (!cdev->type) {
 		ret = -ENOMEM;
@@ -901,6 +897,11 @@  __thermal_cooling_device_register(struct device_node *np,
 	cdev->device.class = &thermal_class;
 	cdev->devdata = devdata;
 	thermal_cooling_device_setup_sysfs(cdev);
+	ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
+	if (ret) {
+		thermal_cooling_device_destroy_sysfs(cdev);
+		goto out_ida_remove;
+	}
 	ret = device_register(&cdev->device);
 	if (ret)
 		goto out_kfree_type;
@@ -1234,10 +1235,6 @@  thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
 	tz->id = id;
 	strscpy(tz->type, type, sizeof(tz->type));
 
-	result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
-	if (result)
-		goto remove_id;
-
 	if (!ops->critical)
 		ops->critical = thermal_zone_device_critical;
 
@@ -1260,6 +1257,11 @@  thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
 	/* A new thermal zone needs to be updated anyway. */
 	atomic_set(&tz->need_update, 1);
 
+	result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
+	if (result) {
+		thermal_zone_destroy_device_groups(tz);
+		goto remove_id;
+	}
 	result = device_register(&tz->device);
 	if (result)
 		goto release_device;