ocfs2: Add a sanity check for corrupted file system.
PATCH 1/2 ocfs2: Add a sanity check for corrupted file system.
Hi,
Recently syzbot reported a use-after-free issue[1].
The root cause of the problem is that the journal
inode recorded in this file system image is corrupted. The value of "di->id2.i_list.l_next_free_rec" is 8193, which is greater than the value of "di->id2.i_list.l_count" (19).
To solve this problem, an additional check should be added within ocfs2_get_clusters_nocache(). If the check fails, an error will be returned and the file system will be set to read-only.
[1]: https://lore.kernel.org/all/[email protected]/T/
--- fs/ocfs2/extent_map.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/ocfs2/extent_map.c +10 -0
@@ -414,6 +414,16 @@ static int ocfs2_get_clusters_nocache(struct inode *inode, el = &di->id2.i_list; tree_height =le16_to_cpu (el->l_tree_depth); +if (le16_to_cpu (di->id2.i_list.l_next_free_rec) > +le16_to_cpu (di->id2.i_list.l_count)) { + ret =ocfs2_error (inode->i_sb,"Invalid dinode #%llu: next_free_rec is %u, count is %u\n" , + (unsigned long long )di_bh->b_blocknr, +le16_to_cpu (di->id2.i_list.l_next_free_rec), +le16_to_cpu (di->id2.i_list.l_count)); + +return ret; + } +if (tree_height >0 ) { ret =ocfs2_find_leaf (INODE_CACHE (inode), el, v_cluster, &eb_bh);
Signature · 1 line
2.39.5
PATCH 2/2 ocfs2: Correct l_next_free_rec in online check
Correct the value of l_next_free_rec to l_count during the online check, as done in the check_el() function in ocfs2_tools.
--- fs/ocfs2/inode.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
fs/ocfs2/inode.c +24 -0
@@ -200,6 +200,20 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,return inode; } +static int has_extents (struct ocfs2_dinode *di) +{ +/* inodes flagged with other stuff in id2 */ +if (di->i_flags & (OCFS2_SUPER_BLOCK_FL |OCFS2_LOCAL_ALLOC_FL | +OCFS2_CHAIN_FL |OCFS2_DEALLOC_FL )) +return 0 ; +/* i_flags doesn't indicate when id2 is a fast symlink */ +if (S_ISLNK (di->i_mode) && di->i_size && di->i_clusters ==0 ) +return 0 ; +if (di->i_dyn_features &OCFS2_INLINE_DATA_FL ) +return 0 ; + +return 1 ; +}/* * here's how inodes get read from disk:
@@ -1528,6 +1542,16 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,return -OCFS2_FILECHECK_ERR_VALIDFLAG ; } +if (has_extents (di) &&le16_to_cpu (di->id2.i_list.l_next_free_rec) > +le16_to_cpu (di->id2.i_list.l_count)) { + di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count; + changed =1 ; +mlog (ML_ERROR , +"Filecheck: reset dinode #%llu: l_next_free_rec to %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->id2.i_list.l_next_free_rec)); + } +if (le64_to_cpu (di->i_blkno) != bh->b_blocknr) { di->i_blkno =cpu_to_le64 (bh->b_blocknr); changed =1 ;
Signature · 1 line
2.39.5
Joseph Qi
maintainerRe: [PATCH 2/2] ocfs2: Correct l_next_free_rec in online check
Quoted 13 lines · Julian Sun
Correct the value of
l_next_free_rectol_countduring the online check, as done in thecheck_el()function inocfs2_tools.Signed-off-by Julian Sun <[email protected]>--- fs/ocfs2/inode.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)fs/ocfs2/inode.c +0
-0
fs/ocfs2/inode.c struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, · line 203
Quoted 4 lines
fs/ocfs2/inode.c +1
-0@@ -200,6 +200,20 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,return inode; } +static int has_extents (struct ocfs2_dinode *di)
Better to rename it to 'ocfs2_dinode_has_extents'.
Quoted 20 lines
fs/ocfs2/inode.c +13
-0+{ +/* inodes flagged with other stuff in id2 */ +if (di->i_flags & (OCFS2_SUPER_BLOCK_FL |OCFS2_LOCAL_ALLOC_FL | +OCFS2_CHAIN_FL |OCFS2_DEALLOC_FL )) +return 0 ; +/* i_flags doesn't indicate when id2 is a fast symlink */ +if (S_ISLNK (di->i_mode) && di->i_size && di->i_clusters ==0 ) +return 0 ; +if (di->i_dyn_features &OCFS2_INLINE_DATA_FL ) +return 0 ; + +return 1 ; +}/* * here's how inodes get read from disk: @@ -1528,6 +1542,16 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,return -OCFS2_FILECHECK_ERR_VALIDFLAG ; }
fs/ocfs2/inode.c static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, · line 1554
Quoted 10 lines
fs/ocfs2/inode.c +10
-0+if (has_extents (di) &&le16_to_cpu (di->id2.i_list.l_next_free_rec) > +le16_to_cpu (di->id2.i_list.l_count)) { + di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count; + changed =1 ; +mlog (ML_ERROR , +"Filecheck: reset dinode #%llu: l_next_free_rec to %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->id2.i_list.l_next_free_rec)); + } +
I'd like to move this check behind fs_generation since they are commoner check.
Thanks,
Joseph
Quoted 3 lines
fs/ocfs2/inode.c +0
-0if (le64_to_cpu (di->i_blkno) != bh->b_blocknr) { di->i_blkno =cpu_to_le64 (bh->b_blocknr); changed =1 ;
Re: [PATCH 2/2] ocfs2: Correct l_next_free_rec in online check
Quoted 11 lines · Joseph Qi
Quoted 8 lines · Julian Sun
Correct the value of
l_next_free_rectol_countduring the online check, as done in thecheck_el()function inocfs2_tools.Signed-off-by Julian Sun <[email protected]>---
fs/ocfs2/inode.c | 24 ++++++++++++++++++++++++1 file changed, 24 insertions(+)
Quoted 11 lines
Quoted 9 lines
fs/ocfs2/inode.c +0
-0@@ -200,6 +200,20 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, return inode; }
+static inthas_extents(struct ocfs2_dinode *di)Better to rename it to '
ocfs2_dinode_has_extents'.
Reasonable.
Quoted 25 lines
Quoted 25 lines
fs/ocfs2/inode.c +13
-0+{ + /* inodes flagged with other stuff in id2 */ + if (di->i_flags & (OCFS2_SUPER_BLOCK_FL | OCFS2_LOCAL_ALLOC_FL | + OCFS2_CHAIN_FL | OCFS2_DEALLOC_FL)) + return 0; + /* i_flags doesn't indicate when id2 is a fast symlink */ + if (S_ISLNK(di->i_mode) && di->i_size && di->i_clusters == 0) + return 0; + if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) + return 0; + + return 1; +}
/*
* here's how inodes get read from disk:fs/ocfs2/inode.c +0
-0@@ -1528,6 +1542,16 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, return -OCFS2_FILECHECK_ERR_VALIDFLAG; }
+ if (has_extents(di)&&le16_to_cpu(di->id2.i_list.l_next_free_rec)> +le16_to_cpu(di->id2.i_list.l_count)) {
+ di->id2.i_list.l_next_free_rec =di->id2.i_list.l_count; + changed = 1;
Quoted 8 lines
Quoted 6 lines
+ mlog(ML_ERROR,
+ "Filecheck: reset dinode #%llu:l_next_free_recto %u\n", + (unsigned long long)bh->b_blocknr,
+le16_to_cpu(di->id2.i_list.l_next_free_rec)); + }
+I'd like to move this check behind
fs_generationsince they are commoner check.
Sure, will fix it in next version.
Quoted 8 lines
Thanks,
JosephQuoted 3 lines
if (
le64_to_cpu(di->i_blkno)!=bh->b_blocknr) {di->i_blkno=cpu_to_le64(bh->b_blocknr); changed = 1;
Signature · 1 line
Julian Sun <[email protected]>
Joseph Qi
maintainerQuoted 27 lines · Julian Sun
Hi,
Recently syzbot reported a use-after-free issue[1].
The root cause of the problem is that the journal
inode recorded in this file system image is corrupted. The value of "di->id2.i_list.l_next_free_rec" is 8193, which is greater than the value of "di->id2.i_list.l_count" (19).To solve this problem, an additional check should be added within
ocfs2_get_clusters_nocache(). If the check fails, an error will be returned and the file system will be set to read-only.[1]: https://lore.kernel.org/all/[email protected]/T/
Reported-and-tested-by [email protected]Signed-off-by Julian Sun <[email protected]>--- fs/ocfs2/extent_map.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/ocfs2/extent_map.c +0
-0
fs/ocfs2/extent_map.c static int ocfs2_get_clusters_nocache(struct inode *inode, · line 415
Quoted 2 lines
fs/ocfs2/extent_map.c +0
-0@@ -414,6 +414,16 @@ static int ocfs2_get_clusters_nocache(struct inode *inode, el = &di->id2.i_list; tree_height =le16_to_cpu (el->l_tree_depth);
Move this down close to tree_height check.
fs/ocfs2/extent_map.c static int ocfs2_get_clusters_nocache(struct inode *inode, · line 418
Quoted 3 lines
fs/ocfs2/extent_map.c +2
-0+if (le16_to_cpu (di->id2.i_list.l_next_free_rec) > +le16_to_cpu (di->id2.i_list.l_count)) {
Use le16_to_cpu(el->l_next_free_rec) and le16_to_cpu(el->l_count).
Thanks,
Joseph
Quoted 11 lines
fs/ocfs2/extent_map.c +8
-0+ ret =ocfs2_error (inode->i_sb,"Invalid dinode #%llu: next_free_rec is %u, count is %u\n" , + (unsigned long long )di_bh->b_blocknr, +le16_to_cpu (di->id2.i_list.l_next_free_rec), +le16_to_cpu (di->id2.i_list.l_count)); + +return ret; + } +if (tree_height >0 ) { ret =ocfs2_find_leaf (INODE_CACHE (inode), el, v_cluster, &eb_bh);