public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
@ 2026-04-11  6:21 Guangshuo Li
  2026-04-11  7:45 ` Baolin Wang
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Guangshuo Li @ 2026-04-11  6:21 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
	Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
	Barry Song, Lance Yang, linux-mm, linux-kernel
  Cc: Guangshuo Li, stable

After kobject_init_and_add(), the lifetime of the embedded struct
kobject is expected to be managed through the kobject core reference
counting.

In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
directly with kfree() rather than releasing the kobject reference with
kobject_put(). This may leave the reference count of the embedded struct
kobject unbalanced, resulting in a refcount leak and potentially leading
to a use-after-free.

Fix this by using kobject_put(&thpsize->kobj) in the failure path and
letting thpsize_release() handle the final cleanup.

Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 mm/huge_memory.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 40cf59301c21..ae6ed483cd53 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -726,11 +726,8 @@ static struct thpsize *thpsize_create(int order, struct kobject *parent)
 
 	ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent,
 				   "hugepages-%lukB", size);
-	if (ret) {
-		kfree(thpsize);
-		goto err;
-	}
-
+	if (ret)
+		goto err_put;
 
 	ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp);
 	if (ret)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-11  6:21 [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path Guangshuo Li
@ 2026-04-11  7:45 ` Baolin Wang
  2026-04-11  8:32 ` Barry Song
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Baolin Wang @ 2026-04-11  7:45 UTC (permalink / raw)
  To: Guangshuo Li, Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Zi Yan, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
	Barry Song, Lance Yang, linux-mm, linux-kernel
  Cc: stable



On 4/11/26 2:21 PM, Guangshuo Li wrote:
> After kobject_init_and_add(), the lifetime of the embedded struct
> kobject is expected to be managed through the kobject core reference
> counting.
> 
> In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
> directly with kfree() rather than releasing the kobject reference with
> kobject_put(). This may leave the reference count of the embedded struct
> kobject unbalanced, resulting in a refcount leak and potentially leading
> to a use-after-free.
> 
> Fix this by using kobject_put(&thpsize->kobj) in the failure path and
> letting thpsize_release() handle the final cleanup.
> 
> Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---

Make sense to me.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-11  6:21 [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path Guangshuo Li
  2026-04-11  7:45 ` Baolin Wang
@ 2026-04-11  8:32 ` Barry Song
  2026-04-11  8:34   ` Barry Song
  2026-04-11 14:28 ` Lance Yang
  2026-04-12  1:37 ` Zi Yan
  3 siblings, 1 reply; 9+ messages in thread
From: Barry Song @ 2026-04-11  8:32 UTC (permalink / raw)
  To: Guangshuo Li
  Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
	Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
	Lance Yang, linux-mm, linux-kernel, stable

On Sat, Apr 11, 2026 at 2:22 PM Guangshuo Li <lgs201920130244@gmail.com> wrote:
>
> After kobject_init_and_add(), the lifetime of the embedded struct
> kobject is expected to be managed through the kobject core reference
> counting.
>
> In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
> directly with kfree() rather than releasing the kobject reference with
> kobject_put(). This may leave the reference count of the embedded struct
> kobject unbalanced, resulting in a refcount leak and potentially leading
> to a use-after-free.
>
> Fix this by using kobject_put(&thpsize->kobj) in the failure path and
> letting thpsize_release() handle the final cleanup.
>
> Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>

I’m fine with the patch, but could you send a v2 to drop
the err label, which is no longer used? Alternatively,
could you rename err_put to err?

@@ -825,9 +825,8 @@ static struct thpsize *thpsize_create(int order,
struct kobject *parent)
        }

        return thpsize;
-err_put:
-       kobject_put(&thpsize->kobj);
 err:
+       kobject_put(&thpsize->kobj);
        return ERR_PTR(ret);
 }


> ---
>  mm/huge_memory.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 40cf59301c21..ae6ed483cd53 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -726,11 +726,8 @@ static struct thpsize *thpsize_create(int order, struct kobject *parent)
>
>         ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent,
>                                    "hugepages-%lukB", size);
> -       if (ret) {
> -               kfree(thpsize);
> -               goto err;
> -       }
> -
> +       if (ret)
> +               goto err_put;
>
>         ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp);
>         if (ret)
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-11  8:32 ` Barry Song
@ 2026-04-11  8:34   ` Barry Song
  0 siblings, 0 replies; 9+ messages in thread
