[PATCH] f2fs: device alias: add sanity check for device alias inode

Chao Yu posted 1 patch 2 months ago
fs/f2fs/extent_cache.c | 32 +++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)
[PATCH] f2fs: device alias: add sanity check for device alias inode
Posted by Chao Yu 2 months ago
Do sanity check for extent info of device alias inode, in order to
avoid unexpected error caused by fuzz test.

Cc: Daeho Jeong <daehojeong@google.com>
Signed-off-by: Chao Yu <chao@kernel.org>
---

To Daeho and Jaegeuk,

Merge this into initial patch or merge it separately is both fine
to me.

 fs/f2fs/extent_cache.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/extent_cache.c b/fs/f2fs/extent_cache.c
index 0c8a705faa8b..5bf9e4c2b49c 100644
--- a/fs/f2fs/extent_cache.c
+++ b/fs/f2fs/extent_cache.c
@@ -24,6 +24,7 @@ bool sanity_check_extent_cache(struct inode *inode, struct page *ipage)
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 	struct f2fs_extent *i_ext = &F2FS_INODE(ipage)->i_ext;
 	struct extent_info ei;
+	int devi;
 
 	get_read_extent_info(&ei, i_ext);
 
@@ -38,7 +39,36 @@ bool sanity_check_extent_cache(struct inode *inode, struct page *ipage)
 			  ei.blk, ei.fofs, ei.len);
 		return false;
 	}
