linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
* [f2fs-dev] [PATCH v3] f2fs: fix to detect corrupted meta ino
@ 2022-09-13  7:48 Chao Yu
  2022-09-22  1:54 ` Chao Yu
  0 siblings, 1 reply; 4+ messages in thread
From: Chao Yu @ 2022-09-13  7:48 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-kernel, linux-f2fs-devel

It is possible that ino of dirent or orphan inode is corrupted in a
fuzzed image, occasionally, if corrupted ino is equal to meta ino:
meta_ino, node_ino or compress_ino, caller of f2fs_iget() from below
call paths will get meta inode directly, it's not allowed, let's
add sanity check to detect such cases.

case #1
- recover_dentry
 - __f2fs_find_entry
 - f2fs_iget_retry

case #2
- recover_orphan_inode
 - f2fs_iget_retry

Signed-off-by: Chao Yu <chao@kernel.org>
---
v3:
- update commit title/message
- change logic inside f2fs_iget() rather than its caller
 fs/f2fs/inode.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index cde0a3dc80c3..1baac6056733 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -487,6 +487,12 @@ static int do_read_inode(struct inode *inode)
 	return 0;
 }
 
+static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
+{
+	return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) ||
+		ino == F2FS_COMPRESS_INO(sbi);
+}
+
 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
 {
 	struct f2fs_sb_info *sbi = F2FS_SB(sb);
@@ -497,17 +503,22 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
 	if (!inode)
 		return ERR_PTR(-ENOMEM);
 
+	if (is_meta_ino(sbi, ino)) {
+		if (!(inode->i_state & I_NEW)) {
+			f2fs_err(sbi, "detect corrupted inode no:%lu, run fsck to repair", ino);
+			set_sbi_flag(sbi, SBI_NEED_FSCK);
+			ret = -EFSCORRUPTED;
+			trace_f2fs_iget_exit(inode, ret);
+			iput(inode);
+			return ERR_PTR(ret);
+		}
+		goto make_now;
+	}
+
 	if (!(inode->i_state & I_NEW)) {
 		trace_f2fs_iget(inode);
 		return inode;
 	}
-	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
-		goto make_now;
-
-#ifdef CONFIG_F2FS_FS_COMPRESSION
-	if (ino == F2FS_COMPRESS_INO(sbi))
-		goto make_now;
-#endif
 
 	ret = do_read_inode(inode);
 	if (ret)
-- 
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH v3] f2fs: fix to detect corrupted meta ino
  2022-09-13  7:48 [f2fs-dev] [PATCH v3] f2fs: fix to detect corrupted meta ino Chao Yu
@ 2022-09-22  1:54 ` Chao Yu
  2022-09-22 18:11   ` Jaegeuk Kim
  0 siblings, 1 reply; 4+ messages in thread
From: Chao Yu @ 2022-09-22  1:54 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-kernel, linux-f2fs-devel

Ping,

On 2022/9/13 15:48, Chao Yu wrote:
> It is possible that ino of dirent or orphan inode is corrupted in a
> fuzzed image, occasionally, if corrupted ino is equal to meta ino:
> meta_ino, node_ino or compress_ino, caller of f2fs_iget() from below
> call paths will get meta inode directly, it's not allowed, let's
> add sanity check to detect such cases.
> 
> case #1
> - recover_dentry
>   - __f2fs_find_entry
>   - f2fs_iget_retry
> 
> case #2
> - recover_orphan_inode
>   - f2fs_iget_retry
> 
> Signed-off-by: Chao Yu <chao@kernel.org>
> ---
> v3:
> - update commit title/message
> - change logic inside f2fs_iget() rather than its caller
>   fs/f2fs/inode.c | 25 ++++++++++++++++++-------
>   1 file changed, 18 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
> index cde0a3dc80c3..1baac6056733 100644
> --- a/fs/f2fs/inode.c
> +++ b/fs/f2fs/inode.c
> @@ -487,6 +487,12 @@ static int do_read_inode(struct inode *inode)
>   	return 0;
>   }
>   
> +static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
> +{
> +	return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) ||
> +		ino == F2FS_COMPRESS_INO(sbi);
> +}
> +
>   struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
>   {
>   	struct f2fs_sb_info *sbi = F2FS_SB(sb);
> @@ -497,17 +503,22 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
>   	if (!inode)
>   		return ERR_PTR(-ENOMEM);
>   
> +	if (is_meta_ino(sbi, ino)) {
> +		if (!(inode->i_state & I_NEW)) {
> +			f2fs_err(sbi, "detect corrupted inode no:%lu, run fsck to repair", ino);
> +			set_sbi_flag(sbi, SBI_NEED_FSCK);
> +			ret = -EFSCORRUPTED;
> +			trace_f2fs_iget_exit(inode, ret);
> +			iput(inode);
> +			return ERR_PTR(ret);
> +		}
> +		goto make_now;
> +	}
> +
>   	if (!(inode->i_state & I_NEW)) {
>   		trace_f2fs_iget(inode);
>   		return inode;
>   	}
> -	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
> -		goto make_now;
> -
> -#ifdef CONFIG_F2FS_FS_COMPRESSION
> -	if (ino == F2FS_COMPRESS_INO(sbi))
> -		goto make_now;
> -#endif
>   
>   	ret = do_read_inode(inode);
>   	if (ret)


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH v3] f2fs: fix to detect corrupted meta ino
  2022-09-22  1:54 ` Chao Yu
