ocfs2: validate suballoc slot and bit of metadata blocks
Joseph Qi
maintainercover ocfs2: validate suballoc slot and bit of metadata blocks
ocfs2 metadata validators trust the on-disk suballoc slot (and the dinode suballoc bit) without checking them against the mounted filesystem's slot range. A corrupted image can carry OCFS2_INVALID_SLOT or another out-of-range value; when the corresponding inode, extent block, xattr block, or dir index root is later freed, the unvalidated slot/bit is passed to ocfs2_get_system_file_inode() or _ocfs2_free_suballoc_bits() and hits BUG_ON() or indexes local_system_inodes[] out of bounds.
Mounting ocfs2 requires privilege (ocfs2 cannot be mounted from a user namespace), so this needs a privileged mount of a crafted or corrupted image -- e.g. an administrator mounting untrusted media or a disk image supplied to a virtual machine. Once such an image is mounted, an unprivileged user who can delete the corrupted object triggers the crash.
This series rejects such values at read time, in the existing validators, so corrupted objects fail with -EROFS (and a read-only remount) instead of crashing:
patch 1 restricts OCFS2_INVALID_SLOT dinodes to system inodes,
completing fe7a283b3916 ("ocfs2: add suballoc slot check in ocfs2_validate_inode_block()");
patch 2 rejects oversized dinode suballoc bits;
patch 3 validates the suballoc slot of xattr and dir index blocks; patch 4 validates the suballoc slot of extent blocks.
All checks match what the kernel itself writes (always a valid slot from meta_ac->ac_alloc_slot) and what mkfs.ocfs2/libocfs2 write (system inodes carry OCFS2_INVALID_SLOT with OCFS2_SYSTEM_FL, extent blocks carry slot 0), so legitimate filesystems are unaffected.
Tested with negative corruption testing under QEMU: for each new check a test image was generated with the field overwritten by an out-of-range value; with the series applied the access fails cleanly with -EROFS and the filesystem remounts read-only instead of hitting BUG_ON().
Joseph Qi (4):
ocfs2: restrict OCFS2_INVALID_SLOT suballoc slot to system inodes
ocfs2: validate suballoc bit during inode read
ocfs2: validate suballoc slot of xattr and dir index blocks
ocfs2: validate suballoc slot of extent blocks
fs/ocfs2/alloc.c | 14 ++++++++++++++ fs/ocfs2/dir.c | 14 ++++++++++++++ fs/ocfs2/inode.c | 33 +++++++++++++++++++++++++++++++-- fs/ocfs2/xattr.c | 13 +++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.3
Joseph Qi
maintainerPATCH 1/4 ocfs2: restrict OCFS2_INVALID_SLOT suballoc slot to system inodes
ocfs2_validate_inode_block() currently permits i_suballoc_slot to be OCFS2_INVALID_SLOT for any dinode. Only system inodes created by mkfs.ocfs2 are allocated from the global allocator and thus legitimately carry this value; regular inodes are always allocated from a per-slot suballocator and hence must have a valid slot.
If a corrupted regular inode with OCFS2_INVALID_SLOT is accepted, ocfs2_remove_inode() will pass the slot to ocfs2_get_system_file_inode() and get_local_system_inode() will hit BUG_ON(slot == OCFS2_INVALID_SLOT) when the inode is deleted. This can be triggered by an unprivileged user unlinking such a corrupted file.
Reject OCFS2_INVALID_SLOT for non-system dinodes during validation, while still accepting it for system inodes.
--- fs/ocfs2/inode.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +17 -2
@@ -1520,8 +1520,23 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } -if (le16_to_cpu (di->i_suballoc_slot) != (u16 )OCFS2_INVALID_SLOT && - (u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { +/* +* Only system inodes created by mkfs.ocfs2 are allocated from the +* global allocator and thus legitimately carry OCFS2_INVALID_SLOT. +* Regular inodes are always allocated from a per-slot suballocator. +* If a regular inode with OCFS2_INVALID_SLOT was accepted here, +* deleting it would pass the slot to get_local_system_inode() via +* ocfs2_remove_inode() and trigger BUG_ON(slot == OCFS2_INVALID_SLOT). +*/ +if (le16_to_cpu (di->i_suballoc_slot) == (u16 )OCFS2_INVALID_SLOT ) { +if (!(le32_to_cpu (di->i_flags) &OCFS2_SYSTEM_FL )) { + rc =ocfs2_error (sb, +"Invalid dinode %llu: suballoc slot %u for non-system inode\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->i_suballoc_slot)); + goto bail; + } + }else if ((u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { rc =ocfs2_error (sb,"Invalid dinode %llu: suballoc slot %u\n" , (unsigned long long )bh->b_blocknr,le16_to_cpu (di->i_suballoc_slot));
Signature · 1 line
2.39.3
Re: [PATCH 1/4] ocfs2: restrict OCFS2_INVALID_SLOT suballoc slot to system inodes
Quoted 12 lines · Joseph Qi
ocfs2_validate_inode_block()currently permitsi_suballoc_slotto beOCFS2_INVALID_SLOTfor any dinode. Only system inodes created by mkfs.ocfs2 are allocated from the global allocator and thus legitimately carry this value; regular inodes are always allocated from a per-slot suballocator and hence must have a valid slot.If a corrupted regular inode with
OCFS2_INVALID_SLOTis accepted,ocfs2_remove_inode()will pass the slot toocfs2_get_system_file_inode()andget_local_system_inode()will hitBUG_ON(slot == OCFS2_INVALID_SLOT)when the inode is deleted. This can be triggered by an unprivileged user unlinking such a corrupted file.
Quoted 6 lines
Reject
OCFS2_INVALID_SLOTfor non-system dinodes during validation, while still accepting it for system inodes.Fixes fe7a283b3916 ("ocfs2: add suballoc slot check in ocfs2_validate_inode_block()")Signed-off-by Joseph Qi <[email protected]>
LGTM.
Quoted 37 lines
--- fs/ocfs2/inode.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)fs/ocfs2/inode.c +17
-2@@ -1520,8 +1520,23 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } -if (le16_to_cpu (di->i_suballoc_slot) != (u16 )OCFS2_INVALID_SLOT && - (u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { +/* +* Only system inodes created by mkfs.ocfs2 are allocated from the +* global allocator and thus legitimately carry OCFS2_INVALID_SLOT. +* Regular inodes are always allocated from a per-slot suballocator. +* If a regular inode with OCFS2_INVALID_SLOT was accepted here, +* deleting it would pass the slot to get_local_system_inode() via +* ocfs2_remove_inode() and trigger BUG_ON(slot == OCFS2_INVALID_SLOT). +*/ +if (le16_to_cpu (di->i_suballoc_slot) == (u16 )OCFS2_INVALID_SLOT ) { +if (!(le32_to_cpu (di->i_flags) &OCFS2_SYSTEM_FL )) { + rc =ocfs2_error (sb, +"Invalid dinode %llu: suballoc slot %u for non-system inode\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->i_suballoc_slot)); + goto bail; + } + }else if ((u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { rc =ocfs2_error (sb,"Invalid dinode %llu: suballoc slot %u\n" , (unsigned long long )bh->b_blocknr,le16_to_cpu (di->i_suballoc_slot));Signature · 1 line
2.39.3
Joseph Qi
maintainerRe: [PATCH 1/4] ocfs2: restrict OCFS2_INVALID_SLOT suballoc slot to system inodes
Quoted 16 lines · Heming Zhao
Quoted 15 lines · Joseph Qi
ocfs2_validate_inode_block()currently permitsi_suballoc_slotto beOCFS2_INVALID_SLOTfor any dinode. Only system inodes created by mkfs.ocfs2 are allocated from the global allocator and thus legitimately carry this value; regular inodes are always allocated from a per-slot suballocator and hence must have a valid slot.If a corrupted regular inode with
OCFS2_INVALID_SLOTis accepted,ocfs2_remove_inode()will pass the slot toocfs2_get_system_file_inode()andget_local_system_inode()will hitBUG_ON(slot == OCFS2_INVALID_SLOT)when the inode is deleted. This can be triggered by an unprivileged user unlinking such a corrupted file.Reject
OCFS2_INVALID_SLOTfor non-system dinodes during validation, while still accepting it for system inodes.
Quoted 6 lines
Quoted 3 lines
Fixes fe7a283b3916 ("ocfs2: add suballoc slot check in ocfs2_validate_inode_block()")Signed-off-by Joseph Qi <[email protected]>LGTM.
Reviewed-by Heming Zhao <[email protected]>
Thanks, sashiko has some review comments, I'll fix them and send v2 later.
Joseph
Quoted 37 lines
Quoted 37 lines
--- fs/ocfs2/inode.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)fs/ocfs2/inode.c +17
-2@@ -1520,8 +1520,23 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } -if (le16_to_cpu (di->i_suballoc_slot) != (u16 )OCFS2_INVALID_SLOT && - (u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { +/* +* Only system inodes created by mkfs.ocfs2 are allocated from the +* global allocator and thus legitimately carry OCFS2_INVALID_SLOT. +* Regular inodes are always allocated from a per-slot suballocator. +* If a regular inode with OCFS2_INVALID_SLOT was accepted here, +* deleting it would pass the slot to get_local_system_inode() via +* ocfs2_remove_inode() and trigger BUG_ON(slot == OCFS2_INVALID_SLOT). +*/ +if (le16_to_cpu (di->i_suballoc_slot) == (u16 )OCFS2_INVALID_SLOT ) { +if (!(le32_to_cpu (di->i_flags) &OCFS2_SYSTEM_FL )) { + rc =ocfs2_error (sb, +"Invalid dinode %llu: suballoc slot %u for non-system inode\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->i_suballoc_slot)); + goto bail; + } + }else if ((u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { rc =ocfs2_error (sb,"Invalid dinode %llu: suballoc slot %u\n" , (unsigned long long )bh->b_blocknr,le16_to_cpu (di->i_suballoc_slot));Signature · 1 line
2.39.3
Heming Zhao
reviewerRe: [PATCH 1/4] ocfs2: restrict OCFS2_INVALID_SLOT suballoc slot to system inodes
Quoted 23 lines · Joseph Qi
Quoted 20 lines · Heming Zhao
Quoted 18 lines · Joseph Qi
ocfs2_validate_inode_block()currently permitsi_suballoc_slotto beOCFS2_INVALID_SLOTfor any dinode. Only system inodes created by mkfs.ocfs2 are allocated from the global allocator and thus legitimately carry this value; regular inodes are always allocated from a per-slot suballocator and hence must have a valid slot.If a corrupted regular inode with
OCFS2_INVALID_SLOTis accepted,ocfs2_remove_inode()will pass the slot toocfs2_get_system_file_inode()andget_local_system_inode()will hitBUG_ON(slot == OCFS2_INVALID_SLOT)when the inode is deleted. This can be triggered by an unprivileged user unlinking such a corrupted file.Reject
OCFS2_INVALID_SLOTfor non-system dinodes during validation, while still accepting it for system inodes.Fixes fe7a283b3916 ("ocfs2: add suballoc slot check in ocfs2_validate_inode_block()")Signed-off-by Joseph Qi <[email protected]>
Quoted 6 lines
Quoted 2 lines
LGTM.
Reviewed-by Heming Zhao <[email protected]>Thanks, sashiko has some review comments, I'll fix them and send v2 later.
Joseph
It seems sashiko review comments only go to the patch submitter/author. In my view, they're also sent to [email protected], which is nice as it lets others on the list see them."
- Heming
Quoted 39 lines
Quoted 38 lines
Quoted 38 lines
--- fs/ocfs2/inode.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)fs/ocfs2/inode.c +17
-2@@ -1520,8 +1520,23 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } -if (le16_to_cpu (di->i_suballoc_slot) != (u16 )OCFS2_INVALID_SLOT && - (u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { +/* +* Only system inodes created by mkfs.ocfs2 are allocated from the +* global allocator and thus legitimately carry OCFS2_INVALID_SLOT. +* Regular inodes are always allocated from a per-slot suballocator. +* If a regular inode with OCFS2_INVALID_SLOT was accepted here, +* deleting it would pass the slot to get_local_system_inode() via +* ocfs2_remove_inode() and trigger BUG_ON(slot == OCFS2_INVALID_SLOT). +*/ +if (le16_to_cpu (di->i_suballoc_slot) == (u16 )OCFS2_INVALID_SLOT ) { +if (!(le32_to_cpu (di->i_flags) &OCFS2_SYSTEM_FL )) { + rc =ocfs2_error (sb, +"Invalid dinode %llu: suballoc slot %u for non-system inode\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->i_suballoc_slot)); + goto bail; + } + }else if ((u32 )le16_to_cpu (di->i_suballoc_slot) >OCFS2_SB (sb)->max_slots -1 ) { rc =ocfs2_error (sb,"Invalid dinode %llu: suballoc slot %u\n" , (unsigned long long )bh->b_blocknr,le16_to_cpu (di->i_suballoc_slot));Signature · 1 line
2.39.3
Joseph Qi
maintainerRe: [PATCH 1/4] ocfs2: restrict OCFS2_INVALID_SLOT suballoc slot to system inodes
Quoted 27 lines · Heming Zhao
Quoted 26 lines · Joseph Qi
Quoted 22 lines · Heming Zhao
Quoted 12 lines · Joseph Qi
ocfs2_validate_inode_block()currently permitsi_suballoc_slotto beOCFS2_INVALID_SLOTfor any dinode. Only system inodes created by mkfs.ocfs2 are allocated from the global allocator and thus legitimately carry this value; regular inodes are always allocated from a per-slot suballocator and hence must have a valid slot.If a corrupted regular inode with
OCFS2_INVALID_SLOTis accepted,ocfs2_remove_inode()will pass the slot toocfs2_get_system_file_inode()andget_local_system_inode()will hitBUG_ON(slot == OCFS2_INVALID_SLOT)when the inode is deleted. This can be triggered by an unprivileged user unlinking such a corrupted file.Quoted 6 lines
Reject
OCFS2_INVALID_SLOTfor non-system dinodes during validation, while still accepting it for system inodes.Fixes fe7a283b3916 ("ocfs2: add suballoc slot check in ocfs2_validate_inode_block()")Signed-off-by Joseph Qi <[email protected]>LGTM.
Reviewed-by Heming Zhao <[email protected]>
Quoted 8 lines
Quoted 3 lines
Thanks, sashiko has some review comments, I'll fix them and send v2 later.
Joseph
It seems sashiko review comments only go to the patch submitter/author. In my view, they're also sent to [email protected], which is nice as it lets others on the list see them."
Yes, now it only sends to author and maintainers.
I'll send a patch to cc list as well.
Thanks,
Joseph
Joseph Qi
maintainerPATCH 2/4 ocfs2: validate suballoc bit during inode read
In reply to Joseph Qii_suballoc_bit of a dinode is currently not validated at all. A corrupted dinode can carry an abnormally large i_suballoc_bit, which bypasses ocfs2_validate_inode_block(). When the inode is deleted, ocfs2_remove_inode() calls ocfs2_free_dinode(), which passes the unvalidated bit to _ocfs2_free_suballoc_bits() and triggers BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)).
Since suballocator block group bitmaps are contained in a single block, a valid suballoc bit must be smaller than the number of bits per block. Reject oversized i_suballoc_bit values during dinode validation.
--- fs/ocfs2/inode.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/ocfs2/inode.c +14 -0
@@ -1543,6 +1543,20 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } +/* +* A suballocator block group bitmap is contained in a single block, +* so a valid suballoc bit can never exceed the number of bits per +* block. Otherwise deleting the inode will pass the oversized bit +* to _ocfs2_free_suballoc_bits() via ocfs2_free_dinode() and trigger +* BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)). +*/ +if (le16_to_cpu (di->i_suballoc_bit) >= sb->s_blocksize *8 ) { + rc =ocfs2_error (sb,"Invalid dinode %llu: suballoc bit %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->i_suballoc_bit)); + goto bail; + } +if ((le32_to_cpu (di->i_flags) &OCFS2_ORPHANED_FL ) &&le16_to_cpu (di->i_orphaned_slot) >=OCFS2_SB (sb)->max_slots) { rc = ocfs2_error(sb, "Invalid dinode %llu: orphaned slot %u\n",
Signature · 1 line
2.39.3
Re: [PATCH 2/4] ocfs2: validate suballoc bit during inode read
Quoted 7 lines · Joseph Qi
i_suballoc_bitof a dinode is currently not validated at all. A corrupted dinode can carry an abnormally largei_suballoc_bit, which bypassesocfs2_validate_inode_block(). When the inode is deleted,ocfs2_remove_inode()callsocfs2_free_dinode(), which passes the unvalidated bit to_ocfs2_free_suballoc_bits()and triggersBUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)).
Quoted 6 lines
Since suballocator block group bitmaps are contained in a single block, a valid suballoc bit must be smaller than the number of bits per block. Reject oversized
i_suballoc_bitvalues during dinode validation.Signed-off-by Joseph Qi <[email protected]>
LGTM.
Quoted 32 lines
--- fs/ocfs2/inode.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)fs/ocfs2/inode.c +14
-0@@ -1543,6 +1543,20 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } +/* +* A suballocator block group bitmap is contained in a single block, +* so a valid suballoc bit can never exceed the number of bits per +* block. Otherwise deleting the inode will pass the oversized bit +* to _ocfs2_free_suballoc_bits() via ocfs2_free_dinode() and trigger +* BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)). +*/ +if (le16_to_cpu (di->i_suballoc_bit) >= sb->s_blocksize *8 ) { + rc =ocfs2_error (sb,"Invalid dinode %llu: suballoc bit %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (di->i_suballoc_bit)); + goto bail; + } +if ((le32_to_cpu (di->i_flags) &OCFS2_ORPHANED_FL ) &&le16_to_cpu (di->i_orphaned_slot) >=OCFS2_SB (sb)->max_slots) { rc = ocfs2_error(sb, "Invalid dinode %llu: orphaned slot %u\n",Signature · 1 line
2.39.3
Joseph Qi
maintainerPATCH 3/4 ocfs2: validate suballoc slot of xattr and dir index blocks
In reply to Joseph Qiocfs2_validate_xattr_block() and ocfs2_validate_dx_root() do not validate xb_suballoc_slot and dr_suballoc_slot against the mounted filesystem's slot range. Since xattr blocks and dir index root blocks are allocated from a per-slot suballocator at runtime, their suballoc slots must be within range.
Otherwise a corrupted image can carry an out-of-range slot. When the xattr block or dir index is removed, ocfs2_xattr_block_remove() or ocfs2_dx_dir_remove_index() passes the unvalidated slot to ocfs2_get_system_file_inode() and get_local_system_inode() will either hit BUG_ON(slot == OCFS2_INVALID_SLOT) or compute an out-of-bounds index into the local_system_inodes array.
Reject out-of-range suballoc slots during validation.
--- fs/ocfs2/dir.c | 14 ++++++++++++++ fs/ocfs2/xattr.c | 13 +++++++++++++ 2 files changed, 27 insertions(+)
fs/ocfs2/dir.c +14 -0
@@ -605,6 +605,20 @@ static int ocfs2_validate_dx_root(struct super_block *sb, goto bail; } +/* +* Dir index root blocks are allocated from a per-slot suballocator, +* so the slot must be in range. Otherwise removing the index passes +* it to get_local_system_inode(), which hits BUG_ON() for +* OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. +*/ +if ((u32 )le16_to_cpu (dx_root->dr_suballoc_slot) >=OCFS2_SB (sb)->max_slots) { + ret =ocfs2_error (sb, +"Dir Index Root # %llu has invalid dr_suballoc_slot %u\n" , + (unsigned long long )le64_to_cpu (dx_root->dr_blkno), +le16_to_cpu (dx_root->dr_suballoc_slot)); + goto bail; + } +if (!(dx_root->dr_flags &OCFS2_DX_FLAG_INLINE )) {struct ocfs2_extent_list *el = &dx_root->dr_list;
fs/ocfs2/xattr.c +13 -0
@@ -517,6 +517,19 @@ static int ocfs2_validate_xattr_block(struct super_block *sb, le32_to_cpu(xb->xb_fs_generation)); } +/* +* Xattr blocks are allocated from a per-slot suballocator, so the +* slot must be in range. Otherwise freeing the block passes it to +* get_local_system_inode(), which hits BUG_ON() for +* OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. +*/ +if ((u32 )le16_to_cpu (xb->xb_suballoc_slot) >=OCFS2_SB (sb)->max_slots) { +return ocfs2_error (sb, +"Extended attribute block #%llu has an invalid xb_suballoc_slot of %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (xb->xb_suballoc_slot)); + } +if (!(le16_to_cpu (xb->xb_flags) &OCFS2_XATTR_INDEXED )) {size_t region_offset = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
Signature · 1 line
2.39.3
Re: [PATCH 3/4] ocfs2: validate suballoc slot of xattr and dir index blocks
Quoted 13 lines · Joseph Qi
ocfs2_validate_xattr_block()andocfs2_validate_dx_root()do not validatexb_suballoc_slotanddr_suballoc_slotagainst the mounted filesystem's slot range. Since xattr blocks and dir index root blocks are allocated from a per-slot suballocator at runtime, their suballoc slots must be within range.Otherwise a corrupted image can carry an out-of-range slot. When the xattr block or dir index is removed,
ocfs2_xattr_block_remove()orocfs2_dx_dir_remove_index()passes the unvalidated slot toocfs2_get_system_file_inode()andget_local_system_inode()will either hitBUG_ON(slot == OCFS2_INVALID_SLOT)or compute an out-of-bounds index into thelocal_system_inodesarray.
Quoted 3 lines
Reject out-of-range suballoc slots during validation.
Signed-off-by Joseph Qi <[email protected]>
LGTM, thanks for the contribution.
Quoted 57 lines
--- fs/ocfs2/dir.c | 14 ++++++++++++++ fs/ocfs2/xattr.c | 13 +++++++++++++ 2 files changed, 27 insertions(+)fs/ocfs2/dir.c +14
-0@@ -605,6 +605,20 @@ static int ocfs2_validate_dx_root(struct super_block *sb, goto bail; } +/* +* Dir index root blocks are allocated from a per-slot suballocator, +* so the slot must be in range. Otherwise removing the index passes +* it to get_local_system_inode(), which hits BUG_ON() for +* OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. +*/ +if ((u32 )le16_to_cpu (dx_root->dr_suballoc_slot) >=OCFS2_SB (sb)->max_slots) { + ret =ocfs2_error (sb, +"Dir Index Root # %llu has invalid dr_suballoc_slot %u\n" , + (unsigned long long )le64_to_cpu (dx_root->dr_blkno), +le16_to_cpu (dx_root->dr_suballoc_slot)); + goto bail; + } +if (!(dx_root->dr_flags &OCFS2_DX_FLAG_INLINE )) {struct ocfs2_extent_list *el = &dx_root->dr_list;fs/ocfs2/xattr.c +13
-0@@ -517,6 +517,19 @@ static int ocfs2_validate_xattr_block(struct super_block *sb, le32_to_cpu(xb->xb_fs_generation)); } +/* +* Xattr blocks are allocated from a per-slot suballocator, so the +* slot must be in range. Otherwise freeing the block passes it to +* get_local_system_inode(), which hits BUG_ON() for +* OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. +*/ +if ((u32 )le16_to_cpu (xb->xb_suballoc_slot) >=OCFS2_SB (sb)->max_slots) { +return ocfs2_error (sb, +"Extended attribute block #%llu has an invalid xb_suballoc_slot of %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (xb->xb_suballoc_slot)); + } +if (!(le16_to_cpu (xb->xb_flags) &OCFS2_XATTR_INDEXED )) {size_t region_offset = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);Signature · 1 line
2.39.3
Joseph Qi
maintainerPATCH 4/4 ocfs2: validate suballoc slot of extent blocks
In reply to Joseph Qiocfs2_validate_extent_block() does not validate h_suballoc_slot against the mounted filesystem's slot range. Since extent blocks are allocated from a per-slot suballocator at runtime, their suballoc slot must be within range.
Otherwise a corrupted image can carry an out-of-range slot. When the extent block is freed, ocfs2_cache_extent_block_free() caches it and ocfs2_free_cached_blocks() later passes the unvalidated slot to ocfs2_get_system_file_inode(), so get_local_system_inode() will either hit BUG_ON(slot == OCFS2_INVALID_SLOT) or compute an out-of-bounds index into the local_system_inodes array.
Reject out-of-range suballoc slots during validation.
--- fs/ocfs2/alloc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/ocfs2/alloc.c +14 -0
@@ -925,6 +925,20 @@ static int ocfs2_validate_extent_block(struct super_block *sb, goto bail; } +/* +* Extent blocks are allocated from a per-slot suballocator, so the +* slot must be in range. Otherwise freeing the block passes it to +* get_local_system_inode(), which hits BUG_ON() for +* OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. +*/ +if ((u32 )le16_to_cpu (eb->h_suballoc_slot) >=OCFS2_SB (sb)->max_slots) { + rc =ocfs2_error (sb, +"Extent block #%llu has an invalid h_suballoc_slot of %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (eb->h_suballoc_slot)); + goto bail; + } +if (le16_to_cpu (eb->h_list.l_count) !=ocfs2_extent_recs_per_eb (sb)) { rc = ocfs2_error(sb, "Extent block #%llu has invalid l_count %u (expected %u)\n",
Signature · 1 line
2.39.3
Re: [PATCH 4/4] ocfs2: validate suballoc slot of extent blocks
Quoted 12 lines · Joseph Qi
ocfs2_validate_extent_block()does not validateh_suballoc_slotagainst the mounted filesystem's slot range. Since extent blocks are allocated from a per-slot suballocator at runtime, their suballoc slot must be within range.Otherwise a corrupted image can carry an out-of-range slot. When the extent block is freed,
ocfs2_cache_extent_block_free()caches it andocfs2_free_cached_blocks()later passes the unvalidated slot toocfs2_get_system_file_inode(), soget_local_system_inode()will either hitBUG_ON(slot == OCFS2_INVALID_SLOT)or compute an out-of-bounds index into thelocal_system_inodesarray.
Quoted 3 lines
Reject out-of-range suballoc slots during validation.
Signed-off-by Joseph Qi <[email protected]>
LGTM.
Quoted 32 lines
--- fs/ocfs2/alloc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)fs/ocfs2/alloc.c +14
-0@@ -925,6 +925,20 @@ static int ocfs2_validate_extent_block(struct super_block *sb, goto bail; } +/* +* Extent blocks are allocated from a per-slot suballocator, so the +* slot must be in range. Otherwise freeing the block passes it to +* get_local_system_inode(), which hits BUG_ON() for +* OCFS2_INVALID_SLOT or computes an out-of-bounds index otherwise. +*/ +if ((u32 )le16_to_cpu (eb->h_suballoc_slot) >=OCFS2_SB (sb)->max_slots) { + rc =ocfs2_error (sb, +"Extent block #%llu has an invalid h_suballoc_slot of %u\n" , + (unsigned long long )bh->b_blocknr, +le16_to_cpu (eb->h_suballoc_slot)); + goto bail; + } +if (le16_to_cpu (eb->h_list.l_count) !=ocfs2_extent_recs_per_eb (sb)) { rc = ocfs2_error(sb, "Extent block #%llu has invalid l_count %u (expected %u)\n",Signature · 1 line
2.39.3