git: 0e86854450b4 - stable/13 - Don't panic when tasting a disk with sectorsize=0

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Wed, 23 Nov 2022 21:03:33 UTC
The branch stable/13 has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=0e86854450b49af5d3f7c85656d6874a3851dcb0

commit 0e86854450b49af5d3f7c85656d6874a3851dcb0
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2022-11-09 02:59:50 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2022-11-23 20:57:47 +0000

    Don't panic when tasting a disk with sectorsize=0
    
    This can sometimes happen with broken HDDs.
    
    Sponsored by:   Axcient
    Reviewed by:    markj
    Differential Revision: https://reviews.freebsd.org/D37313
    
    (cherry picked from commit 05d0f4308caaa3e9b8cbcbb30b2d7f39dd367a57)
---
 sys/geom/label/g_label.c          | 4 ++++
 sys/geom/label/g_label_ext2fs.c   | 5 +++--
 sys/geom/label/g_label_iso9660.c  | 5 +++--
 sys/geom/label/g_label_reiserfs.c | 5 +++--
 sys/geom/label/g_label_ufs.c      | 1 +
 5 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/sys/geom/label/g_label.c b/sys/geom/label/g_label.c
index 2244931cd19b..8f6ac38179a6 100644
--- a/sys/geom/label/g_label.c
+++ b/sys/geom/label/g_label.c
@@ -394,6 +394,10 @@ g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
 	if (pp->acw > 0)
 		return (NULL);
 
+	/* Skip broken disks that don't set their sector size */
+	if (pp->sectorsize == 0)
+		return (NULL);
+
 	if (strcmp(pp->geom->class->name, mp->name) == 0)
 		return (NULL);
 
diff --git a/sys/geom/label/g_label_ext2fs.c b/sys/geom/label/g_label_ext2fs.c
index 3420efb7ca3a..0f21de1ce313 100644
--- a/sys/geom/label/g_label_ext2fs.c
+++ b/sys/geom/label/g_label_ext2fs.c
@@ -62,10 +62,11 @@ g_label_ext2fs_taste(struct g_consumer *cp, char *label, size_t size)
 	pp = cp->provider;
 	label[0] = '\0';
 
-	if ((EXT2FS_SB_OFFSET % pp->sectorsize) != 0)
-		return;
+	KASSERT(pp->sectorsize != 0, ("Tasting a disk with 0 sectorsize"));
 	if (pp->sectorsize < sizeof(*fs))
 		return;
+	if ((EXT2FS_SB_OFFSET % pp->sectorsize) != 0)
+		return;
 
 	fs = g_read_data(cp, EXT2FS_SB_OFFSET, pp->sectorsize, NULL);
 	if (fs == NULL)
diff --git a/sys/geom/label/g_label_iso9660.c b/sys/geom/label/g_label_iso9660.c
index b46a47bbd36a..1ac7283b34e6 100644
--- a/sys/geom/label/g_label_iso9660.c
+++ b/sys/geom/label/g_label_iso9660.c
@@ -52,10 +52,11 @@ g_label_iso9660_taste(struct g_consumer *cp, char *label, size_t size)
 	pp = cp->provider;
 	label[0] = '\0';
 
-	if ((ISO9660_OFFSET % pp->sectorsize) != 0)
-		return;
+	KASSERT(pp->sectorsize != 0, ("Tasting a disk with 0 sectorsize"));
 	if (pp->sectorsize < 0x28 + VOLUME_LEN)
 		return;
+	if ((ISO9660_OFFSET % pp->sectorsize) != 0)
+		return;
 	sector = g_read_data(cp, ISO9660_OFFSET, pp->sectorsize, NULL);
 	if (sector == NULL)
 		return;
diff --git a/sys/geom/label/g_label_reiserfs.c b/sys/geom/label/g_label_reiserfs.c
index d6f9a0428b6a..8fe1a72458e4 100644
--- a/sys/geom/label/g_label_reiserfs.c
+++ b/sys/geom/label/g_label_reiserfs.c
@@ -59,10 +59,11 @@ g_label_reiserfs_read_super(struct g_consumer *cp, off_t offset)
 
 	secsize = cp->provider->sectorsize;
 
-	if ((offset % secsize) != 0)
-		return (NULL);
+	KASSERT(secsize != 0, ("Tasting a disk with 0 sectorsize"));
 	if (secsize < sizeof(*fs))
 		return (NULL);
+	if ((offset % secsize) != 0)
+		return (NULL);
 
 	fs = g_read_data(cp, offset, secsize, NULL);
 	if (fs == NULL)
diff --git a/sys/geom/label/g_label_ufs.c b/sys/geom/label/g_label_ufs.c
index ababbaa4b43a..8b2776e034fe 100644
--- a/sys/geom/label/g_label_ufs.c
+++ b/sys/geom/label/g_label_ufs.c
@@ -74,6 +74,7 @@ g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int wh
 	label[0] = '\0';
 
 	fs = NULL;
+	KASSERT(pp->sectorsize != 0, ("Tasting a disk with 0 sectorsize"));
 	if (SBLOCKSIZE % pp->sectorsize != 0 || ffs_sbget(cp, &fs,
 	    STDSB_NOHASHFAIL, M_GEOM, g_use_g_read_data) != 0) {
 		KASSERT(fs == NULL,