[PATCH 06/11] firmware: qemu_fw_cfg: fix possible memory leak in fw_cfg_build_symlink()

Yang Yingliang
2022-10-21T02:23:02Z

Inject fault while loading module, kset_register() may fail, if
it fails, but the refcount of kobject is not decreased to 0, the
name allocated in kobject_set_name() is leaked. To fix this by
calling kset_put(), so that name can be freed in callback function
kobject_cleanup() and 'subdir' is freed in kset_release().

unreferenced object 0xffff88810ad69050 (size 8):
comm "swapper/0", pid 1, jiffies 4294677178 (age 38.812s)
hex dump (first 8 bytes):
65 74 63 00 81 88 ff ff etc.....
backtrace:
[<00000000a80c7bf1>] __kmalloc_node_track_caller+0x44/0x1b0
[<000000003f0167c7>] kstrdup+0x3a/0x70
[<0000000049336709>] kstrdup_const+0x41/0x60
[<00000000175616e4>] kvasprintf_const+0xf5/0x180
[<000000004bcc30f7>] kobject_set_name_vargs+0x56/0x150
[<000000004b0251bd>] kobject_set_name+0xab/0xe0
[<00000000700151fb>] fw_cfg_sysfs_probe+0xa5b/0x1320

Fixes: 246c46ebaeae ("firmware: create directory hierarchy for sysfs fw_cfg entries")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
drivers/firmware/qemu_fw_cfg.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c
index a69399a6b7c0..d036e69cabbb 100644
--- a/drivers/firmware/qemu_fw_cfg.c
+++ b/drivers/firmware/qemu_fw_cfg.c
@@ -544,7 +544,7 @@ static int fw_cfg_build_symlink(struct kset *dir,
}
ret = kset_register(subdir);
if (ret) {
- kfree(subdir);
+ kset_put(subdir);
break;
}

--
2.25.1