Fix two memory leaks for kobject name, thanks!
Gaosheng Cui (2): kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype kobject: fix memory leak when kobject_add_varg() returns error
lib/kobject.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
If a kset with uninitialized kset->kobj.ktype be registered, kset_register() will return error, and the kset.kobj.name allocated by kobject_set_name() will be leaked.
To mitigate this, we free the name in kset_register() when an error is encountered due to uninitialized kset->kobj.ktype.
Fixes: 4d0fe8c52bb3 ("kobject: Add sanity check for kset->kobj.ktype in kset_register()") Signed-off-by: Gaosheng Cui cuigaosheng1@huawei.com --- lib/kobject.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/lib/kobject.c b/lib/kobject.c index 72fa20f405f1..ecca72622933 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -862,6 +862,8 @@ int kset_register(struct kset *k) return -EINVAL;
if (!k->kobj.ktype) { + kfree_const(k->kobj.name); + k->kobj.name = NULL; pr_err("must have a ktype to be initialized properly!\n"); return -EINVAL; }
On Wed, Sep 25, 2024 at 08:07:46PM +0800, Gaosheng Cui wrote:
If a kset with uninitialized kset->kobj.ktype be registered, kset_register() will return error, and the kset.kobj.name allocated by kobject_set_name() will be leaked.
To mitigate this, we free the name in kset_register() when an error is encountered due to uninitialized kset->kobj.ktype.
Fixes: 4d0fe8c52bb3 ("kobject: Add sanity check for kset->kobj.ktype in kset_register()") Signed-off-by: Gaosheng Cui cuigaosheng1@huawei.com
lib/kobject.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/lib/kobject.c b/lib/kobject.c index 72fa20f405f1..ecca72622933 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -862,6 +862,8 @@ int kset_register(struct kset *k) return -EINVAL; if (!k->kobj.ktype) {
kfree_const(k->kobj.name);
pr_err("must have a ktype to be initialized properly!\n"); return -EINVAL; }k->kobj.name = NULL;
-- 2.25.1
Hi,
This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.
You are receiving this message because of the following common error(s) as indicated below:
- You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this.
If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.
thanks,
greg k-h's patch email bot
On Wed, Sep 25, 2024 at 08:07:46PM +0800, Gaosheng Cui wrote:
If a kset with uninitialized kset->kobj.ktype be registered,
Does that happen today with any in-kernel code? If so, let's fix those kset instances, right?
kset_register() will return error, and the kset.kobj.name allocated by kobject_set_name() will be leaked.
To mitigate this, we free the name in kset_register() when an error is encountered due to uninitialized kset->kobj.ktype.
How did you hit this?
thanks,
greg k-h
Inject fault while loading module, kobject_add_varg() may fail. If it fails, the kset.kobj.name allocated by kobject_set_name_vargs() may be leaked, the call trace as follow:
unreferenced object 0xffff8884ef4fccc0 (size 32): comm "modprobe", pid 56721, jiffies 4304802933 backtrace (crc 4b069391): [<ffffffff85e9fb2b>] kmemleak_alloc+0x4b/0x80 [<ffffffff83664674>] __kmalloc_node_track_caller_noprof+0x3d4/0x510 [<ffffffff83510656>] kstrdup+0x46/0x80 [<ffffffff8351070f>] kstrdup_const+0x6f/0x90 [<ffffffff84213842>] kvasprintf_const+0x112/0x190 [<ffffffff85d8446b>] kobject_set_name_vargs+0x5b/0x160 [<ffffffff85d85969>] kobject_init_and_add+0xc9/0x170 [<ffffffff83661788>] sysfs_slab_add+0x188/0x230 [<ffffffff83665e24>] do_kmem_cache_create+0x4d4/0x5a0 [<ffffffff835343cd>] __kmem_cache_create_args+0x18d/0x310 [<ffffffffc64a08b4>] 0xffffffffc64a08b4 [<ffffffffc64a005f>] 0xffffffffc64a005f [<ffffffff82a04198>] do_one_initcall+0xb8/0x590 [<ffffffff82f0c626>] do_init_module+0x256/0x7d0 [<ffffffff82f12f73>] load_module+0x5953/0x7010 [<ffffffff82f14b0a>] init_module_from_file+0xea/0x140
To mitigate this, we need to check return value of kobject_add_internal, and free the name when an error is encountered.
Fixes: 244f6cee9a92 ("kobject: add kobject_add_ng function") Signed-off-by: Gaosheng Cui cuigaosheng1@huawei.com --- lib/kobject.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/lib/kobject.c b/lib/kobject.c index ecca72622933..365e2ad12cba 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -371,7 +371,13 @@ static __printf(3, 0) int kobject_add_varg(struct kobject *kobj, return retval; } kobj->parent = parent; - return kobject_add_internal(kobj); + retval = kobject_add_internal(kobj); + if (retval) { + kfree_const(kobj->name); + kobj->name = NULL; + } + + return retval; }
/**
On Wed, Sep 25, 2024 at 08:07:47PM +0800, Gaosheng Cui wrote:
Inject fault while loading module, kobject_add_varg() may fail. If it fails, the kset.kobj.name allocated by kobject_set_name_vargs() may be leaked, the call trace as follow:
unreferenced object 0xffff8884ef4fccc0 (size 32): comm "modprobe", pid 56721, jiffies 4304802933 backtrace (crc 4b069391): [<ffffffff85e9fb2b>] kmemleak_alloc+0x4b/0x80 [<ffffffff83664674>] __kmalloc_node_track_caller_noprof+0x3d4/0x510 [<ffffffff83510656>] kstrdup+0x46/0x80 [<ffffffff8351070f>] kstrdup_const+0x6f/0x90 [<ffffffff84213842>] kvasprintf_const+0x112/0x190 [<ffffffff85d8446b>] kobject_set_name_vargs+0x5b/0x160 [<ffffffff85d85969>] kobject_init_and_add+0xc9/0x170 [<ffffffff83661788>] sysfs_slab_add+0x188/0x230 [<ffffffff83665e24>] do_kmem_cache_create+0x4d4/0x5a0 [<ffffffff835343cd>] __kmem_cache_create_args+0x18d/0x310 [<ffffffffc64a08b4>] 0xffffffffc64a08b4 [<ffffffffc64a005f>] 0xffffffffc64a005f [<ffffffff82a04198>] do_one_initcall+0xb8/0x590 [<ffffffff82f0c626>] do_init_module+0x256/0x7d0 [<ffffffff82f12f73>] load_module+0x5953/0x7010 [<ffffffff82f14b0a>] init_module_from_file+0xea/0x140
To mitigate this, we need to check return value of kobject_add_internal, and free the name when an error is encountered.
Fixes: 244f6cee9a92 ("kobject: add kobject_add_ng function") Signed-off-by: Gaosheng Cui cuigaosheng1@huawei.com
lib/kobject.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/lib/kobject.c b/lib/kobject.c index ecca72622933..365e2ad12cba 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -371,7 +371,13 @@ static __printf(3, 0) int kobject_add_varg(struct kobject *kobj, return retval; } kobj->parent = parent;
- return kobject_add_internal(kobj);
- retval = kobject_add_internal(kobj);
- if (retval) {
kfree_const(kobj->name);
kobj->name = NULL;
- }
- return retval;
} /** -- 2.25.1
Hi,
This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.
You are receiving this message because of the following common error(s) as indicated below:
- You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this.
If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.
thanks,
greg k-h's patch email bot
Hi,
Thanks for your patch.
FYI: kernel test robot notices the stable kernel rule is not satisfied.
The check is based on https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html#opti...
Rule: add the tag "Cc: stable@vger.kernel.org" in the sign-off area to have the patch automatically included in the stable tree. Subject: [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error Link: https://lore.kernel.org/stable/20240925120747.1930709-3-cuigaosheng1%40huawe...
linux-stable-mirror@lists.linaro.org