All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] erofs: fix possible memory leak in erofs_init_sysfs()
@ 2022-10-18  7:39 Yang Yingliang via Linux-erofs
  2022-10-19  2:21 ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Yang Yingliang via Linux-erofs @ 2022-10-18  7:39 UTC (permalink / raw)
  To: linux-erofs; +Cc: yangyingliang, hsiangkao, huangjianan

Inject fault while probing 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. Fix
this by calling kset_put(), so that name can be freed in
callback function kobject_cleanup().

unreferenced object 0xffff888101d228c0 (size 8):
  comm "modprobe", pid 276, jiffies 4294722700 (age 13.151s)
  hex dump (first 8 bytes):
    65 72 6f 66 73 00 ff ff                          erofs...
  backtrace:
    [<00000000e2a9a4a6>] __kmalloc_node_track_caller+0x44/0x1b0
    [<00000000b8ce02de>] kstrdup+0x3a/0x70
    [<000000004a0e01d2>] kstrdup_const+0x63/0x80
    [<00000000051b6cda>] kvasprintf_const+0x149/0x180
    [<000000004dc51dad>] kobject_set_name_vargs+0x56/0x150
    [<00000000b30f0bad>] kobject_set_name+0xab/0xe0

Fixes: 168e9a76200c ("erofs: add sysfs interface")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 fs/erofs/sysfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/erofs/sysfs.c b/fs/erofs/sysfs.c
index 783bb7b21b51..653b35001bc5 100644
--- a/fs/erofs/sysfs.c
+++ b/fs/erofs/sysfs.c
@@ -254,8 +254,10 @@ int __init erofs_init_sysfs(void)
 	kobject_set_name(&erofs_root.kobj, "erofs");
 	erofs_root.kobj.parent = fs_kobj;
 	ret = kset_register(&erofs_root);
-	if (ret)
+	if (ret) {
+		kset_put(&erofs_root);
 		goto root_err;
+	}
 
 	ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
 				   NULL, "features");
-- 
2.25.1


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

* Re: [PATCH] erofs: fix possible memory leak in erofs_init_sysfs()
  2022-10-18  7:39 [PATCH] erofs: fix possible memory leak in erofs_init_sysfs() Yang Yingliang via Linux-erofs
@ 2022-10-19  2:21 ` Gao Xiang
  2022-10-20  8:49   ` Chao Yu
  0 siblings, 1 reply; 5+ messages in thread
From: Gao Xiang @ 2022-10-19  2:21 UTC (permalink / raw)
  To: Yang Yingliang; +Cc: gregkh, huangjianan, Joseph Qi, linux-erofs

Hi Yingliang,

On Tue, Oct 18, 2022 at 03:39:47PM +0800, Yang Yingliang wrote:
> Inject fault while probing 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. Fix
> this by calling kset_put(), so that name can be freed in
> callback function kobject_cleanup().
> 
> unreferenced object 0xffff888101d228c0 (size 8):
>   comm "modprobe", pid 276, jiffies 4294722700 (age 13.151s)
>   hex dump (first 8 bytes):
>     65 72 6f 66 73 00 ff ff                          erofs...
>   backtrace:
>     [<00000000e2a9a4a6>] __kmalloc_node_track_caller+0x44/0x1b0
>     [<00000000b8ce02de>] kstrdup+0x3a/0x70
>     [<000000004a0e01d2>] kstrdup_const+0x63/0x80
>     [<00000000051b6cda>] kvasprintf_const+0x149/0x180
>     [<000000004dc51dad>] kobject_set_name_vargs+0x56/0x150
>     [<00000000b30f0bad>] kobject_set_name+0xab/0xe0
> 
> Fixes: 168e9a76200c ("erofs: add sysfs interface")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>

Thanks for the catch, the issue itself seems true.  However, I found
several fses all have this issue.

After I did some discussion with Joseph Qi offline, I'd like to wait
for the conclusion of the following thread:
https://lore.kernel.org/r/bf27f347-5ced-98e5-f188-659cc2a9736f@linux.alibaba.com

Since I tend to think kset_put() should be one fail step of
kset_register().