@ 2022-09-22 18:11   ` Jaegeuk Kim
  2022-09-25 13:58     ` Chao Yu
  0 siblings, 1 reply; 4+ messages in thread
From: Jaegeuk Kim @ 2022-09-22 18:11 UTC (permalink / raw)
  To: Chao Yu; +Cc: linux-kernel, linux-f2fs-devel

I modified a bit.

https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/commit/?h=dev-test&id=a9a1592fa175baaaae43f54f175a972757c47919

Thanks,

On 09/22, Chao Yu wrote:
> Ping,
> 
> On 2022/9/13 15:48, Chao Yu wrote:
> > It is possible that ino of dirent or orphan inode is corrupted in a
> > fuzzed image, occasionally, if corrupted ino is equal to meta ino:
> > meta_ino, node_ino or compress_ino, caller of f2fs_iget() from below
> > call paths will get meta inode directly, it's not allowed, let's
> > add sanity check to detect such cases.
> > 
> > case #1
> > - recover_dentry
> >   - __f2fs_find_entry
> >   - f2fs_iget_retry
> > 
> > case #2
> > - recover_orphan_inode
> >   - f2fs_iget_retry
> > 
> > Signed-off-by: Chao Yu <chao@kernel.org>
> > ---
> > v3:
> > - update commit title/message
> > - change logic inside f2fs_iget() rather than its caller
> >   fs/f2fs/inode.c | 25 ++++++++++++++++++-------
> >   1 file changed, 18 insertions(+), 7 deletions(-)
> > 
> > diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
> > index cde0a3dc80c3..1baac6056733 100644
> > --- a/fs/f2fs/inode.c
> > +++ b/fs/f2fs/inode.c
> > @@ -487,6 +487,12 @@ static int do_read_inode(struct inode *inode)
> >   	return 0;
> >   }
> > +static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
> > +{
> > +	return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) ||
> > +		ino == F2FS_COMPRESS_INO(sbi);
> > +}
> > +
> >   struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
> >   {
> >   	struct f2fs_sb_info *sbi = F2FS_SB(sb);
> > @@ -497,17 +503,22 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
> >   	if (!inode)
> >   		return ERR_PTR(-ENOMEM);
> > +	if (is_meta_ino(sbi, ino)) {
> > +		if (!(inode->i_state & I_NEW)) {
> > +			f2fs_err(sbi, "detect corrupted inode no:%lu, run fsck to repair", ino);
> > +			set_sbi_flag(sbi, SBI_NEED_FSCK);
> > +			ret = -EFSCORRUPTED;
> > +			trace_f2fs_iget_exit(inode, ret);
> > +			iput(inode);
> > +			return ERR_PTR(ret);
> > +		}
> > +		goto make_now;
> > +	}
> > +
> >   	if (!(inode->i_state & I_NEW)) {
> >   		trace_f2fs_iget(inode);
> >   		return inode;
> >   	}
> > -	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
> > -		goto make_now;
> > -
> > -#ifdef CONFIG_F2FS_FS_COMPRESSION
> > -	if (ino == F2FS_COMPRESS_INO(sbi))
> > -		goto make_now;
> > -#endif
> >   	ret = do_read_inode(inode);
> >   	if (ret)


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

* Re: [f2fs-dev] [PATCH v3] f2fs: fix to detect corrupted meta ino
  2022-09-22 18:11   ` Jaegeuk Kim
@ 2022-09-25 13:58     ` Chao Yu
  0 siblings, 0 replies; 4+ messages in thread
From: Chao Yu @ 2022-09-25 13:58 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: linux-kernel, linux-f2fs-devel

On 2022/9/23 2:11, Jaegeuk Kim wrote:
> I modified a bit.
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/commit/?h=dev-test&id=a9a1592fa175baaaae43f54f175a972757c47919

Fine to me. :)

