提交 1467ec66 编写于 作者: Z Ziyang Xuan 提交者: Zheng Zengkai

thermal/core: fix a UAF bug in __thermal_cooling_device_register()

stable inclusion
from stable-v5.10.121
commit b132abaa6515e14e0db292389c25007d666e1925
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I5L6CQ

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=b132abaa6515e14e0db292389c25007d666e1925

--------------------------------

commit 0a5c2671 upstream.

When device_register() return failed, program will goto out_kfree_type
to release 'cdev->device' by put_device(). That will call thermal_release()
to free 'cdev'. But the follow-up processes access 'cdev' continually.
That trggers the UAF bug.
Acked-by: NXie XiuQi <xiexiuqi@huawei.com>

====================================================================
BUG: KASAN: use-after-free in __thermal_cooling_device_register+0x75b/0xa90
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
 dump_stack_lvl+0xe2/0x152
 print_address_description.constprop.0+0x21/0x140
 ? __thermal_cooling_device_register+0x75b/0xa90
 kasan_report.cold+0x7f/0x11b
 ? __thermal_cooling_device_register+0x75b/0xa90
 __thermal_cooling_device_register+0x75b/0xa90
 ? memset+0x20/0x40
 ? __sanitizer_cov_trace_pc+0x1d/0x50
 ? __devres_alloc_node+0x130/0x180
 devm_thermal_of_cooling_device_register+0x67/0xf0
 max6650_probe.cold+0x557/0x6aa
......

Freed by task 258:
 kasan_save_stack+0x1b/0x40
 kasan_set_track+0x1c/0x30
 kasan_set_free_info+0x20/0x30
 __kasan_slab_free+0x109/0x140
 kfree+0x117/0x4c0
 thermal_release+0xa0/0x110
 device_release+0xa7/0x240
 kobject_put+0x1ce/0x540
 put_device+0x20/0x30
 __thermal_cooling_device_register+0x731/0xa90
 devm_thermal_of_cooling_device_register+0x67/0xf0
 max6650_probe.cold+0x557/0x6aa [max6650]

Do not use 'cdev' again after put_device() to fix the problem like doing
in thermal_zone_device_register().

[dlezcano]: as requested by Rafael, change the affectation into two statements.

Fixes: 58483761 ("thermal/drivers/core: Use a char pointer for the cooling device name")
Signed-off-by: NZiyang Xuan <william.xuanziyang@huawei.com>
Reported-by: Nkernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/r/20211015024504.947520-1-william.xuanziyang@huawei.comSigned-off-by: NDaniel Lezcano <daniel.lezcano@linaro.org>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 0b78257e
...@@ -1092,7 +1092,7 @@ __thermal_cooling_device_register(struct device_node *np, ...@@ -1092,7 +1092,7 @@ __thermal_cooling_device_register(struct device_node *np,
{ {
struct thermal_cooling_device *cdev; struct thermal_cooling_device *cdev;
struct thermal_zone_device *pos = NULL; struct thermal_zone_device *pos = NULL;
int ret; int id, ret;
if (!ops || !ops->get_max_state || !ops->get_cur_state || if (!ops || !ops->get_max_state || !ops->get_cur_state ||
!ops->set_cur_state) !ops->set_cur_state)
...@@ -1106,6 +1106,7 @@ __thermal_cooling_device_register(struct device_node *np, ...@@ -1106,6 +1106,7 @@ __thermal_cooling_device_register(struct device_node *np,
if (ret < 0) if (ret < 0)
goto out_kfree_cdev; goto out_kfree_cdev;
cdev->id = ret; cdev->id = ret;
id = ret;
cdev->type = kstrdup(type ? type : "", GFP_KERNEL); cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
if (!cdev->type) { if (!cdev->type) {
...@@ -1147,8 +1148,9 @@ __thermal_cooling_device_register(struct device_node *np, ...@@ -1147,8 +1148,9 @@ __thermal_cooling_device_register(struct device_node *np,
thermal_cooling_device_destroy_sysfs(cdev); thermal_cooling_device_destroy_sysfs(cdev);
kfree(cdev->type); kfree(cdev->type);
put_device(&cdev->device); put_device(&cdev->device);
cdev = NULL;
out_ida_remove: out_ida_remove:
ida_simple_remove(&thermal_cdev_ida, cdev->id); ida_simple_remove(&thermal_cdev_ida, id);
out_kfree_cdev: out_kfree_cdev:
return ERR_PTR(ret); return ERR_PTR(ret);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册