Thanks,
Gao Xiang

> ---
>  fs/erofs/sysfs.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/erofs/sysfs.c b/fs/erofs/sysfs.c
> index 783bb7b21b51..653b35001bc5 100644
> --- a/fs/erofs/sysfs.c
> +++ b/fs/erofs/sysfs.c
> @@ -254,8 +254,10 @@ int __init erofs_init_sysfs(void)
>  	kobject_set_name(&erofs_root.kobj, "erofs");
>  	erofs_root.kobj.parent = fs_kobj;
>  	ret = kset_register(&erofs_root);
> -	if (ret)
> +	if (ret) {
> +		kset_put(&erofs_root);
>  		goto root_err;
> +	}
>  
>  	ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
>  				   NULL, "features");
> -- 
> 2.25.1

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

* Re: [PATCH] erofs: fix possible memory leak in erofs_init_sysfs()
  2022-10-19  2:21 ` Gao Xiang
@ 2022-10-20  8:49   ` Chao Yu
  2022-10-20  9:14     ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Chao Yu @ 2022-10-20  8:49 UTC (permalink / raw)
  To: Gao Xiang, Yang Yingliang; +Cc: Joseph Qi, gregkh, linux-erofs, huangjianan

On 2022/10/19 10:21, Gao Xiang wrote:
> Hi Yingliang,
> 
> On Tue, Oct 18, 2022 at 03:39:47PM +0800, Yang Yingliang wrote:
>> Inject fault while probing 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. Fix
>> this by calling kset_put(), so that name can be freed in
>> callback function kobject_cleanup().
>>
>> unreferenced object 0xffff888101d228c0 (size 8):
>>    comm "modprobe", pid 276, jiffies 4294722700 (age 13.151s)
>>    hex dump (first 8 bytes):
>>      65 72 6f 66 73 00 ff ff                          erofs...
>>    backtrace:
>>      [<00000000e2a9a4a6>] __kmalloc_node_track_caller+0x44/0x1b0
>>      [<00000000b8ce02de>] kstrdup+0x3a/0x70
>>      [<000000004a0e01d2>] kstrdup_const+0x63/0x80
>>      [<00000000051b6cda>] kvasprintf_const+0x149/0x180
>>      [<000000004dc51dad>] kobject_set_name_vargs+0x56/0x150
>>      [<00000000b30f0bad>] kobject_set_name+0xab/0xe0
>>
>> Fixes: 168e9a76200c ("erofs: add sysfs interface")
>> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> 
> Thanks for the catch, the issue itself seems true.  However, I found
> several fses all have this issue.
> 
> After I did some discussion with Joseph Qi offline, I'd like to wait
> for the conclusion of the following thread:
> https://lore.kernel.org/r/bf27f347-5ced-98e5-f188-659cc2a9736f@linux.alibaba.com
> 
> Since I tend to think kset_put() should be one fail step of
> kset_register().

I guess in error path, it missed to free kobj->name which was allocated by
kobject_set_name(&erofs_root.kobj, "erofs"), so calling kset_put() here looks
fine?

Thanks,

> 
> Thanks,
> Gao Xiang
> 
>> ---
>>   fs/erofs/sysfs.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/erofs/sysfs.c b/fs/erofs/sysfs.c
>> index 783bb7b21b51..653b35001bc5 100644
>> --- a/fs/erofs/sysfs.c
>> +++ b/fs/erofs/sysfs.c
>> @@ -254,8 +254,10 @@ int __init erofs_init_sysfs(void)
>>   	kobject_set_name(&erofs_root.kobj, "erofs");
>>   	erofs_root.kobj.parent = fs_kobj;
>>   	ret = kset_register(&erofs_root);
>> -	if (ret)
>> +	if (ret) {
>> +		kset_put(&erofs_root);
>>   		goto root_err;
>> +	}
>>   
>>   	ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
>>   				   NULL, "features");
>> -- 
>> 2.25.1

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

