git: 184e3118c105 - main - Updates to UFS/FFS superblock integrity checks when reading a superblock.

From: Kirk McKusick <mckusick_at_FreeBSD.org>
Date: Mon, 01 Aug 2022 03:29:24 UTC
The branch main has been updated by mckusick:

URL: https://cgit.FreeBSD.org/src/commit/?id=184e3118c1057a97e16230baf0f0433adeeed723

commit 184e3118c1057a97e16230baf0f0433adeeed723
Author:     Kirk McKusick <mckusick@FreeBSD.org>
AuthorDate: 2022-08-01 03:28:04 +0000
Commit:     Kirk McKusick <mckusick@FreeBSD.org>
CommitDate: 2022-08-01 03:29:08 +0000

    Updates to UFS/FFS superblock integrity checks when reading a superblock.
    
    Further updates based on analysis of the way the fields are used
    in the various filesystem macros defined in fs.h.
    
    Eliminate several checks for non-negative values where the fields
    are checked for specific values. Since these specific values are
    non-negative, if the value is a verified positive value then it
    cannot be negative and such a check is redundant and unnecessary.
    
    No legitimate superblocks should fail as a result of these changes.
---
 sys/ufs/ffs/ffs_subr.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/sys/ufs/ffs/ffs_subr.c b/sys/ufs/ffs/ffs_subr.c
index 90e7034e65a5..0bcc43fae4d4 100644
--- a/sys/ufs/ffs/ffs_subr.c
+++ b/sys/ufs/ffs/ffs_subr.c
@@ -355,33 +355,28 @@ validate_sblock(struct fs *fs, int flags)
 	sectorsize = dbtob(1);
 	prtmsg = ((flags & UFS_NOMSG) == 0);
 	if (fs->fs_magic == FS_UFS2_MAGIC) {
-		if ((flags & UFS_ALTSBLK) == 0) {
-			CHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
+		if ((flags & UFS_ALTSBLK) == 0)
 			CHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2,
 			    fs->fs_sblockactualloc, !=, 0, %jd);
-		}
+		CHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
 		CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
 			sizeof(ufs2_daddr_t)), %jd);
 		CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t),
 		    %jd);
-		CHK(fs->fs_inopb, <, 1, %jd);
-		CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs2_dinode),
-		    %jd);
+		CHK(fs->fs_inopb, !=,
+		    fs->fs_bsize / sizeof(struct ufs2_dinode), %jd);
 	} else if (fs->fs_magic == FS_UFS1_MAGIC) {
-		if ((flags & UFS_ALTSBLK) == 0) {
-			CHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
+		if ((flags & UFS_ALTSBLK) == 0)
 			CHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd);
-		}
+		CHK(fs->fs_sblockloc, <, 0, %jd);
+		CHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
 		CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t),
 		    %jd);
-		CHK(fs->fs_inopb, <, 1, %jd);
-		CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs1_dinode),
-		    %jd);
+		CHK(fs->fs_inopb, !=,
+		    fs->fs_bsize / sizeof(struct ufs1_dinode), %jd);
 		CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
 			sizeof(ufs1_daddr_t)), %jd);
 		CHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd);
-		CHK(fs->fs_old_cgoffset, !=, 0, %jd);
-		CHK(fs->fs_old_cgmask, !=, 0xffffffff, %#jx);
 		CHK(fs->fs_old_rotdelay, !=, 0, %jd);
 		CHK(fs->fs_old_rps, !=, 60, %jd);
 		CHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd);
@@ -414,8 +409,6 @@ validate_sblock(struct fs *fs, int flags)
 	CHK(fs->fs_ipg, <, 1, %jd);
 	CHK(fs->fs_ipg * fs->fs_ncg, >, (((int64_t)(1)) << 32) - INOPB(fs),
 	    %jd);
-	CHK(fs->fs_sblockloc, <, 0, %jd);
-	CHK(fs->fs_sblockloc, >, dbtob(fs->fs_fpg), %jd);
 	CHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
 	CHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd);
 	CHK(powerof2(fs->fs_maxbsize), ==, 0, %jd);
@@ -428,6 +421,9 @@ validate_sblock(struct fs *fs, int flags)
 	CHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd);
 	CHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd);
 	CHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
+	CHK(fs->fs_old_cgoffset, <, 0, %jd);
+	CHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
+	CHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd);
 	CHK(fs->fs_sblkno, !=, roundup(
 	    howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
 	    fs->fs_frag), %jd);