2022-08-16 08:03:21

by Dongliang Mu

[permalink] [raw]
Subject: [PATCH v2] fs: fix UAF/GPF bug in nilfs_mdt_destroy

From: Dongliang Mu <[email protected]>

In alloc_inode, inode_init_always() could return -ENOMEM if
security_inode_alloc() fails, which causes inode->i_private
uninitialized. Then nilfs_is_metadata_file_inode() returns
true and nilfs_free_inode() wrongly calls nilfs_mdt_destroy(),
which frees the uninitialized inode->i_private
and leads to crashes(e.g., UAF/GPF).

Fix this by moving security_inode_alloc just prior to
this_cpu_inc(nr_inodes)

Link: https://lkml.kernel.org/r/CAFcO6XOcf1Jj2SeGt=jJV59wmhESeSKpfR0omdFRq+J9nD1vfQ@mail.gmail.com
Reported-by: butt3rflyh4ck <[email protected]>
Reported-by: Hao Sun <[email protected]>
Reported-by: Jiacheng Xu <[email protected]>
Signed-off-by: Dongliang Mu <[email protected]>
Cc: Al Viro <[email protected]>
Cc: [email protected]
---
v1->v2: move security_inode_alloc at the very end according to Al Viro
other than initializing i_private before security_inode_alloc.
fs/inode.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/inode.c b/fs/inode.c
index 6462276dfdf0..49d1eb91728c 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -192,8 +192,6 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
inode->i_wb_frn_history = 0;
#endif

- if (security_inode_alloc(inode))
- goto out;
spin_lock_init(&inode->i_lock);
lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);

@@ -228,6 +226,9 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
inode->i_fsnotify_mask = 0;
#endif
inode->i_flctx = NULL;
+
+ if (security_inode_alloc(inode))
+ goto out;
this_cpu_inc(nr_inodes);

return 0;
--
2.35.1


2022-08-16 12:02:28

by Christian Brauner

[permalink] [raw]
Subject: Re: [PATCH v2] fs: fix UAF/GPF bug in nilfs_mdt_destroy

On Tue, Aug 16, 2022 at 12:08:58PM +0800, Dongliang Mu wrote:
> From: Dongliang Mu <[email protected]>
>
> In alloc_inode, inode_init_always() could return -ENOMEM if
> security_inode_alloc() fails, which causes inode->i_private
> uninitialized. Then nilfs_is_metadata_file_inode() returns
> true and nilfs_free_inode() wrongly calls nilfs_mdt_destroy(),
> which frees the uninitialized inode->i_private
> and leads to crashes(e.g., UAF/GPF).
>
> Fix this by moving security_inode_alloc just prior to
> this_cpu_inc(nr_inodes)
>
> Link: https://lkml.kernel.org/r/CAFcO6XOcf1Jj2SeGt=jJV59wmhESeSKpfR0omdFRq+J9nD1vfQ@mail.gmail.com
> Reported-by: butt3rflyh4ck <[email protected]>
> Reported-by: Hao Sun <[email protected]>
> Reported-by: Jiacheng Xu <[email protected]>
> Signed-off-by: Dongliang Mu <[email protected]>
> Cc: Al Viro <[email protected]>
> Cc: [email protected]
> ---

Looks good to me,
Reviewed-by: Christian Brauner (Microsoft) <[email protected]>

2022-08-25 20:02:05

by Eric Sandeen

[permalink] [raw]
Subject: Re: [PATCH v2] fs: fix UAF/GPF bug in nilfs_mdt_destroy

On 8/15/22 11:08 PM, Dongliang Mu wrote:
> From: Dongliang Mu <[email protected]>
>
> In alloc_inode, inode_init_always() could return -ENOMEM if
> security_inode_alloc() fails, which causes inode->i_private
> uninitialized. Then nilfs_is_metadata_file_inode() returns
> true and nilfs_free_inode() wrongly calls nilfs_mdt_destroy(),
> which frees the uninitialized inode->i_private
> and leads to crashes(e.g., UAF/GPF).
>
> Fix this by moving security_inode_alloc just prior to
> this_cpu_inc(nr_inodes)
>
> Link: https://lkml.kernel.org/r/CAFcO6XOcf1Jj2SeGt=jJV59wmhESeSKpfR0omdFRq+J9nD1vfQ@mail.gmail.com
> Reported-by: butt3rflyh4ck <[email protected]>
> Reported-by: Hao Sun <[email protected]>
> Reported-by: Jiacheng Xu <[email protected]>
> Signed-off-by: Dongliang Mu <[email protected]>
> Cc: Al Viro <[email protected]>
> Cc: [email protected]
> ---
> v1->v2: move security_inode_alloc at the very end according to Al Viro
> other than initializing i_private before security_inode_alloc.
> fs/inode.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/fs/inode.c b/fs/inode.c
> index 6462276dfdf0..49d1eb91728c 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -192,8 +192,6 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
> inode->i_wb_frn_history = 0;
> #endif
>
> - if (security_inode_alloc(inode))
> - goto out;
> spin_lock_init(&inode->i_lock);
> lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
>
> @@ -228,6 +226,9 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
> inode->i_fsnotify_mask = 0;
> #endif
> inode->i_flctx = NULL;
> +
> + if (security_inode_alloc(inode))
> + goto out;

Seems like the out: label could be removed, and simply return -ENOMEM directly here,
but that's just a nitpick.

-Eric

> this_cpu_inc(nr_inodes);
>
> return 0;