]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: refactor bmap record validation
authorDarrick J. Wong <darrick.wong@oracle.com>
Wed, 18 Apr 2018 19:46:07 +0000 (14:46 -0500)
committerEric Sandeen <sandeen@redhat.com>
Wed, 18 Apr 2018 19:46:07 +0000 (14:46 -0500)
Source kernel commit: 30b0984d9117dd14c895265886d34335856b712b

Refactor the bmap validator into a more complete helper that looks for
extents that run off the end of the device, overflow into the next AG,
or have invalid flag states.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
include/libxfs.h
libxfs/libxfs_api_defs.h
libxfs/libxfs_priv.h
libxfs/xfs_bmap.c
libxfs/xfs_bmap.h
libxfs/xfs_bmap_btree.h
libxfs/xfs_inode_fork.c

index c5fb396e26af89afd784345fba345a3dea69d031..fbaae089af8f49a20ba48a399346c3f7e78092ca 100644 (file)
@@ -227,6 +227,7 @@ libxfs_bmbt_disk_get_all(
 /* XXX: this is clearly a bug - a shared header needs to export this */
 /* xfs_rtalloc.c */
 int libxfs_rtfree_extent(struct xfs_trans *, xfs_rtblock_t, xfs_extlen_t);
+bool libxfs_verify_rtbno(struct xfs_mount *mp, xfs_rtblock_t rtbno);
 
 /* XXX: need parts of xfs_attr.h in userspace */
 #define LIBXFS_ATTR_ROOT       0x0002  /* use attrs in root namespace */
index 389f480f20a13c1d63e383c679c424850b3dd01e..48cf25be0a600fd1c97df82874bff696390e1624 100644 (file)
@@ -77,6 +77,7 @@
 #define xfs_bunmapi                    libxfs_bunmapi
 #define xfs_bmbt_get_all               libxfs_bmbt_get_all
 #define xfs_rtfree_extent              libxfs_rtfree_extent
+#define xfs_verify_rtbno               libxfs_verify_rtbno
 #define xfs_zero_extent                        libxfs_zero_extent
 
 #define xfs_defer_init                 libxfs_defer_init
index cc19d509d44318dd110f8465057375d244800fb5..9a019b7ef92c049a9dc09650d57f65f5f02c88aa 100644 (file)
@@ -543,6 +543,7 @@ void xfs_inode_verifier_error(struct xfs_inode *ip, int error,
 /* XXX: this is clearly a bug - a shared header needs to export this */
 /* xfs_rtalloc.c */
 int libxfs_rtfree_extent(struct xfs_trans *, xfs_rtblock_t, xfs_extlen_t);
+bool libxfs_verify_rtbno(struct xfs_mount *mp, xfs_rtblock_t rtbno);
 
 struct xfs_rtalloc_rec {
        xfs_rtblock_t           ar_startblock;
index afc569cf40a51616cf431dabbcda7b4e05fec02a..eca3e3da1063bfa1d58591e6fa783bd4c2fa89ff 100644 (file)
@@ -1252,11 +1252,15 @@ xfs_iread_extents(
                 */
                frp = XFS_BMBT_REC_ADDR(mp, block, 1);
                for (j = 0; j < num_recs; j++, frp++, i++) {
+                       xfs_failaddr_t  fa;
+
                        xfs_bmbt_disk_get_all(frp, &new);
-                       if (!xfs_bmbt_validate_extent(mp, whichfork, &new)) {
-                               XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
-                                                XFS_ERRLEVEL_LOW, mp);
+                       fa = xfs_bmap_validate_extent(ip, whichfork, &new);
+                       if (fa) {
                                error = -EFSCORRUPTED;
+                               xfs_inode_verifier_error(ip, error,
+                                               "xfs_iread_extents(2)",
+                                               frp, sizeof(*frp), fa);
                                goto out_brelse;
                        }
                        xfs_iext_insert(ip, &icur, &new, state);
@@ -6145,3 +6149,39 @@ xfs_bmap_finish_one(
 
        return error;
 }
+
+/* Check that an inode's extent does not have invalid flags or bad ranges. */
+xfs_failaddr_t
+xfs_bmap_validate_extent(
+       struct xfs_inode        *ip,
+       int                     whichfork,
+       struct xfs_bmbt_irec    *irec)
+{
+       struct xfs_mount        *mp = ip->i_mount;
+       xfs_fsblock_t           endfsb;
+       bool                    isrt;
+
+       isrt = XFS_IS_REALTIME_INODE(ip);
+       endfsb = irec->br_startblock + irec->br_blockcount - 1;
+       if (isrt) {
+               if (!xfs_verify_rtbno(mp, irec->br_startblock))
+                       return __this_address;
+               if (!xfs_verify_rtbno(mp, endfsb))
+                       return __this_address;
+       } else {
+               if (!xfs_verify_fsbno(mp, irec->br_startblock))
+                       return __this_address;
+               if (!xfs_verify_fsbno(mp, endfsb))
+                       return __this_address;
+               if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
+                   XFS_FSB_TO_AGNO(mp, endfsb))
+                       return __this_address;
+       }
+       if (irec->br_state != XFS_EXT_NORM) {
+               if (whichfork != XFS_DATA_FORK)
+                       return __this_address;
+               if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
+                       return __this_address;
+       }
+       return NULL;
+}
index e36d75799cd564f0b327e68285b38e9733333a72..f3be6416260bb79c005a57e0de5fdca7a750557c 100644 (file)
@@ -274,4 +274,7 @@ static inline int xfs_bmap_fork_to_state(int whichfork)
        }
 }
 
+xfs_failaddr_t xfs_bmap_validate_extent(struct xfs_inode *ip, int whichfork,
+               struct xfs_bmbt_irec *irec);
+
 #endif /* __XFS_BMAP_H__ */
index 135b8c56d23e83c492812982120a093bf7cc5527..e4505746ccaa86489b0a96894ff11778253eeb45 100644 (file)
@@ -118,18 +118,4 @@ extern int xfs_bmbt_change_owner(struct xfs_trans *tp, struct xfs_inode *ip,
 extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
                struct xfs_trans *, struct xfs_inode *, int);
 
-/*
- * Check that the extent does not contain an invalid unwritten extent flag.
- */
-static inline bool xfs_bmbt_validate_extent(struct xfs_mount *mp, int whichfork,
-               struct xfs_bmbt_irec *irec)
-{
-       if (irec->br_state == XFS_EXT_NORM)
-               return true;
-       if (whichfork == XFS_DATA_FORK &&
-           xfs_sb_version_hasextflgbit(&mp->m_sb))
-               return true;
-       return false;
-}
-
 #endif /* __XFS_BMAP_BTREE_H__ */
index d75db5bdbbbb751b1972acf1b5583fd53c96078b..dd622d7be0a2eb78ae8df4ff6268e94050d61542 100644 (file)
@@ -242,10 +242,14 @@ xfs_iformat_extents(
 
                xfs_iext_first(ifp, &icur);
                for (i = 0; i < nex; i++, dp++) {
+                       xfs_failaddr_t  fa;
+
                        xfs_bmbt_disk_get_all(dp, &new);
-                       if (!xfs_bmbt_validate_extent(mp, whichfork, &new)) {
-                               XFS_ERROR_REPORT("xfs_iformat_extents(2)",
-                                                XFS_ERRLEVEL_LOW, mp);
+                       fa = xfs_bmap_validate_extent(ip, whichfork, &new);
+                       if (fa) {
+                               xfs_inode_verifier_error(ip, -EFSCORRUPTED,
+                                               "xfs_iformat_extents(2)",
+                                               dp, sizeof(*dp), fa);
                                return -EFSCORRUPTED;
                        }
 
@@ -592,7 +596,7 @@ xfs_iextents_copy(
        for_each_xfs_iext(ifp, &icur, &rec) {
                if (isnullstartblock(rec.br_startblock))
                        continue;
-               ASSERT(xfs_bmbt_validate_extent(ip->i_mount, whichfork, &rec));
+               ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
                xfs_bmbt_disk_set_all(dp, &rec);
                trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
                copied += sizeof(struct xfs_bmbt_rec);