Skip to contentSkip to conversation

nilfs2: fix memory leak in nilfs_sysfs_create_device_group

PATCH In review Dongliang Mu 2 participants 8 messages Sep 6, 2021 – Sep 13, 2021 NILFS2 filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nilfs2: fix memory leak in nilfs_sysfs_create_device_group

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

---
 fs/nilfs2/sysfs.c | 1 +
 1 file changed, 1 insertion(+)
fs/nilfs2/sysfs.c +1 -0
index 68e8d61e28dd..7ab60711ca76 100644
@@ -1024,6 +1024,7 @@ int nilfs_sysfs_create_device_group(struct super_block *sb)
 
 cleanup_dev_kobject:
 	kobject_del(&nilfs->ns_dev_kobj);
+	kobject_put(&nilfs->ns_dev_kobj);
 
 free_dev_subgroups:
 	kfree(nilfs->ns_dev_subgroups);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 10 lines · Dongliang Mu

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

Even after I add this patch, my local syzkaller still reports this memory leak one hour later. Therefore, there are some other paths or magics which can trigger the memory leak. I need to dig deeper.

Pavel, do you have any idea about this crash report?

BUG: memory leak
unreferenced object 0xffff88804a1a8a60 (size 32):
  comm "syz-executor", pid 14551, jiffies 4294960586 (age 14.780s)
  hex dump (first 32 bytes):
    6c 6f 6f 70 35 00 00 00 00 00 00 00 00 00 00 00  loop5...........
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<ffffffff814750c6>] kstrdup+0x36/0x70
    [<ffffffff81475153>] kstrdup_const+0x53/0x80
    [<ffffffff822773a2>] kvasprintf_const+0xc2/0x110
    [<ffffffff82337c5b>] kobject_set_name_vargs+0x3b/0xe0
    [<ffffffff823385ed>] kobject_init_and_add+0x6d/0xc0
    [<ffffffff81d2bd08>] nilfs_sysfs_create_device_group+0x98/0x3a0
    [<ffffffff81d14fc4>] init_nilfs+0x424/0x580
    [<ffffffff81d02962>] nilfs_mount+0x532/0x8c0
    [<ffffffff815c754b>] legacy_get_tree+0x2b/0x90
    [<ffffffff81565158>] vfs_get_tree+0x28/0x100
    [<ffffffff815a3a82>] path_mount+0xb92/0xfe0
    [<ffffffff815a3f71>] do_mount+0xa1/0xc0
    [<ffffffff815a4584>] __x64_sys_mount+0xf4/0x160
    [<ffffffff8433fd35>] do_syscall_64+0x35/0xb0
    [<ffffffff84400068>] entry_SYSCALL_64_after_hwframe+0x44/0xae
Quoted 21 lines
---
 fs/nilfs2/sysfs.c | 1 +
 1 file changed, 1 insertion(+)
fs/nilfs2/sysfs.c +1 -1
index 68e8d61e28dd..7ab60711ca76 100644
@@ -1024,6 +1024,7 @@ int nilfs_sysfs_create_device_group(struct super_block *sb)
 
 cleanup_dev_kobject:
        kobject_del(&nilfs->ns_dev_kobj);
+       kobject_put(&nilfs->ns_dev_kobj);
 
 free_dev_subgroups:
        kfree(nilfs->ns_dev_subgroups);
--

2.25.1

#2 lore raw
Quoted 32 lines · Dongliang Mu
Quoted 10 lines · Dongliang Mu

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

Even after I add this patch, my local syzkaller still reports this memory leak one hour later. Therefore, there are some other paths or magics which can trigger the memory leak. I need to dig deeper.

Pavel, do you have any idea about this crash report?

BUG: memory leak
unreferenced object 0xffff88804a1a8a60 (size 32):
   comm "syz-executor", pid 14551, jiffies 4294960586 (age 14.780s)
   hex dump (first 32 bytes):
     6c 6f 6f 70 35 00 00 00 00 00 00 00 00 00 00 00  loop5...........
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   backtrace:
     [<ffffffff814750c6>] kstrdup+0x36/0x70
     [<ffffffff81475153>] kstrdup_const+0x53/0x80
     [<ffffffff822773a2>] kvasprintf_const+0xc2/0x110
     [<ffffffff82337c5b>] kobject_set_name_vargs+0x3b/0xe0
     [<ffffffff823385ed>] kobject_init_and_add+0x6d/0xc0
     [<ffffffff81d2bd08>] nilfs_sysfs_create_device_group+0x98/0x3a0
     [<ffffffff81d14fc4>] init_nilfs+0x424/0x580
