* [PATCH -next 0/2] Fix memory leaks for kobject
@ 2024-09-25 12:07 Gaosheng Cui
2024-09-25 12:07 ` [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype Gaosheng Cui
2024-09-25 12:07 ` [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error Gaosheng Cui
0 siblings, 2 replies; 8+ messages in thread
From: Gaosheng Cui @ 2024-09-25 12:07 UTC (permalink / raw)
To: gregkh, rafael, akpm, thunder.leizhen, cuigaosheng1, wangweiyang2
Cc: linux-kernel, stable
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(-)
--
2.25.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype
2024-09-25 12:07 [PATCH -next 0/2] Fix memory leaks for kobject Gaosheng Cui
@ 2024-09-25 12:07 ` Gaosheng Cui
2024-09-25 12:18 ` Greg KH
2024-09-25 12:19 ` Greg KH
2024-09-25 12:07 ` [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error Gaosheng Cui
1 sibling, 2 replies; 8+ messages in thread
From: Gaosheng Cui @ 2024-09-25 12:07 UTC (permalink / raw)
To: gregkh, rafael, akpm, thunder.leizhen, cuigaosheng1, wangweiyang2
Cc: linux-kernel, stable
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;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error
2024-09-25 12:07 [PATCH -next 0/2] Fix memory leaks for kobject Gaosheng Cui
2024-09-25 12:07 ` [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype Gaosheng Cui
@ 2024-09-25 12:07 ` Gaosheng Cui
2024-09-25 12:18 ` Greg KH
2024-09-25 13:25 ` kernel test robot
1 sibling, 2 replies; 8+ messages in thread
From: Gaosheng Cui @ 2024-09-25 12:07 UTC (permalink / raw)
To: gregkh, rafael, akpm, thunder.leizhen, cuigaosheng1, wangweiyang2
Cc: linux-kernel, stable
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype
2024-09-25 12:07 ` [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype Gaosheng Cui
@ 2024-09-25 12:18 ` Greg KH
2024-09-25 12:19 ` Greg KH
1 sibling, 0 replies; 8+ messages in thread
From: Greg KH @ 2024-09-25 12:18 UTC (permalink / raw)
To: Gaosheng Cui
Cc: rafael, akpm, thunder.leizhen, wangweiyang2, linux-kernel, stable
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);
> + k->kobj.name = NULL;
> pr_err("must have a ktype to be initialized properly!\n");
> return -EINVAL;
> }
> --
> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error
2024-09-25 12:07 ` [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error Gaosheng Cui
@ 2024-09-25 12:18 ` Greg KH
2024-09-25 13:25 ` kernel test robot
1 sibling, 0 replies; 8+ messages in thread
From: Greg KH @ 2024-09-25 12:18 UTC (permalink / raw)
To: Gaosheng Cui
Cc: rafael, akpm, thunder.leizhen, wangweiyang2, linux-kernel, stable
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype
2024-09-25 12:07 ` [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype Gaosheng Cui
2024-09-25 12:18 ` Greg KH
@ 2024-09-25 12:19 ` Greg KH
[not found] ` <c928dc7c-3418-d6cb-9503-e0c9a48adc1c@huawei.com>
1 sibling, 1 reply; 8+ messages in thread
From: Greg KH @ 2024-09-25 12:19 UTC (permalink / raw)
To: Gaosheng Cui
Cc: rafael, akpm, thunder.leizhen, wangweiyang2, linux-kernel, stable
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error
2024-09-25 12:07 ` [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error Gaosheng Cui
2024-09-25 12:18 ` Greg KH
@ 2024-09-25 13:25 ` kernel test robot
1 sibling, 0 replies; 8+ messages in thread
From: kernel test robot @ 2024-09-25 13:25 UTC (permalink / raw)
To: Gaosheng Cui; +Cc: stable, oe-kbuild-all
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#option-1
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%40huawei.com
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype
[not found] ` <c928dc7c-3418-d6cb-9503-e0c9a48adc1c@huawei.com>
@ 2024-09-26 8:17 ` Greg KH
0 siblings, 0 replies; 8+ messages in thread
From: Greg KH @ 2024-09-26 8:17 UTC (permalink / raw)
To: cuigaosheng
Cc: rafael, akpm, thunder.leizhen, wangweiyang2, linux-kernel, stable
On Thu, Sep 26, 2024 at 10:56:24AM +0800, cuigaosheng wrote:
> On 2024/9/25 20:19, Greg KH wrote:
>
> > 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?
>
> I didn't find this kset instance in kernel code,itwas discovered through code review.
Great, then it is not a real issue :)
> > > 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?
>
> I am testing kernel functionality through fault injection, and I discovered
> it whenI was locating the issue fixed by another patch, I haven't found this
> wrong usage in the kernel, but we can construct it by fault injection,
"fault injection" also shows things that are impossible to ever have
happen as well, so our "need" to fix them is usually very low, right?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-09-26 8:17 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-25 12:07 [PATCH -next 0/2] Fix memory leaks for kobject Gaosheng Cui
2024-09-25 12:07 ` [PATCH -next 1/2] kobject: fix memory leak in kset_register() due to uninitialized kset->kobj.ktype Gaosheng Cui
2024-09-25 12:18 ` Greg KH
2024-09-25 12:19 ` Greg KH
[not found] ` <c928dc7c-3418-d6cb-9503-e0c9a48adc1c@huawei.com>
2024-09-26 8:17 ` Greg KH
2024-09-25 12:07 ` [PATCH -next 2/2] kobject: fix memory leak when kobject_add_varg() returns error Gaosheng Cui
2024-09-25 12:18 ` Greg KH
2024-09-25 13:25 ` kernel test robot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox