Skip to contentSkip to conversation

kobject: fix possible memory leak in kset_create_and_add()

PATCH Superseded Yang Yingliang 2 participants 6 messages Oct 17, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kobject: fix possible memory leak in kset_create_and_add()

If kset_register() fails in kset_create_and_add(), the name allocated in kset_create() will be leaked. To fix this by calling kset_put() so that the name will be freed in callback function kobject_cleanup() and kset will be freed in kset_release().

unreferenced object 0xffff888103cc8c08 (size 8):
comm "modprobe", pid 508, jiffies 4294915182 (age 120.020s) hex dump (first 8 bytes):

    62 79 5f 6e 61 6d 65 00                          by_name.
  backtrace:
    [<00000000572f97f9>] __kmalloc_track_caller+0x1ae/0x320
    [<00000000a167a5cc>] kstrdup+0x3a/0x70
    [<000000001cd0d05e>] kstrdup_const+0x68/0x80
    [<00000000b9101e6d>] kvasprintf_const+0x10b/0x190
    [<0000000088f2b8df>] kobject_set_name_vargs+0x56/0x150
    [<000000003f8aca68>] kobject_set_name+0xab/0xe0
    [<00000000249f7816>] kset_create_and_add+0x72/0x200
Fixes b727c702896f ("kset: add kset_create_and_add function")
---
 lib/kobject.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/kobject.c +1 -1
index a0b2dbfcfa23..f5e943c9027b 100644
@@ -982,7 +982,7 @@ struct kset *kset_create_and_add(const char *name,
 		return NULL;
 	error = kset_register(kset);
 	if (error) {
-		kfree(kset);
+		kset_put(kset);
 		return NULL;
 	}
 	return kset;
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 2 lines · Yang Yingliang

If kset_register() fails in kset_create_and_add(), the name allocated in kset_create() will be leaked.

How is kset_create_and_add() failing? Is this in a real kernel, or created with a fake fault injection?

thanks,

greg k-h

#2 lore raw

Hi,

Quoted 5 lines · Greg KH
Quoted 2 lines · Yang Yingliang

If kset_register() fails in kset_create_and_add(), the name allocated in kset_create() will be leaked.

How is kset_create_and_add() failing? Is this in a real kernel, or created with a fake fault injection?

Inject fault while probing module qemu_fw_cfg, kset_create_and_add() may fail.

Thanks,
Yang

Quoted 5 lines

thanks,

greg k-h
.

maintainer
#3 lore raw
Quoted 10 lines · Yang Yingliang

Hi,

Quoted 5 lines · Greg KH
Quoted 2 lines · Yang Yingliang

If kset_register() fails in kset_create_and_add(), the name allocated in kset_create() will be leaked.

How is kset_create_and_add() failing? Is this in a real kernel, or created with a fake fault injection?

Inject fault while probing module qemu_fw_cfg, kset_create_and_add() may fail.

Ah good, it's never being hit in a real situation. The next time you submit patches that are found like this, please include this type of information.

thanks,

greg k-h

#4 lore raw
Quoted 7 lines · Greg KH
Quoted 6 lines · Yang Yingliang

Hi,

Quoted 3 lines · Greg KH
Quoted 2 lines · Yang Yingliang

If kset_register() fails in kset_create_and_add(), the name allocated in kset_create() will be leaked.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

How is kset_create_and_add() failing? Is this in a real kernel, or created with a fake fault injection?

Inject fault while probing module qemu_fw_cfg, kset_create_and_add() may fail.

Ah good, it's never being hit in a real situation. The next time you submit patches that are found like this, please include this type of information.

OK. Do I need to send a v2 with commit message update.

Thanks,
Yang

Quoted 5 lines

thanks,

greg k-h
.

maintainer
#5 lore raw
Quoted 9 lines · Yang Yingliang
Quoted 7 lines · Greg KH
Quoted 6 lines · Yang Yingliang

Hi,

Quoted 3 lines · Greg KH
Quoted 2 lines · Yang Yingliang

If kset_register() fails in kset_create_and_add(), the name allocated in kset_create() will be leaked.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

How is kset_create_and_add() failing? Is this in a real kernel, or created with a fake fault injection?

Inject fault while probing module qemu_fw_cfg, kset_create_and_add() may fail.

Ah good, it's never being hit in a real situation. The next time you submit patches that are found like this, please include this type of information.

OK. Do I need to send a v2 with commit message update.

That would be wonderful for you to do, thank you!

Also do the same thing for the other patches you sent that fix up error paths like this.

thanks,

greg k-h