Thanks,

> 
> Thanks,
> 
> On 09/22, Chao Yu wrote:
>> Ping,
>>
>> On 2022/9/13 15:48, Chao Yu wrote:
>>> It is possible that ino of dirent or orphan inode is corrupted in a
>>> fuzzed image, occasionally, if corrupted ino is equal to meta ino:
>>> meta_ino, node_ino or compress_ino, caller of f2fs_iget() from below
>>> call paths will get meta inode directly, it's not allowed, let's
>>> add sanity check to detect such cases.
>>>
>>> case #1
>>> - recover_dentry
>>>    - __f2fs_find_entry
>>>    - f2fs_iget_retry
>>>
>>> case #2
>>> - recover_orphan_inode
>>>    - f2fs_iget_retry
>>>
>>> Signed-off-by: Chao Yu <chao@kernel.org>
>>> ---
>>> v3:
>>> - update commit title/message
>>> - change logic inside f2fs_iget() rather than its caller
>>>    fs/f2fs/inode.c | 25 ++++++++++++++++++-------
>>>    1 file changed, 18 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
>>> index cde0a3dc80c3..1baac6056733 100644
>>> --- a/fs/f2fs/inode.c
>>> +++ b/fs/f2fs/inode.c
>>> @@ -487,6 +487,12 @@ static int do_read_inode(struct inode *inode)
>>>    	return 0;
>>>    }
>>> +static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
>>> +{
>>> +	return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) ||
>>> +		ino == F2FS_COMPRESS_INO(sbi);
>>> +}
>>> +
>>>    struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
>>>    {
>>>    	struct f2fs_sb_info *sbi = F2FS_SB(sb);
>>> @@ -497,17 +503,22 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
>>>    	if (!inode)
>>>    		return ERR_PTR(-ENOMEM);
>>> +	if (is_meta_ino(sbi, ino)) {
>>> +		if (!(inode->i_state & I_NEW)) {
>>> +			f2fs_err(sbi, "detect corrupted inode no:%lu, run fsck to repair", ino);
>>> +			set_sbi_flag(sbi, SBI_NEED_FSCK);
>>> +			ret = -EFSCORRUPTED;
>>> +			trace_f2fs_iget_exit(inode, ret);
>>> +			iput(inode);
>>> +			return ERR_PTR(ret);
>>> +		}
>>> +		goto make_now;
>>> +	}
>>> +
>>>    	if (!(inode->i_state & I_NEW)) {
>>>    		trace_f2fs_iget(inode);
>>>    		return inode;
>>>    	}
>>> -	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
>>> -		goto make_now;
>>> -
>>> -#ifdef CONFIG_F2FS_FS_COMPRESSION
>>> -	if (ino == F2FS_COMPRESS_INO(sbi))
>>> -		goto make_now;
>>> -#endif
>>>    	ret = do_read_inode(inode);
>>>    	if (ret)


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

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

end of thread, other threads:[~2022-09-25 13:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-13  7:48 [f2fs-dev] [PATCH v3] f2fs: fix to detect corrupted meta ino Chao Yu
2022-09-22  1:54 ` Chao Yu
2022-09-22 18:11   ` Jaegeuk Kim
2022-09-25 13:58     ` Chao Yu

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).