From: Barry Song @ 2026-04-11  8:34 UTC (permalink / raw)
  To: Guangshuo Li
  Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Zi Yan,
	Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
	Lance Yang, linux-mm, linux-kernel, stable

On Sat, Apr 11, 2026 at 4:32 PM Barry Song <baohua@kernel.org> wrote:
>
> On Sat, Apr 11, 2026 at 2:22 PM Guangshuo Li <lgs201920130244@gmail.com> wrote:
> >
> > After kobject_init_and_add(), the lifetime of the embedded struct
> > kobject is expected to be managed through the kobject core reference
> > counting.
> >
> > In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
> > directly with kfree() rather than releasing the kobject reference with
> > kobject_put(). This may leave the reference count of the embedded struct
> > kobject unbalanced, resulting in a refcount leak and potentially leading
> > to a use-after-free.
> >
> > Fix this by using kobject_put(&thpsize->kobj) in the failure path and
> > letting thpsize_release() handle the final cleanup.
> >
> > Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
>
> I’m fine with the patch, but could you send a v2 to drop
> the err label, which is no longer used? Alternatively,
> could you rename err_put to err?
>
> @@ -825,9 +825,8 @@ static struct thpsize *thpsize_create(int order,
> struct kobject *parent)
>         }
>
>         return thpsize;
> -err_put:
> -       kobject_put(&thpsize->kobj);
>  err:
> +       kobject_put(&thpsize->kobj);
>         return ERR_PTR(ret);
>  }

Sorry, my mistake—err is still used by kzalloc_obj.

