xfs: add a few more verifier tests
PATCH 1/1 xfs: add a few more verifier tests
These were exposed by fsfuzzer runs; without them we fail in various exciting and sometimes convoluted ways when we encounter disk corruption.
Without the MAXLEVELS tests we tend to walk off the end of an array in a loop like this:
for (i = 0; i < cur->bc_nlevels; i++) {
if (cur->bc_bufs[i])Without the dirblklog test we try to allocate more memory than we could possibly hope for and loop forever:
xfs_dabuf_map()
nfsb = mp->m_dir_geo->fsbcount; irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP...
As for the logbsize check, that's the convoluted one.
If logbsize is specified at mount time, it's sanitized in xfs_parseargs; in particular it makes sure that it's not > XLOG_MAX_RECORD_BSIZE.
If not specified at mount time, it comes from the superblock via sb_logsunit; this is limited to 256k at mkfs time as well; it's copied into m_logbsize in xfs_finish_flags().
However, if for some reason the on-disk value is corrupt and too large, nothing catches it. It's a circuitous path, but that size eventually finds its way to places that make the kernel very unhappy, leading to oopses in xlog_pack_data() because we use the size as an index into iclog->ic_data, but the array is not necessarily that big.
Anyway - bounds checking when we read from disk is a good thing!
--
(if this should be separate patches, I can resend if you like)
fs/xfs/libxfs/xfs_alloc.c +4 -0
@@ -2209,6 +2209,10 @@ xfs_agf_verify( be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))return false; +if (!(be32_to_cpu (agf->agf_levels[XFS_BTNUM_BNO ]) <=XFS_BTREE_MAXLEVELS && +be32_to_cpu (agf->agf_levels[XFS_BTNUM_CNT ]) <=XFS_BTREE_MAXLEVELS )) +return false; +/* * during growfs operations, the perag is not fully initialised, * so we can't use it for any useful checking. growfs ensures we can't
fs/xfs/libxfs/xfs_ialloc.c +2 -0
@@ -2051,6 +2051,8 @@ xfs_agi_verify(if (!XFS_AGI_GOOD_VERSION (be32_to_cpu (agi->agi_versionnum)))return false; +if (!(be32_to_cpu (agi->agi_level) <=XFS_BTREE_MAXLEVELS )) +return false;/* * during growfs operations, the perag is not fully initialised, * so we can't use it for any useful checking. growfs ensures we can't
fs/xfs/libxfs/xfs_sb.c +2 -0
@@ -279,11 +279,13 @@ xfs_mount_validate_sb( sbp->sb_blocklog <XFS_MIN_BLOCKSIZE_LOG || sbp->sb_blocklog >XFS_MAX_BLOCKSIZE_LOG || sbp->sb_blocksize != (1 << sbp->sb_blocklog) || + sbp->sb_dirblklog >XFS_MAX_BLOCKSIZE_LOG || sbp->sb_inodesize <XFS_DINODE_MIN_SIZE || sbp->sb_inodesize >XFS_DINODE_MAX_SIZE || sbp->sb_inodelog <XFS_DINODE_MIN_LOG || sbp->sb_inodelog >XFS_DINODE_MAX_LOG || sbp->sb_inodesize != (1 << sbp->sb_inodelog) || + sbp->sb_logsunit >XLOG_MAX_RECORD_BSIZE || sbp->sb_inopblock !=howmany (sbp->sb_blocksize,sbp->sb_inodesize) || (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 1 line
Anyway - bounds checking when we read from disk is a good thing!
Absolutelt!
Looks good modulo a few nitpicks below.
fs/xfs/libxfs/xfs_alloc.c xfs_agf_verify( · line 2214
Quoted 11 lines
fs/xfs/libxfs/xfs_alloc.c +3
-0@@ -2209,6 +2209,10 @@ xfs_agf_verify( be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))return false; +if (!(be32_to_cpu (agf->agf_levels[XFS_BTNUM_BNO ]) <=XFS_BTREE_MAXLEVELS && +be32_to_cpu (agf->agf_levels[XFS_BTNUM_CNT ]) <=XFS_BTREE_MAXLEVELS )) +return false;
Maybe it's just me, but negated numeric comparisms always confuse the hell out of me, why not simply:
if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS) return false; if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS) return false;
fs/xfs/libxfs/xfs_ialloc.c xfs_agi_verify( · line 2055
Quoted 8 lines
fs/xfs/libxfs/xfs_ialloc.c +2
-0@@ -2051,6 +2051,8 @@ xfs_agi_verify(if (!XFS_AGI_GOOD_VERSION (be32_to_cpu (agi->agi_versionnum)))return false; +if (!(be32_to_cpu (agi->agi_level) <=XFS_BTREE_MAXLEVELS )) +return false;
Same here.
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 34 lines · Christoph Hellwig
Quoted 1 line
Anyway - bounds checking when we read from disk is a good thing!
Absolutelt!
Looks good modulo a few nitpicks below.
Reviewed-by Christoph Hellwig <[email protected]>fs/xfs/libxfs/xfs_alloc.c xfs_agf_verify( · line 2214
Quoted 11 lines
fs/xfs/libxfs/xfs_alloc.c +3
-0@@ -2209,6 +2209,10 @@ xfs_agf_verify( be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))return false; +if (!(be32_to_cpu (agf->agf_levels[XFS_BTNUM_BNO ]) <=XFS_BTREE_MAXLEVELS && +be32_to_cpu (agf->agf_levels[XFS_BTNUM_CNT ]) <=XFS_BTREE_MAXLEVELS )) +return false;Maybe it's just me, but negated numeric comparisms always confuse the hell out of me, why not simply:
if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS) return false; if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS) return false;Quoted 6 lines
fs/xfs/libxfs/xfs_ialloc.c +0
-0@@ -2051,6 +2051,8 @@ xfs_agi_verify(if (!XFS_AGI_GOOD_VERSION (be32_to_cpu (agi->agi_versionnum)))return false;
Quoted 4 lines
fs/xfs/libxfs/xfs_ialloc.c xfs_agi_verify( · line 2055
Quoted 2 lines
fs/xfs/libxfs/xfs_ialloc.c +2
-0+if (!(be32_to_cpu (agi->agi_level) <=XFS_BTREE_MAXLEVELS )) +return false;Same here.
yeah; just following the style of the functions as they exist today...
if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&...
dunno. Don't care too much either way, but consistency and all that...
Maybe the "AGF_GOOD_VERSION" required the negation, and it all got lumped together?
Thanks,
-Eric
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
PATCH v2 1/1 xfs: add a few more verifier tests
In reply to Eric SandeenThese were exposed by fsfuzzer runs; without them we fail in various exciting and sometimes convoluted ways when we encounter disk corruption.
Without the MAXLEVELS tests we tend to walk off the end of an array in a loop like this:
for (i = 0; i < cur->bc_nlevels; i++) {
if (cur->bc_bufs[i])Without the dirblklog test we try to allocate more memory than we could possibly hope for and loop forever:
xfs_dabuf_map()
nfsb = mp->m_dir_geo->fsbcount; irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP...
As for the logbsize check, that's the convoluted one.
If logbsize is specified at mount time, it's sanitized in xfs_parseargs; in particular it makes sure that it's not > XLOG_MAX_RECORD_BSIZE.
If not specified at mount time, it comes from the superblock via sb_logsunit; this is limited to 256k at mkfs time as well; it's copied into m_logbsize in xfs_finish_flags().
However, if for some reason the on-disk value is corrupt and too large, nothing catches it. It's a circuitous path, but that size eventually finds its way to places that make the kernel very unhappy, leading to oopses in xlog_pack_data() because we use the size as an index into iclog->ic_data, but the array is not necessarily that big.
Anyway - bounds checking when we read from disk is a good thing!
--
V2: un-obfuscate the btree level tests, removing negated comparisons, per hch's suggestion.
fs/xfs/libxfs/xfs_alloc.c +4 -0
@@ -2209,6 +2209,10 @@ xfs_agf_verify( be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))return false; +if (be32_to_cpu (agf->agf_levels[XFS_BTNUM_BNO ]) >XFS_BTREE_MAXLEVELS || +be32_to_cpu (agf->agf_levels[XFS_BTNUM_CNT ]) >XFS_BTREE_MAXLEVELS ) +return false; +/* * during growfs operations, the perag is not fully initialised, * so we can't use it for any useful checking. growfs ensures we can't
fs/xfs/libxfs/xfs_ialloc.c +2 -0
@@ -2051,6 +2051,8 @@ xfs_agi_verify(if (!XFS_AGI_GOOD_VERSION (be32_to_cpu (agi->agi_versionnum)))return false; +if (be32_to_cpu (agi->agi_level) >XFS_BTREE_MAXLEVELS ) +return false;/* * during growfs operations, the perag is not fully initialised, * so we can't use it for any useful checking. growfs ensures we can't
fs/xfs/libxfs/xfs_sb.c +2 -0
@@ -279,11 +279,13 @@ xfs_mount_validate_sb( sbp->sb_blocklog <XFS_MIN_BLOCKSIZE_LOG || sbp->sb_blocklog >XFS_MAX_BLOCKSIZE_LOG || sbp->sb_blocksize != (1 << sbp->sb_blocklog) || + sbp->sb_dirblklog >XFS_MAX_BLOCKSIZE_LOG || sbp->sb_inodesize <XFS_DINODE_MIN_SIZE || sbp->sb_inodesize >XFS_DINODE_MAX_SIZE || sbp->sb_inodelog <XFS_DINODE_MIN_LOG || sbp->sb_inodelog >XFS_DINODE_MAX_LOG || sbp->sb_inodesize != (1 << sbp->sb_inodelog) || + sbp->sb_logsunit >XLOG_MAX_RECORD_BSIZE || sbp->sb_inopblock !=howmany (sbp->sb_blocksize,sbp->sb_inodesize) || (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Quoted 40 lines · Eric Sandeen
Quoted 34 lines · Christoph Hellwig
Quoted 1 line
Anyway - bounds checking when we read from disk is a good thing!
Absolutelt!
Looks good modulo a few nitpicks below.
Reviewed-by Christoph Hellwig <[email protected]>fs/xfs/libxfs/xfs_alloc.c xfs_agf_verify( · line 2214
Quoted 11 lines
fs/xfs/libxfs/xfs_alloc.c +3
-0@@ -2209,6 +2209,10 @@ xfs_agf_verify( be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))return false; +if (!(be32_to_cpu (agf->agf_levels[XFS_BTNUM_BNO ]) <=XFS_BTREE_MAXLEVELS && +be32_to_cpu (agf->agf_levels[XFS_BTNUM_CNT ]) <=XFS_BTREE_MAXLEVELS )) +return false;Maybe it's just me, but negated numeric comparisms always confuse the hell out of me, why not simply:
if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS) return false; if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS) return false;Quoted 6 lines
fs/xfs/libxfs/xfs_ialloc.c +0
-0@@ -2051,6 +2051,8 @@ xfs_agi_verify(if (!XFS_AGI_GOOD_VERSION (be32_to_cpu (agi->agi_versionnum)))return false;Quoted 4 lines
fs/xfs/libxfs/xfs_ialloc.c xfs_agi_verify( · line 2055
Quoted 2 lines
fs/xfs/libxfs/xfs_ialloc.c +2
-0+if (!(be32_to_cpu (agi->agi_level) <=XFS_BTREE_MAXLEVELS )) +return false;Same here.
Quoted 8 lines
yeah; just following the style of the functions as they exist today...
if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) && XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) && be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&...
dunno. Don't care too much either way, but consistency and all that...
I prefer the metho Christoph suggested - most of the verifiers use that "single check per if statement" pattern because it makes the checks being performed so much easier to read.
Quoted 2 lines
Maybe the "
AGF_GOOD_VERSION" required the negation, and it all got lumped together?
Those should probably be cleaned up - they were done like that originally as a direct transcript from pre-existing code checks to simplify review, not because it was "nice" code.
Cheers,
Dave.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Quoted 34 lines · Eric Sandeen
These were exposed by fsfuzzer runs; without them we fail in various exciting and sometimes convoluted ways when we encounter disk corruption.
Without the MAXLEVELS tests we tend to walk off the end of an array in a loop like this:
for (i = 0; i < cur->bc_nlevels; i++) { if (cur->bc_bufs[i])Without the dirblklog test we try to allocate more memory than we could possibly hope for and loop forever:
xfs_dabuf_map()nfsb = mp->m_dir_geo->fsbcount; irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP...As for the logbsize check, that's the convoluted one.
If logbsize is specified at mount time, it's sanitized in
xfs_parseargs; in particular it makes sure that it's not >XLOG_MAX_RECORD_BSIZE.If not specified at mount time, it comes from the superblock via
sb_logsunit; this is limited to 256k at mkfs time as well; it's copied intom_logbsizeinxfs_finish_flags().However, if for some reason the on-disk value is corrupt and too large, nothing catches it. It's a circuitous path, but that size eventually finds its way to places that make the kernel very unhappy, leading to oopses in
xlog_pack_data()because we use the size as an index intoiclog->ic_data, but the array is not necessarily that big.
Quoted 4 lines
Anyway - bounds checking when we read from disk is a good thing!
Signed-off-by Eric Sandeen <[email protected]>--
Looks good.
Signature · 7 lines
Dave Chinner [email protected] _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs