git: 3d6f886151f9 - main - nvmecontrol: Range-check the format command's field values

From: Kenneth D. Merry <ken_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 21:23:21 UTC
The branch main has been updated by ken:

URL: https://cgit.FreeBSD.org/src/commit/?id=3d6f886151f9ee6f3bf16ea3367a4b540df3f911

commit 3d6f886151f9ee6f3bf16ea3367a4b540df3f911
Author:     Kenneth D. Merry <ken@FreeBSD.org>
AuthorDate: 2026-11-10 21:12:21 +0000
Commit:     Kenneth D. Merry <ken@FreeBSD.org>
CommitDate: 2026-11-10 21:23:19 +0000

    nvmecontrol: Range-check the format command's field values
    
    The format command accepted any 32-bit value for -m, -p, -l and
    --ses and passed it into the Format NVM command.  Before
    eae727676169 an out-of-range value spilled into the adjacent cdw10
    fields, which usually drew an error from the controller; after it,
    the value is masked to the field width, so a mistyped value is
    silently accepted with a different meaning.  Most seriously,
    --ses 8 masked to SES=0 and performed a plain format with no erase
    at all, exiting 0.  A value like --ses 10 masked to a cryptographic
    erase while bypassing the FNA cryptographic erase capability check,
    which compares the unmasked value.
    
    Reject values outside the ranges nvmecontrol.8 documents, which are
    the values the spec defines: 0-1 for -m and -l, 0-3 for -p, and
    0-2 for --ses.  The checks run at option validation time, before
    the device is opened, and the capability check now only ever sees
    valid values.
    
    While here, document --ses in nvmecontrol.8; it was missing from the
    format subcommand's option list.
    
    Reported by:    llm-postcr
    Reviewed by:    imp
    Fixes:          eae727676169
    MFC after:      1 week
    Sponsored by:   Spectra Logic
    Assisted-by:    Claude Fable 5 <noreply@anthropic.com>
    Differential Revision:  https://reviews.freebsd.org/D61406
---
 sbin/nvmecontrol/format.c      | 10 ++++++++++
 sbin/nvmecontrol/nvmecontrol.8 | 20 +++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/sbin/nvmecontrol/format.c b/sbin/nvmecontrol/format.c
index 62dd56a5a81e..102da0d245e8 100644
--- a/sbin/nvmecontrol/format.c
+++ b/sbin/nvmecontrol/format.c
@@ -204,6 +204,16 @@ format(const struct cmd *f, int argc, char *argv[])
 		    "Only one of -E, -C or --ses may be specified\n");
 		arg_help(argc, argv, f);
 	}
+	if (opt.ms != NONE && opt.ms > 1)
+		errx(EX_USAGE, "metadata setting must be 0 or 1");
+	if (opt.pi != NONE && opt.pi > 3)
+		errx(EX_USAGE,
+		    "protection information type must be between 0 and 3");
+	if (opt.pil != NONE && opt.pil > 1)
+		errx(EX_USAGE,
+		    "protection information location must be 0 or 1");
+	if (opt.ses > SES_CRYPTO)
+		errx(EX_USAGE, "secure erase setting must be between 0 and 2");
 
 	target = opt.dev;
 	lbaf = opt.lbaf;
diff --git a/sbin/nvmecontrol/nvmecontrol.8 b/sbin/nvmecontrol/nvmecontrol.8
index 76646a578e3b..1c92eb6af43b 100644
--- a/sbin/nvmecontrol/nvmecontrol.8
+++ b/sbin/nvmecontrol/nvmecontrol.8
@@ -33,7 +33,7 @@
 .\"
 .\" Author: Jim Harris <jimharris@FreeBSD.org>
 .\"
-.Dd September 21, 2026
+.Dd November 10, 2026
 .Dt NVMECONTROL 8
 .Os
 .Sh NAME
@@ -573,6 +573,24 @@ physically erase the underlying media.
 Enables Cryptographic Erase during format.
 All user data is erased  cryptographically by deleting the encryption key,
 rendering it unintelligible.
+.It Fl Fl ses Ar ses
+Secure Erase Settings.
+.Bl -tag -compact -width 6n
+.It Dv 0
+Do not perform a secure erase.
+.It Dv 1
+User Data Erase, as with
+.Fl E .
+.It Dv 2
+Cryptographic Erase, as with
+.Fl C .
+.El
+Only one of
+.Fl E ,
+.Fl C ,
+or
+.Fl Fl ses
+may be specified.
 .El
 .Pp
 When formatting specific namespace, existing values are used as defaults.