-	return true;
+
+	if (!IS_DEVICE_ALIASING(inode))
+		return true;
+
+	for (devi = 0; devi < sbi->s_ndevs; devi++) {
+		if (FDEV(devi).start_blk != ei.blk ||
+			FDEV(devi).end_blk != ei.blk + ei.len)
+			continue;
+
+		if (devi == 0) {
+			f2fs_warn(sbi,
+				"%s: inode (ino=%lx) is an alias of meta device",
+				__func__, inode->i_ino);
+			return false;
+		}
+
+		if (bdev_is_zoned(FDEV(devi).bdev)) {
+			f2fs_warn(sbi,
+				"%s: device alias inode (ino=%lx)'s extent info "
+				"[%u, %u, %u] maps to zoned block device",
+				__func__, inode->i_ino, ei.blk, ei.fofs, ei.len);
+			return false;
+		}
+		return true;
+	}
+
+	f2fs_warn(sbi, "%s: device alias inode (ino=%lx)'s extent info "
+			"[%u, %u, %u] is inconsistent w/ any devices",
+			__func__, inode->i_ino, ei.blk, ei.fofs, ei.len);
+	return false;
 }
 
 static void __set_extent_info(struct extent_info *ei,
-- 
2.40.1
Re: [f2fs-dev] [PATCH] f2fs: device alias: add sanity check for device alias inode
Posted by Daeho Jeong 2 months ago
On Mon, Sep 23, 2024 at 7:32 PM Chao Yu via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> Do sanity check for extent info of device alias inode, in order to
> avoid unexpected error caused by fuzz test.
>
> Cc: Daeho Jeong <daehojeong@google.com>
> Signed-off-by: Chao Yu <chao@kernel.org>
> ---
>
> To Daeho and Jaegeuk,
>
> Merge this into initial patch or merge it separately is both fine
> to me.

I am going to merge this into my one. Thanks~!

>
>  fs/f2fs/extent_cache.c | 32 +++++++++++++++++++++++++++++++-
>  1 file changed, 31 insertions(+), 1 deletion(-)
>
> diff --git a/fs/f2fs/extent_cache.c b/fs/f2fs/extent_cache.c
> index 0c8a705faa8b..5bf9e4c2b49c 100644
> --- a/fs/f2fs/extent_cache.c
> +++ b/fs/f2fs/extent_cache.c
> @@ -24,6 +24,7 @@ bool sanity_check_extent_cache(struct inode *inode, struct page *ipage)
>         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
>         struct f2fs_extent *i_ext = &F2FS_INODE(ipage)->i_ext;
>         struct extent_info ei;
> +       int devi;
>
>         get_read_extent_info(&ei, i_ext);
>
> @@ -38,7 +39,36 @@ bool sanity_check_extent_cache(struct inode *inode, struct page *ipage)
>                           ei.blk, ei.fofs, ei.len);
>                 return false;
>         }
> -       return true;
> +
> +       if (!IS_DEVICE_ALIASING(inode))
> +               return true;
> +
> +       for (devi = 0; devi < sbi->s_ndevs; devi++) {
> +               if (FDEV(devi).start_blk != ei.blk ||
> +                       FDEV(devi).end_blk != ei.blk + ei.len)

ei.blk + ei.len -> ei.blk + ei.len - 1?

> +                       continue;
> +
> +               if (devi == 0) {
> +                       f2fs_warn(sbi,
> +                               "%s: inode (ino=%lx) is an alias of meta device",
> +                               __func__, inode->i_ino);
> +                       return false;
> +               }
> +
> +               if (bdev_is_zoned(FDEV(devi).bdev)) {
> +                       f2fs_warn(sbi,
> +                               "%s: device alias inode (ino=%lx)'s extent info "
> +                               "[%u, %u, %u] maps to zoned block device",
> +                               __func__, inode->i_ino, ei.blk, ei.fofs, ei.len);
> +                       return false;
> +               }
> +               return true;
> +       }
> +
> +       f2fs_warn(sbi, "%s: device alias inode (ino=%lx)'s extent info "
> +                       "[%u, %u, %u] is inconsistent w/ any devices",
> +                       __func__, inode->i_ino, ei.blk, ei.fofs, ei.len);
> +       return false;
>  }
>
>  static void __set_extent_info(struct extent_info *ei,
> --
> 2.40.1
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Re: [f2fs-dev] [PATCH] f2fs: device alias: add sanity check for device alias inode
Posted by Chao Yu 2 months ago
On 2024/9/25 4:38, Daeho Jeong wrote:
> On Mon, Sep 23, 2024 at 7:32 PM Chao Yu via Linux-f2fs-devel
> <linux-f2fs-devel@lists.sourceforge.net> wrote:
>>
>> Do sanity check for extent info of device alias inode, in order to
>> avoid unexpected error caused by fuzz test.
>>
>> Cc: Daeho Jeong <daehojeong@google.com>
>> Signed-off-by: Chao Yu <chao@kernel.org>
>> ---
>>
>> To Daeho and Jaegeuk,
>>
>> Merge this into initial patch or merge it separately is both fine
>> to me.
> 
> I am going to merge this into my one. Thanks~!
> 
>>
>>   fs/f2fs/extent_cache.c | 32 +++++++++++++++++++++++++++++++-
>>   1 file changed, 31 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/f2fs/extent_cache.c b/fs/f2fs/extent_cache.c
>> index 0c8a705faa8b..5bf9e4c2b49c 100644
>> --- a/fs/f2fs/extent_cache.c
>> +++ b/fs/f2fs/extent_cache.c
>> @@ -24,6 +24,7 @@ bool sanity_check_extent_cache(struct inode *inode, struct page *ipage)
>>          struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
>>          struct f2fs_extent *i_ext = &F2FS_INODE(ipage)->i_ext;
>>          struct extent_info ei;
>> +       int devi;
>>
>>          get_read_extent_info(&ei, i_ext);
>>
>> @@ -38,7 +39,36 @@ bool sanity_check_extent_cache(struct inode *inode, struct page *ipage)
>>                            ei.blk, ei.fofs, ei.len);
>>                  return false;
>>          }
>> -       return true;
>> +
>> +       if (!IS_DEVICE_ALIASING(inode))
>> +               return true;
>> +
>> +       for (devi = 0; devi < sbi->s_ndevs; devi++) {
>> +               if (FDEV(devi).start_blk != ei.blk ||
>> +                       FDEV(devi).end_blk != ei.blk + ei.len)
> 
> ei.blk + ei.len -> ei.blk + ei.len - 1?

Yes, thanks for the correcting.

Thanks,

> 
>> +                       continue;
>> +
>> +               if (devi == 0) {
>> +                       f2fs_warn(sbi,
>> +                               "%s: inode (ino=%lx) is an alias of meta device",
>> +                               __func__, inode->i_ino);
>> +                       return false;
>> +               }
>> +
>> +               if (bdev_is_zoned(FDEV(devi).bdev)) {
>> +                       f2fs_warn(sbi,
>> +                               "%s: device alias inode (ino=%lx)'s extent info "
>> +                               "[%u, %u, %u] maps to zoned block device",
>> +                               __func__, inode->i_ino, ei.blk, ei.fofs, ei.len);
>> +                       return false;
>> +               }
>> +               return true;
>> +       }
>> +
>> +       f2fs_warn(sbi, "%s: device alias inode (ino=%lx)'s extent info "
>> +                       "[%u, %u, %u] is inconsistent w/ any devices",
>> +                       __func__, inode->i_ino, ei.blk, ei.fofs, ei.len);
>> +       return false;
>>   }
>>
>>   static void __set_extent_info(struct extent_info *ei,
>> --
>> 2.40.1
>>
>>
>>
>> _______________________________________________
>> Linux-f2fs-devel mailing list
>> Linux-f2fs-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel