* [PATCH] libnvdimm/security: Reject security commands with missing key ids
@ 2026-12-02 3:16 ` Zijie Zhao via B4 Relay
0 siblings, 0 replies; 3+ messages in thread
From: Zijie Zhao @ 2026-12-02 3:16 UTC (permalink / raw)
To: Dan Williams, Vishal Verma, Dave Jiang, Alison Schofield
Cc: Ira Weiny, nvdimm, linux-kernel, Zijie Zhao
nvdimm_security_store() parses the 'security' attribute with sscanf()
into the uninitialized stack arrays cmd, keystr and nkeystr, but only
rejects a return value below 1. Commands that take key ids are then
parsed with kstrtouint() on keystr and nkeystr according to
ops[i].args, regardless of how many fields sscanf() actually filled.
Writing e.g. "disable" without a key id, or "update 1" without the new
key id, makes kstrtouint() parse uninitialized stack memory that is not
guaranteed to be NUL terminated within the array. If that memory parses
as a number, a key id taken from stale stack contents is passed to the
security operation handler instead of the write failing with -EINVAL,
and the handler proceeds if its state and key checks succeed.
Return -EINVAL when sscanf() converted fewer fields than the matched
command requires.
Found by a static analysis tool.
The fix and this changelog were drafted with AI assistance (Claude Code,
with a second review by OpenAI Codex), then reviewed by the submitter.
Fixes: 03b65b22ada8 ("acpi/nfit, libnvdimm: Add disable passphrase support to Intel nvdimm.")
Signed-off-by: Zijie Zhao <zzjas98@gmail.com>
---
Testing:
- Compile-tested on x86_64 (allmodconfig, W=1):
drivers/nvdimm/security.o; no new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
drivers/nvdimm/security.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/nvdimm/security.c b/drivers/nvdimm/security.c
index e41f6951ca0f..1fc6c0ad1460 100644
--- a/drivers/nvdimm/security.c
+++ b/drivers/nvdimm/security.c
@@ -534,6 +534,8 @@ ssize_t nvdimm_security_store(struct device *dev, const char *buf, size_t len)
break;
if (i >= ARRAY_SIZE(ops))
return -EINVAL;
+ if (rc < ops[i].args)
+ return -EINVAL;
if (ops[i].args > 1)
rc = kstrtouint(keystr, 0, &key);
if (rc >= 0 && ops[i].args > 2)
---
base-commit: 5a277aa7eb4d4f1f05534d6c3dcb1a9fa4f6b8f2
change-id: 20261201-kbug-l936-0b24cea11668
Best regards,
--
Zijie Zhao <zzjas98@gmail.com>
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH] libnvdimm/security: Reject security commands with missing key ids
@ 2026-12-02 3:16 ` Zijie Zhao via B4 Relay
0 siblings, 0 replies; 3+ messages in thread
From: Zijie Zhao via B4 Relay @ 2026-12-02 3:16 UTC (permalink / raw)
To: Dan Williams, Vishal Verma, Dave Jiang, Alison Schofield
Cc: Ira Weiny, nvdimm, linux-kernel, Zijie Zhao
From: Zijie Zhao <zzjas98@gmail.com>
nvdimm_security_store() parses the 'security' attribute with sscanf()
into the uninitialized stack arrays cmd, keystr and nkeystr, but only
rejects a return value below 1. Commands that take key ids are then
parsed with kstrtouint() on keystr and nkeystr according to
ops[i].args, regardless of how many fields sscanf() actually filled.
Writing e.g. "disable" without a key id, or "update 1" without the new
key id, makes kstrtouint() parse uninitialized stack memory that is not
guaranteed to be NUL terminated within the array. If that memory parses
as a number, a key id taken from stale stack contents is passed to the
security operation handler instead of the write failing with -EINVAL,
and the handler proceeds if its state and key checks succeed.
Return -EINVAL when sscanf() converted fewer fields than the matched
command requires.
Found by a static analysis tool.
The fix and this changelog were drafted with AI assistance (Claude Code,
with a second review by OpenAI Codex), then reviewed by the submitter.
Fixes: 03b65b22ada8 ("acpi/nfit, libnvdimm: Add disable passphrase support to Intel nvdimm.")
Signed-off-by: Zijie Zhao <zzjas98@gmail.com>
---
Testing:
- Compile-tested on x86_64 (allmodconfig, W=1):
drivers/nvdimm/security.o; no new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
drivers/nvdimm/security.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/nvdimm/security.c b/drivers/nvdimm/security.c
index e41f6951ca0f..1fc6c0ad1460 100644
--- a/drivers/nvdimm/security.c
+++ b/drivers/nvdimm/security.c
@@ -534,6 +534,8 @@ ssize_t nvdimm_security_store(struct device *dev, const char *buf, size_t len)
break;
if (i >= ARRAY_SIZE(ops))
return -EINVAL;
+ if (rc < ops[i].args)
+ return -EINVAL;
if (ops[i].args > 1)
rc = kstrtouint(keystr, 0, &key);
if (rc >= 0 && ops[i].args > 2)
---
base-commit: 5a277aa7eb4d4f1f05534d6c3dcb1a9fa4f6b8f2
change-id: 20261201-kbug-l936-0b24cea11668
Best regards,
--
Zijie Zhao <zzjas98@gmail.com>
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] libnvdimm/security: Reject security commands with missing key ids
2026-12-02 3:16 ` Zijie Zhao via B4 Relay
(?)
@ 2026-12-02 22:55 ` Alison Schofield
-1 siblings, 0 replies; 3+ messages in thread
From: Alison Schofield @ 2026-12-02 22:55 UTC (permalink / raw)
To: zzjas98
Cc: Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny, nvdimm,
linux-kernel
On Tue, Dec 01, 2026 at 09:16:27PM -0600, Zijie Zhao via B4 Relay wrote:
> From: Zijie Zhao <zzjas98@gmail.com>
>
> nvdimm_security_store() parses the 'security' attribute with sscanf()
> into the uninitialized stack arrays cmd, keystr and nkeystr, but only
> rejects a return value below 1. Commands that take key ids are then
> parsed with kstrtouint() on keystr and nkeystr according to
> ops[i].args, regardless of how many fields sscanf() actually filled.
Hi Zijie,
Thanks for catching the missing argument check. I'm going to NAK this
because it seems to be simply a forward of a static checker result
without the needed human effort added. See below...
>
> Writing e.g. "disable" without a key id, or "update 1" without the new
> key id, makes kstrtouint() parse uninitialized stack memory that is not
> guaranteed to be NUL terminated within the array. If that memory parses
> as a number, a key id taken from stale stack contents is passed to the
> security operation handler instead of the write failing with -EINVAL,
> and the handler proceeds if its state and key checks succeed.
This depends on the kernel configuration. With CONFIG_INIT_STACK_ALL_ZERO,
the missing key ID buffer is zeroed, kstrtouint() sees an empty string,
and the write already fails with -EINVAL. Also, ndctl supplies the
required key IDs, so the case described here is a malformed privileged
write directly to the sysfs attribute.
Please rewrite the commit message to explain that context, the expected
behavior, and the impact on affected kernels. Say what you observed at
runtime and which effects you inferred from the code. Please name the
analysis tool and only summarize what it reported.
>
> Return -EINVAL when sscanf() converted fewer fields than the matched
> command requires.
>
> Found by a static analysis tool.
>
> The fix and this changelog were drafted with AI assistance (Claude Code,
> with a second review by OpenAI Codex), then reviewed by the submitter.
>
> Fixes: 03b65b22ada8 ("acpi/nfit, libnvdimm: Add disable passphrase support to Intel nvdimm.")
> Signed-off-by: Zijie Zhao <zzjas98@gmail.com>
> ---
> Testing:
> - Compile-tested on x86_64 (allmodconfig, W=1):
> drivers/nvdimm/security.o; no new warnings.
> - checkpatch.pl --strict: no errors or warnings.
The existing ndctl security tests send complete commands through ndctl,
so they miss the short-write path. I expect a regression test to
accompany this fix. Please add a case that writes directly to the
security attribute, checks for EINVAL when a required key ID is
missing, and verifies that the security state is unchanged.
If you're unable to reproduce the problem or run the ndctl security
tests, please say so explicitly in v2 and describe what you were able
to verify. If you plan to continue contributing to libnvdimm, I
encourage you to get the nfit_test setup running and learn to use the
ndctl test suite. Being able to exercise these paths is part of
validating fixes in this subsystem.
> ---
> drivers/nvdimm/security.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/nvdimm/security.c b/drivers/nvdimm/security.c
> index e41f6951ca0f..1fc6c0ad1460 100644
> --- a/drivers/nvdimm/security.c
> +++ b/drivers/nvdimm/security.c
> @@ -534,6 +534,8 @@ ssize_t nvdimm_security_store(struct device *dev, const char *buf, size_t len)
> break;
> if (i >= ARRAY_SIZE(ops))
> return -EINVAL;
> + if (rc < ops[i].args)
> + return -EINVAL;
This checks the number of conversions, but that count doesn't establish
that the input contained the required tokens. A width-limited %s can
consume part of an overlong key ID, and the next conversion can pick up
the rest as a second ID. The parser also ignores extra fields. Please
account for both cases in v2 and explain which malformed inputs the
patch is intended to reject. In particular, the field count alone
doesn't catch a missing second ID when the first token is split.
-- Alison
> if (ops[i].args > 1)
> rc = kstrtouint(keystr, 0, &key);
> if (rc >= 0 && ops[i].args > 2)
>
> ---
> base-commit: 5a277aa7eb4d4f1f05534d6c3dcb1a9fa4f6b8f2
> change-id: 20261201-kbug-l936-0b24cea11668
>
> Best regards,
> --
> Zijie Zhao <zzjas98@gmail.com>
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 22:55 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 3:16 [PATCH] libnvdimm/security: Reject security commands with missing key ids Zijie Zhao
2026-12-02 3:16 ` Zijie Zhao via B4 Relay
2026-12-02 22:55 ` Alison Schofield
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.