Skip to content

Commit

Permalink
thermal/core: Fix null pointer dereference in thermal_release()
Browse files Browse the repository at this point in the history
If both dev_set_name() and device_register() failed, then null pointer
dereference occurs in thermal_release() which will use strncmp() to
compare the name.

So fix it by adding dev_set_name() return value check.

Signed-off-by: Yuanzheng Song <songyuanzheng@huawei.com>
Link: https://lore.kernel.org/r/20211015083230.67658-1-songyuanzheng@huawei.com
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
  • Loading branch information
Yuanzheng Song authored and Daniel Lezcano committed Oct 15, 2021
1 parent c3131bd commit 1dd7128
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions drivers/thermal/thermal_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,10 @@ __thermal_cooling_device_register(struct device_node *np,
goto out_kfree_cdev;
cdev->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;
Expand All @@ -918,7 +922,6 @@ __thermal_cooling_device_register(struct device_node *np,
cdev->device.class = &thermal_class;
cdev->devdata = devdata;
thermal_cooling_device_setup_sysfs(cdev);
dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
ret = device_register(&cdev->device);
if (ret)
goto out_kfree_type;
Expand Down Expand Up @@ -1229,6 +1232,10 @@ thermal_zone_device_register(const char *type, int trips, int mask,
tz->id = id;
strlcpy(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;

Expand All @@ -1250,7 +1257,6 @@ thermal_zone_device_register(const char *type, int trips, int mask,
/* A new thermal zone needs to be updated anyway. */
atomic_set(&tz->need_update, 1);

dev_set_name(&tz->device, "thermal_zone%d", tz->id);
result = device_register(&tz->device);
if (result)
goto release_device;
Expand Down

0 comments on commit 1dd7128

Please sign in to comment.