* Re: [PATCH] erofs: fix possible memory leak in erofs_init_sysfs()
  2022-10-20  8:49   ` Chao Yu
@ 2022-10-20  9:14     ` Gao Xiang
  2022-10-25 10:02       ` Chao Yu
  0 siblings, 1 reply; 5+ messages in thread
From: Gao Xiang @ 2022-10-20  9:14 UTC (permalink / raw)
  To: Chao Yu; +Cc: gregkh, huangjianan, Joseph Qi, Yang Yingliang, linux-erofs

Hi Chao,

On Thu, Oct 20, 2022 at 04:49:34PM +0800, Chao Yu wrote:
> On 2022/10/19 10:21, Gao Xiang wrote:
> > Hi Yingliang,
> > 
> > On Tue, Oct 18, 2022 at 03:39:47PM +0800, Yang Yingliang wrote:
> > > Inject fault while probing 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. Fix
> > > this by calling kset_put(), so that name can be freed in
> > > callback function kobject_cleanup().
> > > 
> > > unreferenced object 0xffff888101d228c0 (size 8):
> > >    comm "modprobe", pid 276, jiffies 4294722700 (age 13.151s)
> > >    hex dump (first 8 bytes):
> > >      65 72 6f 66 73 00 ff ff                          erofs...
> > >    backtrace:
> > >      [<00000000e2a9a4a6>] __kmalloc_node_track_caller+0x44/0x1b0
> > >      [<00000000b8ce02de>] kstrdup+0x3a/0x70
> > >      [<000000004a0e01d2>] kstrdup_const+0x63/0x80
> > >      [<00000000051b6cda>] kvasprintf_const+0x149/0x180
> > >      [<000000004dc51dad>] kobject_set_name_vargs+0x56/0x150
> > >      [<00000000b30f0bad>] kobject_set_name+0xab/0xe0
> > > 
> > > Fixes: 168e9a76200c ("erofs: add sysfs interface")
> > > Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> > 
> > Thanks for the catch, the issue itself seems true.  However, I found
> > several fses all have this issue.
> > 
> > After I did some discussion with Joseph Qi offline, I'd like to wait
> > for the conclusion of the following thread:
> > https://lore.kernel.org/r/bf27f347-5ced-98e5-f188-659cc2a9736f@linux.alibaba.com
> > 
> > Since I tend to think kset_put() should be one fail step of
> > kset_register().
> 
> I guess in error path, it missed to free kobj->name which was allocated by
> kobject_set_name(&erofs_root.kobj, "erofs"), so calling kset_put() here looks
> fine?

We looked into this yesterday.  I think the main problem is that the
kset_put() a reference which is set in kset_init() of kset_register().

Therefore such refernece belongs to an internal implementation and
should not be treated for each kset_register() caller.

Please correct me if I'm wrong here.

Thanks,
Gao Xiang

> 
> Thanks,
> 
> > 
> > Thanks,
> > Gao Xiang
> > 
> > > ---
> > >   fs/erofs/sysfs.c | 4 +++-
> > >   1 file changed, 3 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/fs/erofs/sysfs.c b/fs/erofs/sysfs.c
> > > index 783bb7b21b51..653b35001bc5 100644
> > > --- a/fs/erofs/sysfs.c
> > > +++ b/fs/erofs/sysfs.c
> > > @@ -254,8 +254,10 @@ int __init erofs_init_sysfs(void)
> > >   	kobject_set_name(&erofs_root.kobj, "erofs");
> > >   	erofs_root.kobj.parent = fs_kobj;
> > >   	ret = kset_register(&erofs_root);
> > > -	if (ret)
> > > +	if (ret) {
> > > +		kset_put(&erofs_root);
> > >   		goto root_err;
> > > +	}
> > >   	ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
> > >   				   NULL, "features");
> > > -- 
> > > 2.25.1

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

* Re: [PATCH] erofs: fix possible memory leak in erofs_init_sysfs()
  2022-10-20  9:14     ` Gao Xiang
