diff mbox series

[2/2] ubifs: ubifs_symlink: Fix memleak of inode->i_link in error path

Message ID 20231222025025.2940450-3-chengzhihao1@huawei.com
State Superseded
Headers show
Series ubifs: Fix two kmemleaks in error path | expand

Commit Message

Zhihao Cheng Dec. 22, 2023, 2:50 a.m. UTC
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.

Fixes: 2c58d548f570 ("fscrypt: cache decrypted symlink target in ->i_link")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
---
 fs/ubifs/dir.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Eric Biggers Dec. 22, 2023, 6:22 a.m. UTC | #1
On Fri, Dec 22, 2023 at 10:50:25AM +0800, Zhihao Cheng wrote:
> 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.
> 
> Fixes: 2c58d548f570 ("fscrypt: cache decrypted symlink target in ->i_link")
> Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
> ---
>  fs/ubifs/dir.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
> index 3b13c648d490..9baf029416d4 100644
> --- a/fs/ubifs/dir.c
> +++ b/fs/ubifs/dir.c
> @@ -1234,6 +1234,9 @@ 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. */
> +	if (IS_ENCRYPTED(inode))
> +		fscrypt_free_inode(inode);
>  	make_bad_inode(inode);
>  	iput(inode);
>  out_fname:

fscrypt_free_inode() should be called unconditionally, because
fscrypt_free_inode() already checks for IS_ENCRYPTED().

Also, please add 'Cc: stable@vger.kernel.org' to your commit message.

Also, please add linux-fscrypt@vger.kernel.org to the recipients of this patch.

Anyway, thanks for catching this bug!  It's annoying that make_bad_inode()
changes the inode's type from a symlink to a regular file; normally an inode's
type can never change.  Your fix looks fine, though.  Thanks!

- Eric
Zhihao Cheng Dec. 22, 2023, 8:38 a.m. UTC | #2
在 2023/12/22 14:22, Eric Biggers 写道:
> fscrypt_free_inode() should be called unconditionally, because
> fscrypt_free_inode() already checks for IS_ENCRYPTED().
>
> Also, please add 'Cc:stable@vger.kernel.org' to your commit message.
>
> Also, please addlinux-fscrypt@vger.kernel.org  to the recipients of this patch.
>
> Anyway, thanks for catching this bug!  It's annoying that make_bad_inode()
> changes the inode's type from a symlink to a regular file; normally an inode's
> type can never change.  Your fix looks fine, though.  Thanks!

Thanks for suggestions, I will modify them in v2.
diff mbox series

Patch

diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index 3b13c648d490..9baf029416d4 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -1234,6 +1234,9 @@  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. */
+	if (IS_ENCRYPTED(inode))
+		fscrypt_free_inode(inode);
 	make_bad_inode(inode);
 	iput(inode);
 out_fname: