]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ubifs: dirty_cow_znode: Fix memleak in error handling path
authorZhihao Cheng <chengzhihao1@huawei.com>
Fri, 18 Nov 2022 09:02:36 +0000 (17:02 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 11 Mar 2023 15:26:51 +0000 (16:26 +0100)
[ Upstream commit 122deabfe1428bffe95e2bf364ff8a5059bdf089 ]

Following process will cause a memleak for copied up znode:

dirty_cow_znode
  zn = copy_znode(c, znode);
  err = insert_old_idx(c, zbr->lnum, zbr->offs);
  if (unlikely(err))
     return ERR_PTR(err);   // No one refers to zn.

Fix it by adding copied znode back to tnc, then it will be freed
by ubifs_destroy_tnc_subtree() while closing tnc.

Fetch a reproducer in [Link].

Link: https://bugzilla.kernel.org/show_bug.cgi?id=216705
Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Richard Weinberger <richard@nod.at>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/ubifs/tnc.c

index 20b70e178c4faf1e34de8e2338453538606b624d..6c4af1cfce34697b16790c3f0acd8a31776c2072 100644 (file)
@@ -279,11 +279,18 @@ static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
        if (zbr->len) {
                err = insert_old_idx(c, zbr->lnum, zbr->offs);
                if (unlikely(err))
-                       return ERR_PTR(err);
+                       /*
+                        * Obsolete znodes will be freed by tnc_destroy_cnext()
+                        * or free_obsolete_znodes(), copied up znodes should
+                        * be added back to tnc and freed by
+                        * ubifs_destroy_tnc_subtree().
+                        */
+                       goto out;
                err = add_idx_dirt(c, zbr->lnum, zbr->len);
        } else
                err = 0;
 
+out:
        zbr->znode = zn;
        zbr->lnum = 0;
        zbr->offs = 0;