Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Aug 2019 19:09:40 +0000 (UTC)
From:      Ed Maste <emaste@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r351347 - head/usr.sbin/makefs/msdos
Message-ID:  <201908211909.x7LJ9e7l088698@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: emaste
Date: Wed Aug 21 19:09:40 2019
New Revision: 351347
URL: https://svnweb.freebsd.org/changeset/base/351347

Log:
  makefs: Verify that the BPB media descriptor and FAT ID match
  
  From r322982 in sys/fs/msdosfs.

Modified:
  head/usr.sbin/makefs/msdos/msdosfs_fat.c

Modified: head/usr.sbin/makefs/msdos/msdosfs_fat.c
==============================================================================
--- head/usr.sbin/makefs/msdos/msdosfs_fat.c	Wed Aug 21 19:07:13 2019	(r351346)
+++ head/usr.sbin/makefs/msdos/msdosfs_fat.c	Wed Aug 21 19:09:40 2019	(r351347)
@@ -887,19 +887,17 @@ fillinusemap(struct msdosfsmount *pmp)
 	 * zero.  These represent free clusters.
 	 */
 	pmp->pm_freeclustercount = 0;
-	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
+	for (cn = 0; cn <= pmp->pm_maxcluster; cn++) {
 		byteoffset = FATOFS(pmp, cn);
 		bo = byteoffset % pmp->pm_fatblocksize;
-		if (bo == 0 || bp == NULL) {
+		if (bo == 0) {
 			/* Read new FAT block */
 			if (bp != NULL)
 				brelse(bp);
 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
-			if (error != 0) {
-				brelse(bp);
+			if (error != 0)
 				return (error);
-			}
 		}
 		if (FAT32(pmp))
 			readcn = getulong(bp->b_data + bo);
@@ -909,7 +907,19 @@ fillinusemap(struct msdosfsmount *pmp)
 			readcn >>= 4;
 		readcn &= pmp->pm_fatmask;
 
-		if (readcn == CLUST_FREE)
+		/*
+		 * Check if the FAT ID matches the BPB's media descriptor and
+		 * all other bits are set to 1.
+		 */
+		if (cn == 0 && readcn != ((pmp->pm_fatmask & 0xffffff00) |
+		    pmp->pm_bpb.bpbMedia)) {
+#ifdef MSDOSFS_DEBUG
+			printf("mountmsdosfs(): Media descriptor in BPB"
+			    "does not match FAT ID\n");
+#endif
+			brelse(bp);
+			return (EINVAL);
+		} else if (readcn == CLUST_FREE)
 			usemap_free(pmp, cn);
 	}
 	if (bp != NULL)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201908211909.x7LJ9e7l088698>