]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
libxfs: implement some sanity checking for enormous rgcount
authorDarrick J. Wong <djwong@kernel.org>
Thu, 9 Nov 2023 23:34:10 +0000 (15:34 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Wed, 22 Nov 2023 23:03:34 +0000 (15:03 -0800)
Similar to what we do for suspiciously large sb_agcount values, if
someone tries to get libxfs to load a filesystem with a very large
realtime group count, let's do some basic checks of the rt device to
see if it's really that large.  If the read fails, only load the first
rtgroup and warn the user.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
libxfs/init.c

index e36d105f2eb5ef84d954f8bc6a6de24ba03587c6..29b051c4a5729dcc5721c17778cf912f474d6d82 100644 (file)
@@ -893,6 +893,50 @@ xfs_set_low_space_thresholds(
                mp->m_low_space[i] = dblocks * (i + 1);
 }
 
+/*
+ * libxfs_initialize_rtgroup will allocate a rtgroup structure for each
+ * rtgroup.  If rgcount is corrupted and insanely high, this will OOM the box.
+ * Try to read what would be the last rtgroup superblock.  If that fails, read
+ * the first one and let the user know to check the geometry.
+ */
+static inline bool
+check_many_rtgroups(
+       struct xfs_mount        *mp,
+       struct xfs_sb           *sbp)
+{
+       struct xfs_buf          *bp;
+       xfs_rtblock_t           rtbno;
+       int                     error;
+
+       if (!mp->m_rtdev->bt_bdev) {
+               fprintf(stderr, _("%s: no rt device, ignoring rgcount %u\n"),
+                               progname, sbp->sb_rgcount);
+               if (!xfs_is_debugger(mp))
+                       return false;
+
+               sbp->sb_rgcount = 0;
+               return true;
+       }
+
+       rtbno = xfs_rgbno_to_rtb(mp, sbp->sb_rgcount - 1, 0);
+
+       error = libxfs_buf_read(mp->m_rtdev, xfs_rtb_to_daddr(mp, rtbno), 1, 0,
+                       &bp, NULL);
+       if (!error) {
+               libxfs_buf_relse(bp);
+               return true;
+       }
+
+       fprintf(stderr, _("%s: read of rtgroup %u failed\n"), progname,
+                       sbp->sb_rgcount - 1);
+       if (!xfs_is_debugger(mp))
+               return false;
+
+       fprintf(stderr, _("%s: limiting reads to rtgroup 0\n"), progname);
+       sbp->sb_rgcount = 1;
+       return true;
+}
+
 /*
  * Mount structure initialization, provides a filled-in xfs_mount_t
  * such that the numerous XFS_* macros can be used.  If dev is zero,
@@ -1052,6 +1096,9 @@ libxfs_mount(
                        libxfs_buf_relse(bp);
        }
 
+       if (sbp->sb_rgcount > 1000000 && !check_many_rtgroups(mp, sbp))
+               goto out_da;
+
        error = libxfs_initialize_perag(mp, sbp->sb_agcount, sbp->sb_dblocks,
                        &mp->m_maxagi);
        if (error) {