Quoted 10 lines
     [<ffffffff81d02962>] nilfs_mount+0x532/0x8c0
     [<ffffffff815c754b>] legacy_get_tree+0x2b/0x90
     [<ffffffff81565158>] vfs_get_tree+0x28/0x100
     [<ffffffff815a3a82>] path_mount+0xb92/0xfe0
     [<ffffffff815a3f71>] do_mount+0xa1/0xc0
     [<ffffffff815a4584>] __x64_sys_mount+0xf4/0x160
     [<ffffffff8433fd35>] do_syscall_64+0x35/0xb0
     [<ffffffff84400068>] entry_SYSCALL_64_after_hwframe+0x44/0xae

Hi, Dongliang!

This report says nothing to me... It shows, that there is missing kobject_put() somewhere. I think, we need a reproducer for this leak, otherwise only code review can help :(

With regards,
Pavel Skripkin

#3 lore raw
Quoted 44 lines · Pavel Skripkin
Quoted 32 lines · Dongliang Mu
Quoted 10 lines · Dongliang Mu

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

Even after I add this patch, my local syzkaller still reports this memory leak one hour later. Therefore, there are some other paths or magics which can trigger the memory leak. I need to dig deeper.

Pavel, do you have any idea about this crash report?

BUG: memory leak
unreferenced object 0xffff88804a1a8a60 (size 32):
   comm "syz-executor", pid 14551, jiffies 4294960586 (age 14.780s)
   hex dump (first 32 bytes):
     6c 6f 6f 70 35 00 00 00 00 00 00 00 00 00 00 00  loop5...........
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   backtrace:
     [<ffffffff814750c6>] kstrdup+0x36/0x70
     [<ffffffff81475153>] kstrdup_const+0x53/0x80
     [<ffffffff822773a2>] kvasprintf_const+0xc2/0x110
     [<ffffffff82337c5b>] kobject_set_name_vargs+0x3b/0xe0
     [<ffffffff823385ed>] kobject_init_and_add+0x6d/0xc0
     [<ffffffff81d2bd08>] nilfs_sysfs_create_device_group+0x98/0x3a0
     [<ffffffff81d14fc4>] init_nilfs+0x424/0x580
Quoted 10 lines
     [<ffffffff81d02962>] nilfs_mount+0x532/0x8c0
     [<ffffffff815c754b>] legacy_get_tree+0x2b/0x90
     [<ffffffff81565158>] vfs_get_tree+0x28/0x100
     [<ffffffff815a3a82>] path_mount+0xb92/0xfe0
     [<ffffffff815a3f71>] do_mount+0xa1/0xc0
     [<ffffffff815a4584>] __x64_sys_mount+0xf4/0x160
     [<ffffffff8433fd35>] do_syscall_64+0x35/0xb0
     [<ffffffff84400068>] entry_SYSCALL_64_after_hwframe+0x44/0xae
Quoted 9 lines

Hi, Dongliang!

This report says nothing to me... It shows, that there is missing kobject_put() somewhere. I think, we need a reproducer for this leak, otherwise only code review can help :(

Hm, I guess, I see... We should call kobject_put() in case of kobject_init_and_add() failure:

lib/kobject.c:459

* If this function returns an error, kobject_put() must be called to
* properly clean up the memory associated with the object. This is the

so I suggest:

fs/nilfs2/sysfs.c +1 -0
index 68e8d61e28dd..e8717f4ba2a1 100644
@@ -1026,6 +1026,7 @@ int nilfs_sysfs_create_device_group(struct super_block *sb) 	kobject_del(&nilfs->ns_dev_kobj);
 
 free_dev_subgroups:
+	kobject_put(&nilfs->ns_dev_kobj);
 	kfree(nilfs->ns_dev_subgroups);
 
 failed_create_device_group:
 

This patch should fix 2 memory leaks :)

With regards,
Pavel Skripkin

#4 lore raw
Quoted 80 lines · Pavel Skripkin
Quoted 44 lines · Pavel Skripkin
Quoted 32 lines · Dongliang Mu
Quoted 10 lines · Dongliang Mu

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

Even after I add this patch, my local syzkaller still reports this memory leak one hour later. Therefore, there are some other paths or magics which can trigger the memory leak. I need to dig deeper.

Pavel, do you have any idea about this crash report?

BUG: memory leak
unreferenced object 0xffff88804a1a8a60 (size 32):
   comm "syz-executor", pid 14551, jiffies 4294960586 (age 14.780s)
   hex dump (first 32 bytes):
     6c 6f 6f 70 35 00 00 00 00 00 00 00 00 00 00 00  loop5...........
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   backtrace:
     [<ffffffff814750c6>] kstrdup+0x36/0x70
     [<ffffffff81475153>] kstrdup_const+0x53/0x80
     [<ffffffff822773a2>] kvasprintf_const+0xc2/0x110
     [<ffffffff82337c5b>] kobject_set_name_vargs+0x3b/0xe0
     [<ffffffff823385ed>] kobject_init_and_add+0x6d/0xc0
     [<ffffffff81d2bd08>] nilfs_sysfs_create_device_group+0x98/0x3a0
     [<ffffffff81d14fc4>] init_nilfs+0x424/0x580
Quoted 10 lines
     [<ffffffff81d02962>] nilfs_mount+0x532/0x8c0
     [<ffffffff815c754b>] legacy_get_tree+0x2b/0x90
     [<ffffffff81565158>] vfs_get_tree+0x28/0x100
     [<ffffffff815a3a82>] path_mount+0xb92/0xfe0
     [<ffffffff815a3f71>] do_mount+0xa1/0xc0
     [<ffffffff815a4584>] __x64_sys_mount+0xf4/0x160
     [<ffffffff8433fd35>] do_syscall_64+0x35/0xb0
     [<ffffffff84400068>] entry_SYSCALL_64_after_hwframe+0x44/0xae
Quoted 9 lines

Hi, Dongliang!

This report says nothing to me... It shows, that there is missing kobject_put() somewhere. I think, we need a reproducer for this leak, otherwise only code review can help :(

Hm, I guess, I see... We should call kobject_put() in case of kobject_init_and_add() failure:

lib/kobject.c:459

* If this function returns an error, kobject_put() must be called to
* properly clean up the memory associated with the object. This is the

so I suggest:

fs/nilfs2/sysfs.c +1 -0
index 68e8d61e28dd..e8717f4ba2a1 100644
@@ -1026,6 +1026,7 @@ int nilfs_sysfs_create_device_group(struct
 super_block *sb) 	kobject_del(&nilfs->ns_dev_kobj);
 
   free_dev_subgroups:
+	kobject_put(&nilfs->ns_dev_kobj);
   	kfree(nilfs->ns_dev_subgroups);
 
   failed_create_device_group:
Quoted 3 lines

This patch should fix 2 memory leaks :)

Also, I think, we should add kobject_put() call to

1. nilfs_sysfs_create_##name##_group
2. nilfs_sysfs_create_snapshot_group

To prevent other random kobject memory leaks in nilfs...

With regards,
Pavel Skripkin

#5 lore raw
In reply to Pavel Skripkin
Quoted 81 lines · Pavel Skripkin
Quoted 44 lines · Pavel Skripkin
Quoted 32 lines · Dongliang Mu
Quoted 10 lines · Dongliang Mu

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

Even after I add this patch, my local syzkaller still reports this memory leak one hour later. Therefore, there are some other paths or magics which can trigger the memory leak. I need to dig deeper.

Pavel, do you have any idea about this crash report?

BUG: memory leak
unreferenced object 0xffff88804a1a8a60 (size 32):
   comm "syz-executor", pid 14551, jiffies 4294960586 (age 14.780s)
   hex dump (first 32 bytes):
     6c 6f 6f 70 35 00 00 00 00 00 00 00 00 00 00 00  loop5...........
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   backtrace:
     [<ffffffff814750c6>] kstrdup+0x36/0x70
     [<ffffffff81475153>] kstrdup_const+0x53/0x80
     [<ffffffff822773a2>] kvasprintf_const+0xc2/0x110
     [<ffffffff82337c5b>] kobject_set_name_vargs+0x3b/0xe0
     [<ffffffff823385ed>] kobject_init_and_add+0x6d/0xc0
     [<ffffffff81d2bd08>] nilfs_sysfs_create_device_group+0x98/0x3a0
     [<ffffffff81d14fc4>] init_nilfs+0x424/0x580
Quoted 10 lines
     [<ffffffff81d02962>] nilfs_mount+0x532/0x8c0
     [<ffffffff815c754b>] legacy_get_tree+0x2b/0x90
     [<ffffffff81565158>] vfs_get_tree+0x28/0x100
     [<ffffffff815a3a82>] path_mount+0xb92/0xfe0
     [<ffffffff815a3f71>] do_mount+0xa1/0xc0
     [<ffffffff815a4584>] __x64_sys_mount+0xf4/0x160
     [<ffffffff8433fd35>] do_syscall_64+0x35/0xb0
     [<ffffffff84400068>] entry_SYSCALL_64_after_hwframe+0x44/0xae
Quoted 9 lines

Hi, Dongliang!

This report says nothing to me... It shows, that there is missing kobject_put() somewhere. I think, we need a reproducer for this leak, otherwise only code review can help :(

Hm, I guess, I see... We should call kobject_put() in case of kobject_init_and_add() failure:

lib/kobject.c:459

* If this function returns an error, kobject_put() must be called to
* properly clean up the memory associated with the object. This is the

so I suggest:

fs/nilfs2/sysfs.c +1 -0
index 68e8d61e28dd..e8717f4ba2a1 100644
@@ -1026,6 +1026,7 @@ int nilfs_sysfs_create_device_group(struct
 super_block *sb)        kobject_del(&nilfs->ns_dev_kobj);
 
  free_dev_subgroups:
+       kobject_put(&nilfs->ns_dev_kobj);
        kfree(nilfs->ns_dev_subgroups);
 
  failed_create_device_group:
Quoted 1 line

This patch should fix 2 memory leaks :)

Yes. We should move kobject_put from the label cleanup_dev_kobject to free_dev_subgroups.

However, I tried the syz reproducer just sent to you. The memory leak is still there.

Quoted 6 lines

With regards,
Pavel Skripkin

#6 lore raw

There are many commits related to memory leak fix in nilfs_sysfs.

4 days nilfs2: fix memory leak in nilfs_sysfs_delete_snapshot_group 4 days nilfs2: fix memory leak in nilfs_sysfs_create_snapshot_group 4 days nilfs2: fix memory leak in nilfs_sysfs_delete_##name##_group 4 days nilfs2: fix memory leak in nilfs_sysfs_create_##name##_group 4 days nilfs2: fix NULL pointer in nilfs_##name##_attr_release 4 days nilfs2: fix memory leak in nilfs_sysfs_create_device_group

This thread can be closed.

Best regards,
Dongliang Mu

Quoted 97 lines · Dongliang Mu
Quoted 81 lines · Pavel Skripkin
Quoted 44 lines · Pavel Skripkin
Quoted 32 lines · Dongliang Mu
Quoted 10 lines · Dongliang Mu

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

Even after I add this patch, my local syzkaller still reports this memory leak one hour later. Therefore, there are some other paths or magics which can trigger the memory leak. I need to dig deeper.

Pavel, do you have any idea about this crash report?

BUG: memory leak
unreferenced object 0xffff88804a1a8a60 (size 32):
   comm "syz-executor", pid 14551, jiffies 4294960586 (age 14.780s)
   hex dump (first 32 bytes):
     6c 6f 6f 70 35 00 00 00 00 00 00 00 00 00 00 00  loop5...........
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   backtrace:
     [<ffffffff814750c6>] kstrdup+0x36/0x70
     [<ffffffff81475153>] kstrdup_const+0x53/0x80
     [<ffffffff822773a2>] kvasprintf_const+0xc2/0x110
     [<ffffffff82337c5b>] kobject_set_name_vargs+0x3b/0xe0
     [<ffffffff823385ed>] kobject_init_and_add+0x6d/0xc0
     [<ffffffff81d2bd08>] nilfs_sysfs_create_device_group+0x98/0x3a0
     [<ffffffff81d14fc4>] init_nilfs+0x424/0x580
Quoted 10 lines
     [<ffffffff81d02962>] nilfs_mount+0x532/0x8c0
     [<ffffffff815c754b>] legacy_get_tree+0x2b/0x90
     [<ffffffff81565158>] vfs_get_tree+0x28/0x100
     [<ffffffff815a3a82>] path_mount+0xb92/0xfe0
     [<ffffffff815a3f71>] do_mount+0xa1/0xc0
     [<ffffffff815a4584>] __x64_sys_mount+0xf4/0x160
     [<ffffffff8433fd35>] do_syscall_64+0x35/0xb0
     [<ffffffff84400068>] entry_SYSCALL_64_after_hwframe+0x44/0xae
Quoted 9 lines

Hi, Dongliang!

This report says nothing to me... It shows, that there is missing kobject_put() somewhere. I think, we need a reproducer for this leak, otherwise only code review can help :(

Hm, I guess, I see... We should call kobject_put() in case of kobject_init_and_add() failure:

lib/kobject.c:459

* If this function returns an error, kobject_put() must be called to
* properly clean up the memory associated with the object. This is the

so I suggest:

fs/nilfs2/sysfs.c +1 -0
index 68e8d61e28dd..e8717f4ba2a1 100644
@@ -1026,6 +1026,7 @@ int nilfs_sysfs_create_device_group(struct
 super_block *sb)        kobject_del(&nilfs->ns_dev_kobj);
 
  free_dev_subgroups:
+       kobject_put(&nilfs->ns_dev_kobj);
        kfree(nilfs->ns_dev_subgroups);
 
  failed_create_device_group:
Quoted 1 line

This patch should fix 2 memory leaks :)

Yes. We should move kobject_put from the label cleanup_dev_kobject to free_dev_subgroups.

However, I tried the syz reproducer just sent to you. The memory leak is still there.

Quoted 6 lines

With regards,
Pavel Skripkin

#7 lore raw
In reply to Pavel Skripkin
Quoted 45 lines · Pavel Skripkin
Quoted 32 lines · Dongliang Mu
Quoted 10 lines · Dongliang Mu

The commit 8fd0c1b0647a ("nilfs2: fix memory leak in nilfs_sysfs_delete_device_group") adds a kobject_put to free the leaking object name. However, it is incomplete to only add kobject_put in the nilfs_sysfs_delete_device_group. The function nilfs_sysfs_create_device_group also needs the kobject_put to free the object name in the error handling part.

Fix this by adding kobject_put in the error handling code of nilfs_sysfs_create_device_group.

Even after I add this patch, my local syzkaller still reports this memory leak one hour later. Therefore, there are some other paths or magics which can trigger the memory leak. I need to dig deeper.

Pavel, do you have any idea about this crash report?

BUG: memory leak
unreferenced object 0xffff88804a1a8a60 (size 32):
   comm "syz-executor", pid 14551, jiffies 4294960586 (age 14.780s)
   hex dump (first 32 bytes):
     6c 6f 6f 70 35 00 00 00 00 00 00 00 00 00 00 00  loop5...........
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   backtrace:
     [<ffffffff814750c6>] kstrdup+0x36/0x70
     [<ffffffff81475153>] kstrdup_const+0x53/0x80
     [<ffffffff822773a2>] kvasprintf_const+0xc2/0x110
     [<ffffffff82337c5b>] kobject_set_name_vargs+0x3b/0xe0
     [<ffffffff823385ed>] kobject_init_and_add+0x6d/0xc0
     [<ffffffff81d2bd08>] nilfs_sysfs_create_device_group+0x98/0x3a0
     [<ffffffff81d14fc4>] init_nilfs+0x424/0x580
Quoted 10 lines
     [<ffffffff81d02962>] nilfs_mount+0x532/0x8c0
     [<ffffffff815c754b>] legacy_get_tree+0x2b/0x90
     [<ffffffff81565158>] vfs_get_tree+0x28/0x100
     [<ffffffff815a3a82>] path_mount+0xb92/0xfe0
     [<ffffffff815a3f71>] do_mount+0xa1/0xc0
     [<ffffffff815a4584>] __x64_sys_mount+0xf4/0x160
     [<ffffffff8433fd35>] do_syscall_64+0x35/0xb0
     [<ffffffff84400068>] entry_SYSCALL_64_after_hwframe+0x44/0xae
Quoted 6 lines

Hi, Dongliang!

This report says nothing to me... It shows, that there is missing kobject_put() somewhere. I think, we need a reproducer for this leak, otherwise only code review can help :(

I have attached a syz reproducer to this email. Even if the kernel is patched with your patch and my patch, it still triggers a memory leak.

Quoted 5 lines

With regards,
Pavel Skripkin