xfs: fix ag count overflow during growfs
PATCH 1/1 xfs: fix ag count overflow during growfs
I found a corruption during growfs:
XFS (loop0): Internal error agbno >= mp->m_sb.sb_agblocks at line 3661 of
file fs/xfs/libxfs/xfs_alloc.c. Caller __xfs_free_extent+0x28e/0x3c0
CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_corruption_error+0x134/0x150 __xfs_free_extent+0x2c1/0x3c0 xfs_ag_extend_space+0x291/0x3e0 xfs_growfs_data+0xd72/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd XFS (loop0): Corruption detected. Unmount and run xfs_repair XFS (loop0): Internal error xfs_trans_cancel at line 1097 of file fs/xfs/xfs_trans.c. Caller xfs_growfs_data+0x691/0xe90 CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_error_report+0x93/0xc0 xfs_trans_cancel+0x2c0/0x350 xfs_growfs_data+0x691/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x7f2d86706577
The bug can be reproduced with the following sequence:
# truncate -s 1073741824 xfs_test.img # mkfs.xfs -f -b size=1024 -d agcount=4 xfs_test.img # truncate -s 2305843009213693952 xfs_test.img # mount -o loop xfs_test.img /mnt/test # xfs_growfs -D 1125899907891200 /mnt/test
The root cause is that during growfs, user space passed in a large value of newblcoks to xfs_growfs_data_private(), due to current sb_agblocks is too small, new AG count will exceed UINT_MAX. Because of AG number type is unsigned int and it would overflow, that caused nagcount much smaller than the actual value. During AG extent space, delta blocks in xfs_resizefs_init_new_ags() will much larger than the actual value due to incorrect nagcount, even exceed UINT_MAX. This will cause corruption and be detected in __xfs_free_extent. Fix it by add checks for AG number that should not greater than or equal to NULLAGNUMBER before growfs and mount filesystem.
--- fs/xfs/xfs_fsops.c | 2 +- fs/xfs/xfs_mount.c | 6 +++++- fs/xfs/xfs_mount.h | 2 +- fs/xfs/xfs_rtalloc.c | 2 +- fs/xfs/xfs_super.c | 4 ++-- 5 files changed, 10 insertions(+), 6 deletions(-)
fs/xfs/xfs_fsops.c +1 -1
@@ -100,7 +100,7 @@ xfs_growfs_data_private(struct xfs_perag *last_pag; nb = in->newblocks; - error =xfs_sb_validate_fsb_count (&mp->m_sb, nb); + error =xfs_sb_validate_fsb_count (&mp->m_sb, nb, true);if (error)return error;
fs/xfs/xfs_mount.c +5 -1
@@ -128,7 +128,8 @@ xfs_uuid_unmount(int xfs_sb_validate_fsb_count (xfs_sb_t *sbp, -uint64_t nblocks) +uint64_t nblocks, +bool dblock) {ASSERT (PAGE_SHIFT >= sbp->sb_blocklog);ASSERT (sbp->sb_blocklog >=BBSHIFT );
@@ -136,6 +137,9 @@ xfs_sb_validate_fsb_count(/* Limited by ULONG_MAX of page cache index */ if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) >ULONG_MAX )return -EFBIG ; +/* Limited by NULLAGNUMBER of ag number */ +if (dblock && (nblocks >> sbp->sb_agblklog) >=NULLAGNUMBER ) +return -EFBIG ;return 0 ; }
fs/xfs/xfs_mount.h +1 -1
@@ -531,7 +531,7 @@ xfs_mod_frextents(struct xfs_mount *mp, int64_t delta)extern int xfs_readsb (xfs_mount_t *,int );extern void xfs_freesb (xfs_mount_t *);extern bool xfs_fs_writable (struct xfs_mount *mp,int level); -extern int xfs_sb_validate_fsb_count (struct xfs_sb *,uint64_t ); +extern int xfs_sb_validate_fsb_count (struct xfs_sb *,uint64_t ,bool );extern int xfs_dev_is_read_only (struct xfs_mount *,char *);
fs/xfs/xfs_rtalloc.c +1 -1
@@ -958,7 +958,7 @@ xfs_growfs_rt(return -EOPNOTSUPP ; nrblocks = in->newblocks; - error =xfs_sb_validate_fsb_count (sbp, nrblocks); + error =xfs_sb_validate_fsb_count (sbp, nrblocks, false);if (error)return error;/*
fs/xfs/xfs_super.c +2 -2
@@ -1592,8 +1592,8 @@ xfs_fs_fill_super( }/* Ensure this filesystem fits in the page cache limits */ -if (xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_dblocks) || -xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_rblocks)) { +if (xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_dblocks, true) || +xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_rblocks, false)) {xfs_warn (mp,"file system too large to be mounted on this system." ); error = -EFBIG ;
Signature · 1 line
2.31.1
Quoted 88 lines · Long Li
I found a corruption during growfs:
XFS (loop0): Internal error agbno >=
mp->m_sb.sb_agblocksat line 3661 of
filefs/xfs/libxfs/xfs_alloc.c. Caller__xfs_free_extent+0x28e/0x3c0CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_corruption_error+0x134/0x150 __xfs_free_extent+0x2c1/0x3c0 xfs_ag_extend_space+0x291/0x3e0 xfs_growfs_data+0xd72/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd XFS (loop0): Corruption detected. Unmount and run xfs_repair XFS (loop0): Internal error xfs_trans_cancel at line 1097 of file fs/xfs/xfs_trans.c. Caller xfs_growfs_data+0x691/0xe90 CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_error_report+0x93/0xc0 xfs_trans_cancel+0x2c0/0x350 xfs_growfs_data+0x691/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x7f2d86706577The bug can be reproduced with the following sequence:
# truncate -s 1073741824 xfs_test.img # mkfs.xfs -f -b size=1024 -d agcount=4 xfs_test.img # truncate -s 2305843009213693952 xfs_test.img # mount -o loop xfs_test.img /mnt/test # xfs_growfs -D 1125899907891200 /mnt/testThe root cause is that during growfs, user space passed in a large value of newblcoks to
xfs_growfs_data_private(), due to currentsb_agblocksis too small, new AG count will exceedUINT_MAX. Because of AG number type is unsigned int and it would overflow, that caused nagcount much smaller than the actual value. During AG extent space, delta blocks inxfs_resizefs_init_new_ags()will much larger than the actual value due to incorrect nagcount, even exceedUINT_MAX. This will cause corruption and be detected in__xfs_free_extent. Fix it by add checks for AG number that should not greater than or equal to NULLAGNUMBER before growfs and mount filesystem.Signed-off-by Long Li <[email protected]>--- fs/xfs/xfs_fsops.c | 2 +- fs/xfs/xfs_mount.c | 6 +++++- fs/xfs/xfs_mount.h | 2 +- fs/xfs/xfs_rtalloc.c | 2 +- fs/xfs/xfs_super.c | 4 ++-- 5 files changed, 10 insertions(+), 6 deletions(-)fs/xfs/xfs_fsops.c +1
-1@@ -100,7 +100,7 @@ xfs_growfs_data_private(struct xfs_perag *last_pag; nb = in->newblocks; - error =xfs_sb_validate_fsb_count (&mp->m_sb, nb); + error =xfs_sb_validate_fsb_count (&mp->m_sb, nb, true);if (error)return error;fs/xfs/xfs_mount.c +2
-1@@ -128,7 +128,8 @@ xfs_uuid_unmount(int xfs_sb_validate_fsb_count (xfs_sb_t *sbp, -uint64_t nblocks) +uint64_t nblocks, +bool dblock) {ASSERT (PAGE_SHIFT >= sbp->sb_blocklog);ASSERT (sbp->sb_blocklog >=BBSHIFT );
fs/xfs/xfs_mount.c xfs_sb_validate_fsb_count( · line 142
Quoted 6 lines
fs/xfs/xfs_mount.c +3
-0@@ -136,6 +137,9 @@ xfs_sb_validate_fsb_count(/* Limited by ULONG_MAX of page cache index */ if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) >ULONG_MAX )return -EFBIG ; +/* Limited by NULLAGNUMBER of ag number */ +if (dblock && (nblocks >> sbp->sb_agblklog) >=NULLAGNUMBER ) +return -EFBIG ;
I think this should be a separate predicate to check for overflowing agcount in xfs_validate_sb_common and xfs_growfs_data_private.
I also wonder if @agcount in xfs_validate_sb_common needs to be u64 (and not u32) to handle overflows?
Someone should try fuzzing a filesystem with a small agblklog and a large dblocks to see if one can trip an integer overflow in the superblock verifier.
--D
Quoted 47 lines
fs/xfs/xfs_mount.c +0
-0return 0 ; }fs/xfs/xfs_mount.h +1
-1@@ -531,7 +531,7 @@ xfs_mod_frextents(struct xfs_mount *mp, int64_t delta)extern int xfs_readsb (xfs_mount_t *,int );extern void xfs_freesb (xfs_mount_t *);extern bool xfs_fs_writable (struct xfs_mount *mp,int level); -extern int xfs_sb_validate_fsb_count (struct xfs_sb *,uint64_t ); +extern int xfs_sb_validate_fsb_count (struct xfs_sb *,uint64_t ,bool );extern int xfs_dev_is_read_only (struct xfs_mount *,char *);fs/xfs/xfs_rtalloc.c +1
-1@@ -958,7 +958,7 @@ xfs_growfs_rt(return -EOPNOTSUPP ; nrblocks = in->newblocks; - error =xfs_sb_validate_fsb_count (sbp, nrblocks); + error =xfs_sb_validate_fsb_count (sbp, nrblocks, false);if (error)return error;/* fs/xfs/xfs_super.c +2
-2@@ -1592,8 +1592,8 @@ xfs_fs_fill_super( }/* Ensure this filesystem fits in the page cache limits */ -if (xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_dblocks) || -xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_rblocks)) { +if (xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_dblocks, true) || +xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_rblocks, false)) {xfs_warn (mp,"file system too large to be mounted on this system." ); error = -EFBIG ;Signature · 1 line
2.31.1
Quoted 90 lines · Darrick J. Wong
Quoted 89 lines · Long Li
I found a corruption during growfs:
XFS (loop0): Internal error agbno >=
mp->m_sb.sb_agblocksat line 3661 of
filefs/xfs/libxfs/xfs_alloc.c. Caller__xfs_free_extent+0x28e/0x3c0CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_corruption_error+0x134/0x150 __xfs_free_extent+0x2c1/0x3c0 xfs_ag_extend_space+0x291/0x3e0 xfs_growfs_data+0xd72/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd XFS (loop0): Corruption detected. Unmount and run xfs_repair XFS (loop0): Internal error xfs_trans_cancel at line 1097 of file fs/xfs/xfs_trans.c. Caller xfs_growfs_data+0x691/0xe90 CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_error_report+0x93/0xc0 xfs_trans_cancel+0x2c0/0x350 xfs_growfs_data+0x691/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x7f2d86706577The bug can be reproduced with the following sequence:
# truncate -s 1073741824 xfs_test.img # mkfs.xfs -f -b size=1024 -d agcount=4 xfs_test.img # truncate -s 2305843009213693952 xfs_test.img # mount -o loop xfs_test.img /mnt/test # xfs_growfs -D 1125899907891200 /mnt/testThe root cause is that during growfs, user space passed in a large value of newblcoks to
xfs_growfs_data_private(), due to currentsb_agblocksis too small, new AG count will exceedUINT_MAX. Because of AG number type is unsigned int and it would overflow, that caused nagcount much smaller than the actual value. During AG extent space, delta blocks inxfs_resizefs_init_new_ags()will much larger than the actual value due to incorrect nagcount, even exceedUINT_MAX. This will cause corruption and be detected in__xfs_free_extent. Fix it by add checks for AG number that should not greater than or equal to NULLAGNUMBER before growfs and mount filesystem.Signed-off-by Long Li <[email protected]>--- fs/xfs/xfs_fsops.c | 2 +- fs/xfs/xfs_mount.c | 6 +++++- fs/xfs/xfs_mount.h | 2 +- fs/xfs/xfs_rtalloc.c | 2 +- fs/xfs/xfs_super.c | 4 ++-- 5 files changed, 10 insertions(+), 6 deletions(-)fs/xfs/xfs_fsops.c +1
-1@@ -100,7 +100,7 @@ xfs_growfs_data_private(struct xfs_perag *last_pag; nb = in->newblocks; - error =xfs_sb_validate_fsb_count (&mp->m_sb, nb); + error =xfs_sb_validate_fsb_count (&mp->m_sb, nb, true);if (error)return error;fs/xfs/xfs_mount.c +2
-1@@ -128,7 +128,8 @@ xfs_uuid_unmount(int xfs_sb_validate_fsb_count (xfs_sb_t *sbp, -uint64_t nblocks) +uint64_t nblocks, +bool dblock) {ASSERT (PAGE_SHIFT >= sbp->sb_blocklog);ASSERT (sbp->sb_blocklog >=BBSHIFT );@@ -136,6 +137,9 @@ xfs_sb_validate_fsb_count(/* Limited by ULONG_MAX of page cache index */
Quoted 9 lines
fs/xfs/xfs_mount.c xfs_sb_validate_fsb_count( · line 142
Quoted 5 lines
fs/xfs/xfs_mount.c +3
-0if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) >ULONG_MAX )return -EFBIG ; +/* Limited by NULLAGNUMBER of ag number */ +if (dblock && (nblocks >> sbp->sb_agblklog) >=NULLAGNUMBER ) +return -EFBIG ;I think this should be a separate predicate to check for overflowing agcount in
xfs_validate_sb_commonandxfs_growfs_data_private.
Ok, the next version will be changed.
Quoted 2 lines
I also wonder if @agcount in
xfs_validate_sb_commonneeds to be u64 (and not u32) to handle overflows?
It looks like there is no need for @agcount overflow checking in xfs_validate_sb_common:
If agcount overflow occurs, the flollowing judgment will be true and SB sanity check failed.
sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)
So the check for overflowing of agcount should only need in xfs_growfs_data_private().
Thanks,
Long Li
Quoted 53 lines
Someone should try fuzzing a filesystem with a small agblklog and a large dblocks to see if one can trip an integer overflow in the superblock verifier.
--D
Quoted 47 lines
fs/xfs/xfs_mount.c +0
-0return 0 ; }fs/xfs/xfs_mount.h +1
-1@@ -531,7 +531,7 @@ xfs_mod_frextents(struct xfs_mount *mp, int64_t delta)extern int xfs_readsb (xfs_mount_t *,int );extern void xfs_freesb (xfs_mount_t *);extern bool xfs_fs_writable (struct xfs_mount *mp,int level); -extern int xfs_sb_validate_fsb_count (struct xfs_sb *,uint64_t ); +extern int xfs_sb_validate_fsb_count (struct xfs_sb *,uint64_t ,bool );extern int xfs_dev_is_read_only (struct xfs_mount *,char *);fs/xfs/xfs_rtalloc.c +1
-1@@ -958,7 +958,7 @@ xfs_growfs_rt(return -EOPNOTSUPP ; nrblocks = in->newblocks; - error =xfs_sb_validate_fsb_count (sbp, nrblocks); + error =xfs_sb_validate_fsb_count (sbp, nrblocks, false);if (error)return error;/* fs/xfs/xfs_super.c +2
-2@@ -1592,8 +1592,8 @@ xfs_fs_fill_super( }/* Ensure this filesystem fits in the page cache limits */ -if (xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_dblocks) || -xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_rblocks)) { +if (xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_dblocks, true) || +xfs_sb_validate_fsb_count (&mp->m_sb, mp->m_sb.sb_rblocks, false)) {xfs_warn (mp,"file system too large to be mounted on this system." ); error = -EFBIG ;Signature · 1 line
2.31.1