* [PATCH v3 0/2] ubifs: Fix two kmemleaks in error path
@ 2024-01-08 2:41 Zhihao Cheng
2024-01-08 2:41 ` [PATCH v3 1/2] ubifs: dbg_check_idx_size: Fix kmemleak if loading znode failed Zhihao Cheng
2024-01-08 2:41 ` [PATCH v3 2/2] ubifs: ubifs_symlink: Fix memleak of inode->i_link in error path Zhihao Cheng
0 siblings, 2 replies; 3+ messages in thread
From: Zhihao Cheng @ 2024-01-08 2:41 UTC (permalink / raw)
To: richard, ebiggers, terrelln; +Cc: linux-fscrypt, linux-mtd
First memleak is found by mounting corrupted UBIFS image with chk_index
enabled.
Second memleak is found by powercut testing for encryption scenario.
v1->v2:
ubifs_symlink: Call fscrypt_free_inode() directly in error handling path.
Add 'Cc: stable@vger.kernel.org' and 'Suggested-by' tags in patch 2.
v2->v3:
Handle tnc releasing in error handling path in dbg_check_idx_size().
Zhihao Cheng (2):
ubifs: dbg_check_idx_size: Fix kmemleak if loading znode failed
ubifs: ubifs_symlink: Fix memleak of inode->i_link in error path
fs/ubifs/debug.c | 9 +++++++--
fs/ubifs/dir.c | 2 ++
fs/ubifs/tnc.c | 9 +--------
fs/ubifs/tnc_misc.c | 22 ++++++++++++++++++++++
fs/ubifs/ubifs.h | 1 +
5 files changed, 33 insertions(+), 10 deletions(-)
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 1/2] ubifs: dbg_check_idx_size: Fix kmemleak if loading znode failed
2024-01-08 2:41 [PATCH v3 0/2] ubifs: Fix two kmemleaks in error path Zhihao Cheng
@ 2024-01-08 2:41 ` Zhihao Cheng
2024-01-08 2:41 ` [PATCH v3 2/2] ubifs: ubifs_symlink: Fix memleak of inode->i_link in error path Zhihao Cheng
1 sibling, 0 replies; 3+ messages in thread
From: Zhihao Cheng @ 2024-01-08 2:41 UTC (permalink / raw)
To: richard, ebiggers, terrelln; +Cc: linux-fscrypt, linux-mtd
If function dbg_check_idx_size() failed by loading znode in mounting
process, there are two problems:
1. Allocated znodes won't be freed, which causes kmemleak in kernel:
ubifs_mount
dbg_check_idx_size
dbg_walk_index
c->zroot.znode = ubifs_load_znode
child = ubifs_load_znode // failed
// Loaded znodes won't be freed in error handling path.
2. Global variable ubifs_clean_zn_cnt is not decreased, because
ubifs_tnc_close() is not invoked in error handling path, which
triggers a warning in ubifs_exit():
WARNING: CPU: 1 PID: 1576 at fs/ubifs/super.c:2486 ubifs_exit
Modules linked in: zstd ubifs(-) ubi nandsim
CPU: 1 PID: 1576 Comm: rmmod Not tainted 6.7.0-rc6
Call Trace:
ubifs_exit+0xca/0xc70 [ubifs]
__do_sys_delete_module+0x29a/0x4a0
do_syscall_64+0x6f/0x140
Fix it by adding error handling path in dbg_check_idx_size() to release
tnc tree.
Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Suggested-by: Richard Weinberger <richard@nod.at>
---
fs/ubifs/debug.c | 9 +++++++--
fs/ubifs/tnc.c | 9 +--------
fs/ubifs/tnc_misc.c | 22 ++++++++++++++++++++++
fs/ubifs/ubifs.h | 1 +
4 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
index d013c5b3f1ed..ac77ac1fd73e 100644
--- a/fs/ubifs/debug.c
+++ b/fs/ubifs/debug.c
@@ -1742,17 +1742,22 @@ int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
err = dbg_walk_index(c, NULL, add_size, &calc);
if (err) {
ubifs_err(c, "error %d while walking the index", err);
- return err;
+ goto out_err;
}
if (calc != idx_size) {
ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
calc, idx_size);
dump_stack();
- return -EINVAL;
+ err = -EINVAL;
+ goto out_err;
}
return 0;
+
+out_err:
+ ubifs_destroy_tnc_tree(c);
+ return err;
}
/**
diff --git a/fs/ubifs/tnc.c b/fs/ubifs/tnc.c
index 7b7d75ed3ec7..0fabecd9d379 100644
--- a/fs/ubifs/tnc.c
+++ b/fs/ubifs/tnc.c
@@ -3091,14 +3091,7 @@ static void tnc_destroy_cnext(struct ubifs_info *c)
void ubifs_tnc_close(struct ubifs_info *c)
{
tnc_destroy_cnext(c);
- if (c->zroot.znode) {
- long n, freed;
-
- n = atomic_long_read(&c->clean_zn_cnt);
- freed = ubifs_destroy_tnc_subtree(c, c->zroot.znode);
- ubifs_assert(c, freed == n);
- atomic_long_sub(n, &ubifs_clean_zn_cnt);
- }
+ ubifs_destroy_tnc_tree(c);
kfree(c->gap_lebs);
kfree(c->ilebs);
destroy_old_idx(c);
diff --git a/fs/ubifs/tnc_misc.c b/fs/ubifs/tnc_misc.c
index 4d686e34e64d..d3f8a6aa1f49 100644
--- a/fs/ubifs/tnc_misc.c
+++ b/fs/ubifs/tnc_misc.c
@@ -250,6 +250,28 @@ long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
}
}
+/**
+ * ubifs_destroy_tnc_tree - destroy all znodes connected to the TNC tree.
+ * @c: UBIFS file-system description object
+ *
+ * This function destroys the whole TNC tree and updates clean global znode
+ * count.
+ */
+void ubifs_destroy_tnc_tree(struct ubifs_info *c)
+{
+ long n, freed;
+
+ if (!c->zroot.znode)
+ return;
+
+ n = atomic_long_read(&c->clean_zn_cnt);
+ freed = ubifs_destroy_tnc_subtree(c, c->zroot.znode);
+ ubifs_assert(c, freed == n);
+ atomic_long_sub(n, &ubifs_clean_zn_cnt);
+
+ c->zroot.znode = NULL;
+}
+
/**
* read_znode - read an indexing node from flash and fill znode.
* @c: UBIFS file-system description object
diff --git a/fs/ubifs/ubifs.h b/fs/ubifs/ubifs.h
index 3916dc4f30ca..6eba287ae66c 100644
--- a/fs/ubifs/ubifs.h
+++ b/fs/ubifs/ubifs.h
@@ -1903,6 +1903,7 @@ struct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c,
struct ubifs_znode *znode);
long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
struct ubifs_znode *zr);
+void ubifs_destroy_tnc_tree(struct ubifs_info *c);
struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
struct ubifs_zbranch *zbr,
struct ubifs_znode *parent, int iip);
--
2.39.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v3 2/2] ubifs: ubifs_symlink: Fix memleak of inode->i_link in error path
2024-01-08 2:41 [PATCH v3 0/2] ubifs: Fix two kmemleaks in error path Zhihao Cheng
2024-01-08 2:41 ` [PATCH v3 1/2] ubifs: dbg_check_idx_size: Fix kmemleak if loading znode failed Zhihao Cheng
@ 2024-01-08 2:41 ` Zhihao Cheng
1 sibling, 0 replies; 3+ messages in thread
From: Zhihao Cheng @ 2024-01-08 2:41 UTC (permalink / raw)
To: richard, ebiggers, terrelln; +Cc: linux-fscrypt, linux-mtd
For error handling path in ubifs_symlink(), inode will be marked as
bad first, then iput() is invoked. If inode->i_link is initialized by
fscrypt_encrypt_symlink() in encryption scenario, inode->i_link won't
be freed by callchain ubifs_free_inode -> fscrypt_free_inode in error
handling path, because make_bad_inode() has changed 'inode->i_mode' as
'S_IFREG'.
Following kmemleak is easy to be reproduced by injecting error in
ubifs_jnl_update() when doing symlink in encryption scenario:
unreferenced object 0xffff888103da3d98 (size 8):
comm "ln", pid 1692, jiffies 4294914701 (age 12.045s)
backtrace:
kmemdup+0x32/0x70
__fscrypt_encrypt_symlink+0xed/0x1c0
ubifs_symlink+0x210/0x300 [ubifs]
vfs_symlink+0x216/0x360
do_symlinkat+0x11a/0x190
do_syscall_64+0x3b/0xe0
There are two ways fixing it:
1. Remove make_bad_inode() in error handling path. We can do that
because ubifs_evict_inode() will do same processes for good
symlink inode and bad symlink inode, for inode->i_nlink checking
is before is_bad_inode().
2. Free inode->i_link before marking inode bad.
Method 2 is picked, it has less influence, personally, I think.
Cc: stable@vger.kernel.org
Fixes: 2c58d548f570 ("fscrypt: cache decrypted symlink target in ->i_link")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Suggested-by: Eric Biggers <ebiggers@kernel.org>
Reviewed-by: Eric Biggers <ebiggers@google.com>
---
fs/ubifs/dir.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index 3b13c648d490..e413a9cf8ee3 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -1234,6 +1234,8 @@ static int ubifs_symlink(struct mnt_idmap *idmap, struct inode *dir,
dir_ui->ui_size = dir->i_size;
mutex_unlock(&dir_ui->ui_mutex);
out_inode:
+ /* Free inode->i_link before inode is marked as bad. */
+ fscrypt_free_inode(inode);
make_bad_inode(inode);
iput(inode);
out_fname:
--
2.39.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-01-08 2:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-08 2:41 [PATCH v3 0/2] ubifs: Fix two kmemleaks in error path Zhihao Cheng
2024-01-08 2:41 ` [PATCH v3 1/2] ubifs: dbg_check_idx_size: Fix kmemleak if loading znode failed Zhihao Cheng
2024-01-08 2:41 ` [PATCH v3 2/2] ubifs: ubifs_symlink: Fix memleak of inode->i_link in error path 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).