git: 82b475c654ab - stable/13 - gmirror: Zero the metadata block before writing

Mark Johnston markj at FreeBSD.org
Tue Jul 27 01:49:39 UTC 2021


The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=82b475c654abfaa9c28bb1e2e73dfbd15d3f45f4

commit 82b475c654abfaa9c28bb1e2e73dfbd15d3f45f4
Author:     Mark Johnston <markj at FreeBSD.org>
AuthorDate: 2021-07-13 21:45:57 +0000
Commit:     Mark Johnston <markj at FreeBSD.org>
CommitDate: 2021-07-27 01:47:45 +0000

    gmirror: Zero the metadata block before writing
    
    The mirror metadata fields contain string buffers and pad bytes, neither
    were being zeroed before metadata was written to disk.  Also, the
    metadata structure is smaller than the sector size, and in one case
    gmirror was failing to zero-fill the full buffer before writing.
    
    Fix these problems by pre-zeroing the metadata structure and the sector
    buffer.
    
    Reported by:    KMSAN
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 7f053a44aef75eab395ce15a1c8a1399a2f89cad)
---
 sys/geom/mirror/g_mirror.c     | 9 +--------
 sys/geom/mirror/g_mirror_ctl.c | 2 +-
 2 files changed, 2 insertions(+), 9 deletions(-)

diff --git a/sys/geom/mirror/g_mirror.c b/sys/geom/mirror/g_mirror.c
index 51836b7eabb8..c0641d15673e 100644
--- a/sys/geom/mirror/g_mirror.c
+++ b/sys/geom/mirror/g_mirror.c
@@ -749,6 +749,7 @@ g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
     struct g_mirror_metadata *md)
 {
 
+	bzero(md, sizeof(*md));
 	strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
 	md->md_version = G_MIRROR_VERSION;
 	strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
@@ -760,14 +761,8 @@ g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
 	md->md_mediasize = sc->sc_mediasize;
 	md->md_sectorsize = sc->sc_sectorsize;
 	md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
-	bzero(md->md_provider, sizeof(md->md_provider));
 	if (disk == NULL) {
 		md->md_did = arc4random();
-		md->md_priority = 0;
-		md->md_syncid = 0;
-		md->md_dflags = 0;
-		md->md_sync_offset = 0;
-		md->md_provsize = 0;
 	} else {
 		md->md_did = disk->d_id;
 		md->md_priority = disk->d_priority;
@@ -775,8 +770,6 @@ g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
 		md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
 		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
 			md->md_sync_offset = disk->d_sync.ds_offset_done;
-		else
-			md->md_sync_offset = 0;
 		if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
 			strlcpy(md->md_provider,
 			    disk->d_consumer->provider->name,
diff --git a/sys/geom/mirror/g_mirror_ctl.c b/sys/geom/mirror/g_mirror_ctl.c
index 254841b6c04a..355504a6f90f 100644
--- a/sys/geom/mirror/g_mirror_ctl.c
+++ b/sys/geom/mirror/g_mirror_ctl.c
@@ -747,7 +747,7 @@ again:
 			bzero(md.md_provider, sizeof(md.md_provider));
 		}
 		md.md_provsize = pp->mediasize;
-		sector = g_malloc(pp->sectorsize, M_WAITOK);
+		sector = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
 		mirror_metadata_encode(&md, sector);
 		error = g_write_data(disks[i].consumer,
 		    pp->mediasize - pp->sectorsize, sector, pp->sectorsize);


More information about the dev-commits-src-all mailing list