Reviewed-by: Barry Song <baohua@kernel.org>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-11  6:21 [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path Guangshuo Li
  2026-04-11  7:45 ` Baolin Wang
  2026-04-11  8:32 ` Barry Song
@ 2026-04-11 14:28 ` Lance Yang
  2026-04-12  1:49   ` Zi Yan
  2026-04-12  1:37 ` Zi Yan
  3 siblings, 1 reply; 9+ messages in thread
From: Lance Yang @ 2026-04-11 14:28 UTC (permalink / raw)
  To: lgs201920130244
  Cc: akpm, david, lorenzo.stoakes, ziy, baolin.wang, Liam.Howlett,
	npache, ryan.roberts, dev.jain, baohua, lance.yang, linux-mm,
	linux-kernel, stable


On Sat, Apr 11, 2026 at 02:21:52PM +0800, Guangshuo Li wrote:
>After kobject_init_and_add(), the lifetime of the embedded struct
>kobject is expected to be managed through the kobject core reference
>counting.
>
>In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
>directly with kfree() rather than releasing the kobject reference with
>kobject_put(). This may leave the reference count of the embedded struct

Right. As documented for kobject_init_and_add(), once it has been
called, the error path should go through kobject_put():

/**
 * kobject_init_and_add() - Initialize a kobject structure and add it to
 *                          the kobject hierarchy.
...
 *
 * This function combines the call to kobject_init() and kobject_add().
 *
 * If this function returns an error, kobject_put() must be called to
 * properly clean up the memory associated with the object.  This is the
...
 */
int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
			 struct kobject *parent, const char *fmt, ...)

>kobject unbalanced, resulting in a refcount leak and potentially leading
>to a use-after-free.

IIUC, this looks more like wrong kobject lifetime handling and likely a
leak, not a clear UAF :)

>Fix this by using kobject_put(&thpsize->kobj) in the failure path and
>letting thpsize_release() handle the final cleanup.
>
>Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
>Cc: stable@vger.kernel.org
>Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
>---

Apart from that, LGTM.
Reviewed-by: Lance Yang <lance.yang@linux.dev>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-11  6:21 [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path Guangshuo Li
                   ` (2 preceding siblings ...)
  2026-04-11 14:28 ` Lance Yang
@ 2026-04-12  1:37 ` Zi Yan
  3 siblings, 0 replies; 9+ messages in thread
From: Zi Yan @ 2026-04-12  1:37 UTC (permalink / raw)
  To: Guangshuo Li
  Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, Baolin Wang,
	Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain, Barry Song,
	Lance Yang, linux-mm, linux-kernel, stable

On 11 Apr 2026, at 2:21, Guangshuo Li wrote:

> After kobject_init_and_add(), the lifetime of the embedded struct
> kobject is expected to be managed through the kobject core reference
> counting.
>
> In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
> directly with kfree() rather than releasing the kobject reference with
> kobject_put(). This may leave the reference count of the embedded struct
> kobject unbalanced, resulting in a refcount leak and potentially leading
> to a use-after-free.
>
> Fix this by using kobject_put(&thpsize->kobj) in the failure path and
> letting thpsize_release() handle the final cleanup.
>
> Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
>  mm/huge_memory.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
>
LGTM.

Reviewed-by: Zi Yan <ziy@nvidia.com>


--
Best Regards,
Yan, Zi

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-11 14:28 ` Lance Yang
@ 2026-04-12  1:49   ` Zi Yan
  2026-04-12  3:24     ` Lance Yang
  0 siblings, 1 reply; 9+ messages in thread
From: Zi Yan @ 2026-04-12  1:49 UTC (permalink / raw)
  To: Lance Yang
  Cc: lgs201920130244, akpm, david, lorenzo.stoakes, baolin.wang,
	Liam.Howlett, npache, ryan.roberts, dev.jain, baohua, linux-mm,
	linux-kernel, stable

On 11 Apr 2026, at 10:28, Lance Yang wrote:

> On Sat, Apr 11, 2026 at 02:21:52PM +0800, Guangshuo Li wrote:
>> After kobject_init_and_add(), the lifetime of the embedded struct
>> kobject is expected to be managed through the kobject core reference
>> counting.
>>
>> In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
>> directly with kfree() rather than releasing the kobject reference with
>> kobject_put(). This may leave the reference count of the embedded struct
>
> Right. As documented for kobject_init_and_add(), once it has been
> called, the error path should go through kobject_put():
>
> /**
>  * kobject_init_and_add() - Initialize a kobject structure and add it to
>  *                          the kobject hierarchy.
> ...
>  *
>  * This function combines the call to kobject_init() and kobject_add().
>  *
>  * If this function returns an error, kobject_put() must be called to
>  * properly clean up the memory associated with the object.  This is the
> ...
>  */
> int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
> 			 struct kobject *parent, const char *fmt, ...)
>
>> kobject unbalanced, resulting in a refcount leak and potentially leading
>> to a use-after-free.
>
> IIUC, this looks more like wrong kobject lifetime handling and likely a
> leak, not a clear UAF :)

kobject_put() ends up with calling kobj_type->release(), which is just
kfree(to_thpsize(kobj)), equivalent to kfree(thpsize) in the old code.
IIUC, there is no leak. Let me know if I miss anything.

>
>> Fix this by using kobject_put(&thpsize->kobj) in the failure path and
>> letting thpsize_release() handle the final cleanup.
>>
>> Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
>> ---
>
> Apart from that, LGTM.
> Reviewed-by: Lance Yang <lance.yang@linux.dev>


--
Best Regards,
Yan, Zi

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-12  1:49   ` Zi Yan
@ 2026-04-12  3:24     ` Lance Yang
  2026-04-12 13:33       ` Zi Yan
  0 siblings, 1 reply; 9+ messages in thread
From: Lance Yang @ 2026-04-12  3:24 UTC (permalink / raw)
  To: Zi Yan
  Cc: lgs201920130244, akpm, david, lorenzo.stoakes, baolin.wang,
	Liam.Howlett, npache, ryan.roberts, dev.jain, baohua, linux-mm,
	linux-kernel, stable



On 2026/4/12 09:49, Zi Yan wrote:
> On 11 Apr 2026, at 10:28, Lance Yang wrote:
> 
>> On Sat, Apr 11, 2026 at 02:21:52PM +0800, Guangshuo Li wrote:
>>> After kobject_init_and_add(), the lifetime of the embedded struct
>>> kobject is expected to be managed through the kobject core reference
>>> counting.
>>>
>>> In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
>>> directly with kfree() rather than releasing the kobject reference with
>>> kobject_put(). This may leave the reference count of the embedded struct
>>
>> Right. As documented for kobject_init_and_add(), once it has been
>> called, the error path should go through kobject_put():
>>
>> /**
>>   * kobject_init_and_add() - Initialize a kobject structure and add it to
>>   *                          the kobject hierarchy.
>> ...
>>   *
>>   * This function combines the call to kobject_init() and kobject_add().
>>   *
>>   * If this function returns an error, kobject_put() must be called to
>>   * properly clean up the memory associated with the object.  This is the
>> ...
>>   */
>> int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
>> 			 struct kobject *parent, const char *fmt, ...)
>>
>>> kobject unbalanced, resulting in a refcount leak and potentially leading
>>> to a use-after-free.
>>
>> IIUC, this looks more like wrong kobject lifetime handling and likely a
>> leak, not a clear UAF :)
> 
> kobject_put() ends up with calling kobj_type->release(), which is just
> kfree(to_thpsize(kobj)), equivalent to kfree(thpsize) in the old code.
> IIUC, there is no leak. Let me know if I miss anything.

