ocfs2: validate i_blkno and BITMAP_FL consistency in ocfs2_validate_inode_block
PATCH v3 1/1 ocfs2: validate i_blkno and BITMAP_FL consistency in ocfs2_validate_inode_block
ocfs2_is_cluster_bitmap() checks whether an inode is the global bitmap by comparing ip_blkno with osb->bitmap_blkno. A corrupted filesystem can have an inode with i_blkno and OCFS2_BITMAP_FL flag inconsistent:
- i_blkno matches bitmap_blkno but BITMAP_FL is not set, or
- BITMAP_FL is set but i_blkno does not match bitmap_blkno
In either case, ocfs2_is_cluster_bitmap() returns incorrect results, triggering BUG_ON in ocfs2_block_group_search() or ocfs2_cluster_group_search() causing a kernel panic.
Call trace: ocfs2_block_group_search+0x1c7/0x2c0 fs/ocfs2/suballoc.c:1611 ocfs2_search_chain+0x38a/0x1010 fs/ocfs2/suballoc.c:1764 ocfs2_claim_suballoc_bits+0x3a4/0x650 fs/ocfs2/suballoc.c:1978 ocfs2_claim_new_inode+0x95/0x130 fs/ocfs2/suballoc.c:2137 ocfs2_mknod_locked+0x129/0x510 fs/ocfs2/namei.c:568 ocfs2_mknod+0x5c7/0x11d0 fs/ocfs2/namei.c:802 ocfs2_create+0x136/0x170 fs/ocfs2/namei.c:852
Add validation in ocfs2_validate_inode_block() to check that i_blkno and OCFS2_BITMAP_FL are consistent:
- If i_blkno matches bitmap_blkno, BITMAP_FL must be set
- If BITMAP_FL is set, i_blkno must match bitmap_blkno
This catches corrupted inodes early when reading from disk.
---
v3:
- Move validation to ocfs2_validate_inode_block() to catch corruption early when inode is read from disk (Joseph Qi)
- Add check for both directions of inconsistency
v2:
- Fix commit message - ocfs2_is_cluster_bitmap() checks ip_blkno against bitmap_blkno, not a flag (Joseph Qi)
--- fs/ocfs2/inode.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
fs/ocfs2/inode.c +21 -0
@@ -1529,6 +1529,27 @@ int ocfs2_validate_inode_block(struct super_block *sb, } } +/* +* Validate bitmap flag consistency with bitmap_blkno. +* A corrupted inode with mismatched i_blkno and BITMAP_FL would +* cause ocfs2_is_cluster_bitmap() to return incorrect results, +* triggering BUG_ON in ocfs2_block_group_search() or +* ocfs2_cluster_group_search(). +*/ +if (le64_to_cpu (di->i_blkno) ==OCFS2_SB (sb)->bitmap_blkno && + !(di->i_flags &cpu_to_le32 (OCFS2_BITMAP_FL ))) { + rc =ocfs2_error (sb, +"Invalid dinode #%llu: i_blkno matches bitmap but BITMAP flag not set\n" , + (unsigned long long )bh->b_blocknr); + goto bail; + }else if (di->i_flags &cpu_to_le32 (OCFS2_BITMAP_FL ) && +le64_to_cpu (di->i_blkno) !=OCFS2_SB (sb)->bitmap_blkno) { + rc =ocfs2_error (sb, +"Invalid dinode #%llu: BITMAP flag set but i_blkno does not match bitmap\n" , + (unsigned long long )bh->b_blocknr); + goto bail; + } + rc =0 ; bail:
Signature · 1 line
2.43.0
Joseph Qi
maintainerQuoted 58 lines · Deepanshu Kartikey
ocfs2_is_cluster_bitmap()checks whether an inode is the global bitmap by comparingip_blknowithosb->bitmap_blkno. A corrupted filesystem can have an inode withi_blknoandOCFS2_BITMAP_FLflag inconsistent:
-i_blknomatchesbitmap_blknobutBITMAP_FLis not set, or
-BITMAP_FLis set buti_blknodoes not matchbitmap_blknoIn either case,
ocfs2_is_cluster_bitmap()returns incorrect results, triggeringBUG_ONinocfs2_block_group_search()orocfs2_cluster_group_search()causing a kernel panic.Call trace: ocfs2_block_group_search+0x1c7/0x2c0 fs/ocfs2/suballoc.c:1611 ocfs2_search_chain+0x38a/0x1010 fs/ocfs2/suballoc.c:1764 ocfs2_claim_suballoc_bits+0x3a4/0x650 fs/ocfs2/suballoc.c:1978 ocfs2_claim_new_inode+0x95/0x130 fs/ocfs2/suballoc.c:2137 ocfs2_mknod_locked+0x129/0x510 fs/ocfs2/namei.c:568 ocfs2_mknod+0x5c7/0x11d0 fs/ocfs2/namei.c:802 ocfs2_create+0x136/0x170 fs/ocfs2/namei.c:852Add validation in
ocfs2_validate_inode_block()to check thati_blknoandOCFS2_BITMAP_FLare consistent:
- Ifi_blknomatchesbitmap_blkno,BITMAP_FLmust be set
- IfBITMAP_FLis set,i_blknomust matchbitmap_blknoThis catches corrupted inodes early when reading from disk.
Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>---
v3:
- Move validation toocfs2_validate_inode_block()to catch corruption early when inode is read from disk (Joseph Qi)
- Add check for both directions of inconsistencyv2:
- Fix commit message -ocfs2_is_cluster_bitmap()checksip_blknoagainstbitmap_blkno, not a flag (Joseph Qi)--- fs/ocfs2/inode.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)fs/ocfs2/inode.c +5
-0@@ -1529,6 +1529,27 @@ int ocfs2_validate_inode_block(struct super_block *sb, } } +/* +* Validate bitmap flag consistency with bitmap_blkno. +* A corrupted inode with mismatched i_blkno and BITMAP_FL would +* cause ocfs2_is_cluster_bitmap() to return incorrect results, +* triggering BUG_ON in ocfs2_block_group_search() or
fs/ocfs2/inode.c int ocfs2_validate_inode_block(struct super_block *sb, · line 1546
Quoted 10 lines
fs/ocfs2/inode.c +10
-0+* ocfs2_cluster_group_search(). +*/ +if (le64_to_cpu (di->i_blkno) ==OCFS2_SB (sb)->bitmap_blkno && + !(di->i_flags &cpu_to_le32 (OCFS2_BITMAP_FL ))) { + rc =ocfs2_error (sb, +"Invalid dinode #%llu: i_blkno matches bitmap but BITMAP flag not set\n" , + (unsigned long long )bh->b_blocknr); + goto bail; + }else if (di->i_flags &cpu_to_le32 (OCFS2_BITMAP_FL ) && +le64_to_cpu (di->i_blkno) !=OCFS2_SB (sb)->bitmap_blkno) {
Ummm... This is not right.OCFS2_BITMAP_FL is set not only for global bitmap, but also for localalloc, extent alloc, inode alloc, etc.
BTW, OCFS2_BITMAP_FL should be checked along with OCFS2_CHAIN_FL.
Thanks,
Joseph
Quoted 9 lines
fs/ocfs2/inode.c +6
-0+ rc =ocfs2_error (sb, +"Invalid dinode #%llu: BITMAP flag set but i_blkno does not match bitmap\n" , + (unsigned long long )bh->b_blocknr); + goto bail; + } + rc =0 ; bail: