f2fs: fix to detect corrupted meta ino
Chao Yu
maintainerPATCH v3 1/1 f2fs: fix to detect corrupted meta ino
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
---
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(-)
fs/f2fs/inode.c +18 -7
@@ -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)
Signature · 1 line
2.25.1
Chao Yu
maintainerPing,
Quoted 70 lines · Chao Yu
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_inoorcompress_ino, caller off2fs_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_retrycase #2
-recover_orphan_inode
-f2fs_iget_retrySigned-off-by Chao Yu <[email protected]>---
v3:
- update commit title/message
- change logic insidef2fs_iget()rather than its callerfs/f2fs/inode.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)fs/f2fs/inode.c +18
-7@@ -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)
Jaegeuk Kim
maintainerI modified a bit.
Thanks,
Quoted 70 lines · Chao Yu
Ping,
Quoted 67 lines · Chao Yu
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_inoorcompress_ino, caller off2fs_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_retrycase #2
-recover_orphan_inode
-f2fs_iget_retrySigned-off-by Chao Yu <[email protected]>---
v3:
- update commit title/message
- change logic insidef2fs_iget()rather than its callerfs/f2fs/inode.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)fs/f2fs/inode.c +18
-7@@ -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)
Chao Yu
maintainerQuoted 3 lines · Jaegeuk Kim
I modified a bit.
Fine to me. :)
Thanks,
Quoted 74 lines
Thanks,
Quoted 70 lines · Chao Yu
Ping,
Quoted 67 lines · Chao Yu
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_inoorcompress_ino, caller off2fs_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_retrycase #2
-recover_orphan_inode
-f2fs_iget_retrySigned-off-by Chao Yu <[email protected]>---
v3:
- update commit title/message
- change logic insidef2fs_iget()rather than its callerfs/f2fs/inode.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)fs/f2fs/inode.c +18
-7@@ -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)