Right, the fix is correct. I was only commenting on the changelog
wording, especially:

"resulting in a refcount leak and potentially leading to a use-after-free"

The old code does skip the required kobject cleanup path, but is
a UAF actually possible there?

Just a wording nit.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path
  2026-04-12  3:24     ` Lance Yang
@ 2026-04-12 13:33       ` Zi Yan
  0 siblings, 0 replies; 9+ messages in thread
From: Zi Yan @ 2026-04-12 13:33 UTC (permalink / raw)
  To: Lance Yang, lgs201920130244
  Cc: akpm, david, lorenzo.stoakes, baolin.wang, Liam.Howlett, npache,
	ryan.roberts, dev.jain, baohua, linux-mm, linux-kernel, stable

On 11 Apr 2026, at 23:24, Lance Yang wrote:

> On 2026/4/12 09:49, Zi Yan wrote:
>> On 11 Apr 2026, at 10:28, Lance Yang wrote:
>>
>>> On Sat, Apr 11, 2026 at 02:21:52PM +0800, Guangshuo Li wrote:
>>>> After kobject_init_and_add(), the lifetime of the embedded struct
>>>> kobject is expected to be managed through the kobject core reference
>>>> counting.
>>>>
>>>> In thpsize_create(), if kobject_init_and_add() fails, thpsize is freed
>>>> directly with kfree() rather than releasing the kobject reference with
>>>> kobject_put(). This may leave the reference count of the embedded struct
>>>
>>> Right. As documented for kobject_init_and_add(), once it has been
>>> called, the error path should go through kobject_put():
>>>
>>> /**
>>>   * kobject_init_and_add() - Initialize a kobject structure and add it to
>>>   *                          the kobject hierarchy.
>>> ...
>>>   *
>>>   * This function combines the call to kobject_init() and kobject_add().
>>>   *
>>>   * If this function returns an error, kobject_put() must be called to
>>>   * properly clean up the memory associated with the object.  This is the
>>> ...
>>>   */
>>> int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
>>> 			 struct kobject *parent, const char *fmt, ...)
>>>
>>>> kobject unbalanced, resulting in a refcount leak and potentially leading
>>>> to a use-after-free.
>>>
>>> IIUC, this looks more like wrong kobject lifetime handling and likely a
>>> leak, not a clear UAF :)
>>
>> kobject_put() ends up with calling kobj_type->release(), which is just
>> kfree(to_thpsize(kobj)), equivalent to kfree(thpsize) in the old code.
>> IIUC, there is no leak. Let me know if I miss anything.
>
> Right, the fix is correct. I was only commenting on the changelog
> wording, especially:
>
> "resulting in a refcount leak and potentially leading to a use-after-free"
>
> The old code does skip the required kobject cleanup path, but is
> a UAF actually possible there?

That is my question too. The original code might not cause any real issue.

Guangshuo, let us know if we get it wrong. Thanks.

>
> Just a wording nit.


--
Best Regards,
Yan, Zi

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-04-12 13:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-11  6:21 [PATCH] mm: thp: Fix refcount leak in thpsize_create() error path Guangshuo Li
2026-04-11  7:45 ` Baolin Wang
2026-04-11  8:32 ` Barry Song
2026-04-11  8:34   ` Barry Song
2026-04-11 14:28 ` Lance Yang
2026-04-12  1:49   ` Zi Yan
2026-04-12  3:24     ` Lance Yang
2026-04-12 13:33       ` Zi Yan
2026-04-12  1:37 ` Zi Yan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox