git: 94434cd83c02 - stable/14 - nvme: Mask the fields in the Format NVM command builder

From: Kenneth D. Merry <ken_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 21:01:07 UTC
The branch stable/14 has been updated by ken:

URL: https://cgit.FreeBSD.org/src/commit/?id=94434cd83c0241bb0611051a34d50152ef109e8b

commit 94434cd83c0241bb0611051a34d50152ef109e8b
Author:     Kenneth D. Merry <ken@FreeBSD.org>
AuthorDate: 2026-11-09 18:46:57 +0000
Commit:     Kenneth D. Merry <ken@FreeBSD.org>
CommitDate: 2026-11-16 21:00:06 +0000

    nvme: Mask the fields in the Format NVM command builder
    
    nvme_admin_format_cmd() did not mask any of the fields it composes
    into cdw10, so an out of range value in one field would corrupt
    neighboring fields.  In particular, an LBA format index greater
    than 15 would bleed into the metadata settings and protection
    information fields.  It also had no support for LBA format indexes
    above 15, which NVMe 2.0 encodes via the separate LBAFU field.
    
    Mask all of the fields, and encode bits 5:4 of the LBA format index
    in the LBAFU field (cdw10 bits 13:12).
    
    Also reject out of range LBA format indexes in camcontrol's
    nvformat global (-G) mode, which has no namespace data to validate
    against.  The per-namespace mode already validates the index
    against NLBAF.
    
    sys/dev/nvme/nvme.h:
            Mask the SES, PIL, PI, MSET and LBAF fields in
            nvme_admin_format_cmd() and encode the upper two bits of
            the LBA format index in LBAFU.
    
    sbin/camcontrol/nvformat.c:
            Reject LBA format indexes greater than 63 in -G mode.
    
    Reported by:    llm-postcr
    Fixes:          7a9e4375316b
    Fixes:          80f090111ae4
    MFC after:      1 week
    Sponsored by:   Spectra Logic
    Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
    
    (cherry picked from commit 3892a6453fc60e00b0d6cd5c5d7e2606b4b51e7c)
---
 sbin/camcontrol/nvformat.c | 6 ++++++
 sys/dev/nvme/nvme.h        | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/sbin/camcontrol/nvformat.c b/sbin/camcontrol/nvformat.c
index 94af800db422..a41b490d62bc 100644
--- a/sbin/camcontrol/nvformat.c
+++ b/sbin/camcontrol/nvformat.c
@@ -246,6 +246,12 @@ nvmeformat(struct cam_device *device, int argc, char **argv, char *combinedopt,
 			ses = 0;
 		if (lbaf < 0)
 			lbaf = 0;
+		else if (lbaf > 63) {
+			warnx("%s: LBA format %d is out of range", __func__,
+			    lbaf);
+			error = 1;
+			goto bailout;
+		}
 		if (mset < 0)
 			mset = 0;
 		if (pi < 0)
diff --git a/sys/dev/nvme/nvme.h b/sys/dev/nvme/nvme.h
index 8a37822f841e..658461096b0a 100644
--- a/sys/dev/nvme/nvme.h
+++ b/sys/dev/nvme/nvme.h
@@ -2162,8 +2162,9 @@ void	nvme_admin_format_cmd(struct nvme_command *cmd, uint32_t nsid,
 {
 	cmd->opc = NVME_OPC_FORMAT_NVM;
 	cmd->nsid = htole32(nsid);
-	cmd->cdw10 = htole32((ses << 9) | (pil << 8) | (pi << 5) | (mset << 4) |
-			      lbaf);
+	cmd->cdw10 = htole32(((lbaf & 0x30) << 8) | ((ses & 0x7) << 9) |
+	    ((pil & 0x1) << 8) | ((pi & 0x7) << 5) | ((mset & 0x1) << 4) |
+	    (lbaf & 0x0f));
 }
 
 static inline