linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ubifs: Fix memory leak in alloc_wbufs()
@ 2022-10-22 11:52 Li Zetao
  2022-10-24  1:31 ` Zhihao Cheng
  0 siblings, 1 reply; 2+ messages in thread
From: Li Zetao @ 2022-10-22 11:52 UTC (permalink / raw)
  To: richard, Artem.Bityutskiy, ext-adrian.hunter
  Cc: lizetao1, yi.zhang, chengzhihao1, linux-mtd, linux-kernel

kmemleak reported a sequence of memory leaks, and show them as following:

  unreferenced object 0xffff8881575f8400 (size 1024):
    comm "mount", pid 19625, jiffies 4297119604 (age 20.383s)
    hex dump (first 32 bytes):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    backtrace:
      [<ffffffff8176cecd>] __kmalloc+0x4d/0x150
      [<ffffffffa0406b2b>] ubifs_mount+0x307b/0x7170 [ubifs]
      [<ffffffff819fa8fd>] legacy_get_tree+0xed/0x1d0
      [<ffffffff81936f2d>] vfs_get_tree+0x7d/0x230
      [<ffffffff819b2bd4>] path_mount+0xdd4/0x17b0
      [<ffffffff819b37aa>] __x64_sys_mount+0x1fa/0x270
      [<ffffffff83c14295>] do_syscall_64+0x35/0x80
      [<ffffffff83e0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0

  unreferenced object 0xffff8881798a6e00 (size 512):
    comm "mount", pid 19677, jiffies 4297121912 (age 37.816s)
    hex dump (first 32 bytes):
      6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  kkkkkkkkkkkkkkkk
      6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  kkkkkkkkkkkkkkkk
    backtrace:
      [<ffffffff8176cecd>] __kmalloc+0x4d/0x150
      [<ffffffffa0418342>] ubifs_wbuf_init+0x52/0x480 [ubifs]
      [<ffffffffa0406ca5>] ubifs_mount+0x31f5/0x7170 [ubifs]
      [<ffffffff819fa8fd>] legacy_get_tree+0xed/0x1d0
      [<ffffffff81936f2d>] vfs_get_tree+0x7d/0x230
      [<ffffffff819b2bd4>] path_mount+0xdd4/0x17b0
      [<ffffffff819b37aa>] __x64_sys_mount+0x1fa/0x270
      [<ffffffff83c14295>] do_syscall_64+0x35/0x80
      [<ffffffff83e0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0

The problem is that the ubifs_wbuf_init() returns an error in the
loop which in the alloc_wbufs(), then the wbuf->buf and wbuf->inodes
that were successfully alloced before are not freed.

Fix it by adding error hanging path in alloc_wbufs() which frees
the memory alloced before when ubifs_wbuf_init() returns an error.

Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
 fs/ubifs/super.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index d0c9a09988bc..32cb14759796 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -833,7 +833,7 @@ static int alloc_wbufs(struct ubifs_info *c)
 		INIT_LIST_HEAD(&c->jheads[i].buds_list);
 		err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
 		if (err)
-			return err;
+			goto out_wbuf;
 
 		c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
 		c->jheads[i].wbuf.jhead = i;
@@ -841,7 +841,7 @@ static int alloc_wbufs(struct ubifs_info *c)
 		c->jheads[i].log_hash = ubifs_hash_get_desc(c);
 		if (IS_ERR(c->jheads[i].log_hash)) {
 			err = PTR_ERR(c->jheads[i].log_hash);
-			goto out;
+			goto out_log_hash;
 		}
 	}
 
@@ -854,9 +854,18 @@ static int alloc_wbufs(struct ubifs_info *c)
 
 	return 0;
 
-out:
-	while (i--)
+out_log_hash:
+	kfree(c->jheads[i].wbuf.buf);
+	kfree(c->jheads[i].wbuf.inodes);
+
+out_wbuf:
+	while (i--) {
+		kfree(c->jheads[i].wbuf.buf);
+		kfree(c->jheads[i].wbuf.inodes);
 		kfree(c->jheads[i].log_hash);
+	}
+	kfree(c->jheads);
+	c->jheads = NULL;
 
 	return err;
 }
-- 
2.31.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH] ubifs: Fix memory leak in alloc_wbufs()
  2022-10-22 11:52 [PATCH] ubifs: Fix memory leak in alloc_wbufs() Li Zetao
@ 2022-10-24  1:31 ` Zhihao Cheng
  0 siblings, 0 replies; 2+ messages in thread
From: Zhihao Cheng @ 2022-10-24  1:31 UTC (permalink / raw)
  To: Li Zetao, richard, Artem.Bityutskiy, ext-adrian.hunter
  Cc: yi.zhang, linux-mtd, linux-kernel

在 2022/10/22 19:52, Li Zetao 写道:
> kmemleak reported a sequence of memory leaks, and show them as following:
> 
>    unreferenced object 0xffff8881575f8400 (size 1024):
>      comm "mount", pid 19625, jiffies 4297119604 (age 20.383s)
>      hex dump (first 32 bytes):
>        00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>        00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>      backtrace:
>        [<ffffffff8176cecd>] __kmalloc+0x4d/0x150
>        [<ffffffffa0406b2b>] ubifs_mount+0x307b/0x7170 [ubifs]
>        [<ffffffff819fa8fd>] legacy_get_tree+0xed/0x1d0
>        [<ffffffff81936f2d>] vfs_get_tree+0x7d/0x230
>        [<ffffffff819b2bd4>] path_mount+0xdd4/0x17b0
>        [<ffffffff819b37aa>] __x64_sys_mount+0x1fa/0x270
>        [<ffffffff83c14295>] do_syscall_64+0x35/0x80
>        [<ffffffff83e0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0
> 
>    unreferenced object 0xffff8881798a6e00 (size 512):
>      comm "mount", pid 19677, jiffies 4297121912 (age 37.816s)
>      hex dump (first 32 bytes):
>        6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  kkkkkkkkkkkkkkkk
>        6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b  kkkkkkkkkkkkkkkk
>      backtrace:
>        [<ffffffff8176cecd>] __kmalloc+0x4d/0x150
>        [<ffffffffa0418342>] ubifs_wbuf_init+0x52/0x480 [ubifs]
>        [<ffffffffa0406ca5>] ubifs_mount+0x31f5/0x7170 [ubifs]
>        [<ffffffff819fa8fd>] legacy_get_tree+0xed/0x1d0
>        [<ffffffff81936f2d>] vfs_get_tree+0x7d/0x230
>        [<ffffffff819b2bd4>] path_mount+0xdd4/0x17b0
>        [<ffffffff819b37aa>] __x64_sys_mount+0x1fa/0x270
>        [<ffffffff83c14295>] do_syscall_64+0x35/0x80
>        [<ffffffff83e0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0
> 
> The problem is that the ubifs_wbuf_init() returns an error in the
> loop which in the alloc_wbufs(), then the wbuf->buf and wbuf->inodes
> that were successfully alloced before are not freed.
> 
> Fix it by adding error hanging path in alloc_wbufs() which frees
> the memory alloced before when ubifs_wbuf_init() returns an error.
> 
> Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
>   fs/ubifs/super.c | 17 +++++++++++++----
>   1 file changed, 13 insertions(+), 4 deletions(-)

Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

end of thread, other threads:[~2022-10-24  1:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 11:52 [PATCH] ubifs: Fix memory leak in alloc_wbufs() Li Zetao
2022-10-24  1:31 ` Zhihao Cheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).