mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alison Schofield <alison.schofield@intel.com>
To: <zzjas98@gmail.com>
Cc: Dan Williams <djbw@kernel.org>,
	Vishal Verma <vishal.l.verma@intel.com>,
	Dave Jiang <dave.jiang@intel.com>, Ira Weiny <iweiny@kernel.org>,
	<nvdimm@lists.linux.dev>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] libnvdimm/security: Reject malformed security commands
Date: Thu, 3 Dec 2026 18:34:23 -0800	[thread overview]
Message-ID: <axImr9PO4g0VDYAs@aschofie-mobl2.lan> (raw)
In-Reply-To: <20261203-b4-l936-v2-v2-1-75186c6af4aa@gmail.com>

On Thu, Dec 03, 2026 at 04:41:27PM -0600, Zijie Zhao via B4 Relay wrote:
> From: Zijie Zhao <zzjas98@gmail.com>
> 
> nvdimm_security_store() parses writes to nmemX/security with sscanf()
> and only checks that a command word was found, so it accepts malformed
> commands:
> 
>  - A missing key id ("disable", "update <id>") leaves its buffer
>    unwritten, and kstrtouint() parses uninitialized stack.
>  - A key id longer than 10 characters is split in two, so
>    "update 12345678901" is read as an update from key 1234567890 to
>    key 1.
>  - Extra fields are ignored, so "disable <id> 0" disables security.
> 
> ndctl always writes complete commands; only a root process writing the
> attribute directly can send these.
> 
> With CONFIG_INIT_STACK_ALL_ZERO, the default where the compiler
> supports it, the missing key id buffer is zeroed and the write fails
> with -EINVAL. Without it, stale stack bytes that happen to form a
> number would be used as the key id (inferred from the code, not
> observed).
> 
> Observed with nfit_test under QEMU: "disable <id> 0" and "freeze 0"
> succeed and change the DIMM's security state, "update 12345678901"
> fails with -ENOKEY rather than -EINVAL, and on a KMSAN build each
> missing key id write reports "BUG: KMSAN: uninit-value in kstrtoull".
> 
> Split the input with argv_split(), require exactly the number of fields
> the command takes, and parse each key id from its whole field.
> 
> The missing key id case was found by an AI-powered code analysis tool
> that is still in development.

Thanks!
Reviewed, tested, and applied to libnvdimm-for-next:
https://git.kernel.org/nvdimm/nvdimm/c/182f43e35f38

I noted a couple of very minor mods upon applying:
[ as: link unit test in commit log, reverse fir tree func variables ]


