git: 7b1763b4ee6c - stable/14 - nvmecontrol: Mask the fields in the Format NVM command

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

URL: https://cgit.FreeBSD.org/src/commit/?id=7b1763b4ee6c8ea03e21add2121a7f001d612629

commit 7b1763b4ee6c8ea03e21add2121a7f001d612629
Author:     Kenneth D. Merry <ken@FreeBSD.org>
AuthorDate: 2026-11-09 19:35:00 +0000
Commit:     Kenneth D. Merry <ken@FreeBSD.org>
CommitDate: 2026-11-16 21:00:07 +0000

    nvmecontrol: Mask the fields in the Format NVM command
    
    The format command composed cdw10 by hand without masking any of
    the fields and with no support for the LBAFU field.  Since commit
    2c41f7d8a034 the LBA format index defaulted from FLBAS can exceed
    15 on an NVMe 2.0 namespace using FIDXU, and such an index would
    bleed into the MSET and PI fields.  An out of range index supplied
    with -f for a whole-controller format was not caught either.
    
    Build the command with nvme_admin_format_cmd(), which masks all of
    the fields and encodes LBA format indexes above 15 in LBAFU, and
    reject LBA format indexes greater than 63 for whole-controller
    formats.  Per-namespace formats already validate the index against
    NLBAF.
    
    This is the same problem that llm-postcr reported in camcontrol's
    copy of this code, fixed in 3892a6453fc6; the unmasked composition
    originated here (635c517ae9ca).
    
    Reported by:    llm-postcr
    Reviewed by:    imp
    Fixes:          2c41f7d8a034
    MFC after:      1 week
    Sponsored by:   Spectra Logic
    Assisted-by:    Claude Fable 5 <noreply@anthropic.com>
    Differential Revision:  https://reviews.freebsd.org/D61356
    
    (cherry picked from commit eae727676169de58052fd512ebb0ee565ad6e246)
---
 sbin/nvmecontrol/format.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/sbin/nvmecontrol/format.c b/sbin/nvmecontrol/format.c
index a507ddd78ffb..f45bae593354 100644
--- a/sbin/nvmecontrol/format.c
+++ b/sbin/nvmecontrol/format.c
@@ -192,6 +192,8 @@ format(const struct cmd *f, int argc, char *argv[])
 		/* We have no previous parameters, so default to zeroes. */
 		if (lbaf < 0)
 			lbaf = 0;
+		else if (lbaf > 63)
+			errx(EX_USAGE, "LBA format is out of range");
 		if (ms < 0)
 			ms = 0;
 		if (pi < 0)
@@ -201,10 +203,7 @@ format(const struct cmd *f, int argc, char *argv[])
 	}
 
 	memset(&pt, 0, sizeof(pt));
-	pt.cmd.opc = NVME_OPC_FORMAT_NVM;
-	pt.cmd.nsid = htole32(nsid);
-	pt.cmd.cdw10 = htole32((ses << 9) + (pil << 8) + (pi << 5) +
-	    (ms << 4) + lbaf);
+	nvme_admin_format_cmd(&pt.cmd, nsid, ses, pil, pi, ms, lbaf);
 
 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
 		err(EX_IOERR, "format request failed");