@ 2022-10-25 10:02       ` Chao Yu
  0 siblings, 0 replies; 5+ messages in thread
From: Chao Yu @ 2022-10-25 10:02 UTC (permalink / raw)
  To: Gao Xiang; +Cc: Joseph Qi, gregkh, linux-erofs, Yang Yingliang

On 2022/10/20 17:14, Gao Xiang wrote:
> Hi Chao,
> 
> On Thu, Oct 20, 2022 at 04:49:34PM +0800, Chao Yu wrote:
>> On 2022/10/19 10:21, Gao Xiang wrote:
>>> Hi Yingliang,
>>>
>>> On Tue, Oct 18, 2022 at 03:39:47PM +0800, Yang Yingliang wrote:
>>>> Inject fault while probing 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. Fix
>>>> this by calling kset_put(), so that name can be freed in
>>>> callback function kobject_cleanup().
>>>>
>>>> unreferenced object 0xffff888101d228c0 (size 8):
>>>>     comm "modprobe", pid 276, jiffies 4294722700 (age 13.151s)
>>>>     hex dump (first 8 bytes):
>>>>       65 72 6f 66 73 00 ff ff                          erofs...
>>>>     backtrace:
>>>>       [<00000000e2a9a4a6>] __kmalloc_node_track_caller+0x44/0x1b0
>>>>       [<00000000b8ce02de>] kstrdup+0x3a/0x70
>>>>       [<000000004a0e01d2>] kstrdup_const+0x63/0x80
>>>>       [<00000000051b6cda>] kvasprintf_const+0x149/0x180
>>>>       [<000000004dc51dad>] kobject_set_name_vargs+0x56/0x150
>>>>       [<00000000b30f0bad>] kobject_set_name+0xab/0xe0
>>>>
>>>> Fixes: 168e9a76200c ("erofs: add sysfs interface")
>>>> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
>>>
>>> Thanks for the catch, the issue itself seems true.  However, I found
>>> several fses all have this issue.
>>>
>>> After I did some discussion with Joseph Qi offline, I'd like to wait
>>> for the conclusion of the following thread:
>>> https://lore.kernel.org/r/bf27f347-5ced-98e5-f188-659cc2a9736f@linux.alibaba.com
>>>
>>> Since I tend to think kset_put() should be one fail step of
>>> kset_register().
>>
>> I guess in error path, it missed to free kobj->name which was allocated by
>> kobject_set_name(&erofs_root.kobj, "erofs"), so calling kset_put() here looks
>> fine?
> 
> We looked into this yesterday.  I think the main problem is that the
> kset_put() a reference which is set in kset_init() of kset_register().
> 
> Therefore such refernece belongs to an internal implementation and
> should not be treated for each kset_register() caller.
> 
> Please correct me if I'm wrong here.

Xiang,

Oh, right, I missed reference count operation in kset_put().

Thanks,

> 
> Thanks,
> Gao Xiang
> 
>>
>> Thanks,
>>
>>>
>>> Thanks,
>>> Gao Xiang
>>>
>>>> ---
>>>>    fs/erofs/sysfs.c | 4 +++-
>>>>    1 file changed, 3 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/fs/erofs/sysfs.c b/fs/erofs/sysfs.c
>>>> index 783bb7b21b51..653b35001bc5 100644
>>>> --- a/fs/erofs/sysfs.c
>>>> +++ b/fs/erofs/sysfs.c
>>>> @@ -254,8 +254,10 @@ int __init erofs_init_sysfs(void)
>>>>    	kobject_set_name(&erofs_root.kobj, "erofs");
>>>>    	erofs_root.kobj.parent = fs_kobj;
>>>>    	ret = kset_register(&erofs_root);
>>>> -	if (ret)
>>>> +	if (ret) {
>>>> +		kset_put(&erofs_root);
>>>>    		goto root_err;
>>>> +	}
>>>>    	ret = kobject_init_and_add(&erofs_feat, &erofs_feat_ktype,
>>>>    				   NULL, "features");
>>>> -- 
>>>> 2.25.1

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

end of thread, other threads:[~2022-10-25 10:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-18  7:39 [PATCH] erofs: fix possible memory leak in erofs_init_sysfs() Yang Yingliang via Linux-erofs
2022-10-19  2:21 ` Gao Xiang
2022-10-20  8:49   ` Chao Yu
2022-10-20  9:14     ` Gao Xiang
2022-10-25 10:02       ` Chao Yu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.