> 
> Fixes: 03b65b22ada8 ("acpi/nfit, libnvdimm: Add disable passphrase support to Intel nvdimm.")
> Signed-off-by: Zijie Zhao <zzjas98@gmail.com>
> ---
> Changes in v2:
> - Parse with argv_split() and require an exact field count. This also
>   rejects an overlong key id that sscanf() split in two, and trailing
>   extra fields (Alison).
> - Rewrite the changelog: configuration context, what was observed at
>   runtime and what is inferred, which tool found it (Alison).
> - Add an ndctl regression test, sent separately as "[ndctl PATCH]
>   test/security.sh: Test that malformed security commands are
>   rejected".
> - v1: https://lore.kernel.org/all/20261201-kbug-l936-v1-1-a2e78263625c@gmail.com/
> 
> Testing (x86_64, QEMU/KVM, nfit_test and cxl_test built from this tree,
> ndctl v85 plus the new test):
> - nfit-security.sh and cxl-security.sh with the new malformed-command
>   case: both fail without this patch (at "update 12345678901": ENOKEY
>   instead of EINVAL) and pass with it.
> - Whole ndctl "ndctl" suite (54 tests): 40 pass, 4 fail, 10 skip with
>   this patch; without it the same, except nfit-security.sh and
>   cxl-security.sh fail. The 4 failures (cxl-translate, daxctl-famfs, dm,
>   firmware-update) and the skips (mostly DAX tests, with CONFIG_DAX=y
>   here) are the same with and without the patch.
> - KMSAN build (clang): the four uninit-value reports above are gone
>   with this patch.
> 
> The fix, this changelog and the ndctl test were written with the help
> of an AI coding assistant (Claude Opus 5.5). I have reviewed all of it.
> ---
>  drivers/nvdimm/security.c | 39 +++++++++++++++++++--------------------
>  1 file changed, 19 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/nvdimm/security.c b/drivers/nvdimm/security.c
> index e41f6951ca0f..07f9026a8371 100644
> --- a/drivers/nvdimm/security.c
> +++ b/drivers/nvdimm/security.c
> @@ -511,35 +511,34 @@ static struct {
>  } ops[] = { OPS };
>  #undef C
>  
> -#define SEC_CMD_SIZE 32
> -#define KEY_ID_SIZE 10
> -
>  ssize_t nvdimm_security_store(struct device *dev, const char *buf, size_t len)
>  {
>  	struct nvdimm *nvdimm = to_nvdimm(dev);
> -	ssize_t rc;
> -	char cmd[SEC_CMD_SIZE+1], keystr[KEY_ID_SIZE+1],
> -		nkeystr[KEY_ID_SIZE+1];
>  	unsigned int key, newkey;
> -	int i;
> +	ssize_t rc;
> +	int argc, i;
>  
> -	rc = sscanf(buf, "%"__stringify(SEC_CMD_SIZE)"s"
> -			" %"__stringify(KEY_ID_SIZE)"s"
> -			" %"__stringify(KEY_ID_SIZE)"s",
> -			cmd, keystr, nkeystr);
> -	if (rc < 1)
> +	char **argv __free(argv_free) = argv_split(GFP_KERNEL, buf, &argc);
> +	if (!argv)
> +		return -ENOMEM;
> +	if (argc < 1)
>  		return -EINVAL;
>  	for (i = 0; i < ARRAY_SIZE(ops); i++)
> -		if (sysfs_streq(cmd, ops[i].name))
> +		if (strcmp(argv[0], ops[i].name) == 0)
>  			break;
> -	if (i >= ARRAY_SIZE(ops))
> +	/* The command word must be followed by exactly the key ids it takes. */
> +	if (i >= ARRAY_SIZE(ops) || argc != ops[i].args)
>  		return -EINVAL;
> -	if (ops[i].args > 1)
> -		rc = kstrtouint(keystr, 0, &key);
> -	if (rc >= 0 && ops[i].args > 2)
> -		rc = kstrtouint(nkeystr, 0, &newkey);
> -	if (rc < 0)
> -		return rc;
> +	if (ops[i].args > 1) {
> +		rc = kstrtouint(argv[1], 0, &key);
> +		if (rc)
> +			return rc;
> +	}
> +	if (ops[i].args > 2) {
> +		rc = kstrtouint(argv[2], 0, &newkey);
> +		if (rc)
> +			return rc;
> +	}
>  
>  	if (i == OP_FREEZE) {
>  		dev_dbg(dev, "freeze\n");
> 
> ---
> base-commit: 5a277aa7eb4d4f1f05534d6c3dcb1a9fa4f6b8f2
> change-id: 20261203-b4-l936-v2-08b2f20f6c84
> 
> Best regards,
> --  
> Zijie Zhao <zzjas98@gmail.com>
> 
> 
> 

      reply	other threads:[~2026-12-04  2:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 22:41 Zijie Zhao via B4 Relay
2026-12-04  2:34 ` Alison Schofield [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axImr9PO4g0VDYAs@aschofie-mobl2.lan \
    --to=alison.schofield@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=djbw@kernel.org \
    --cc=iweiny@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=vishal.l.verma@intel.com \
    --cc=zzjas98@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®