From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796337688; b=EJS2YwVRAI9Dpue3VqHOIrRbP9nWJDaNj2DRXRuDPQKbOza6nhDRfSFArWE5uOIt0Oaf WlN/glS5qEfYcp0NmxZztrhz0whCavxZNtAHfQuVCzylfnVU+F/+WjFU1mQcChiiKS9Gw szNBMYZ6fQDWsj0C6HnRWrPyNSBduZh2B4swPnO8wBIrNHycXUo+3W3iCjDN3s2cNNLUq AQMS95ivcDCAI1bzq3WIFZrL7vqpTu548+cNEv3dE5BOFg7Q6du9cpXVwKUPRlrVFYb4e Uzoukv48mygNvxK0yD+FYcF/BI1a0gjKRQVWvuto+zA0xOTRc3tK2fgp0rkxR4i7SVQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796337688; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=cS2Ca/5BxRLAQL3/x7oo1qvDKeBzabVDHWGmCzAgQv8=; b=rAp31+AV0x+xr+M08B6tv8oM22k2wMUdiGMgoPzk5YL7+ImcLc9wggfayl7/TzIgkIOn 2b3B1HK/sK9enPOVoTJwywSwe0bSMDW4t5FNvkXiyH3mS0BMEdFRoSARjyLQVkDnhTHAE w5zT/BjheNKcrgBLIeeTQ3B/Ybt4Sm+f2f7B5LWdeREW14o3w64hbmu1oB7RRliTYBqWR x7TmtbLq3DqEYfqfM70KBWdusZKo3KEzwUI5uNfAH+00YHdNWWlu7RUpafXyoidaEu5Su 5YXUPdAgMog75ZfPKLkC53l89v5uswTnEwPi8IxDFtTmz6V1rA+IA10Fa8n2GhiMtAA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RIA5E1LX; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5382D606A332; Thu, 3 Dec 2026 22:41:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RIA5E1LX Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796337688; b=H1rVcLhG5oGtxMgRP9LkXC7enSYa3XYDwWci06IzR3GQuREs7VHw9cwVdATq6f0yd91v nKog+CgH4sC2Z06rHp0runlYxA6k1PKRgQVPDc8j6rssul/HGK3d8pG7CF7WenMx82Z7W tqoq6V8EFRfOD6dUM5z0gXmUcd4fByiZeI9H+VOOKxgNjLIHil5ZNxFhKPzPMpSgDsEpx FBLH5AOmh5EUJdQK4fMM+DDTW14cvHSZirIQyHw2wfnvn+okmJhuV0TbLfZaa543Xmq6w xVa+qWgCJ2zJN118bLasT7t6Z4pATxBndFkk8HfiRvu44jTqvc9yKQw/v0kVQRXbt/g== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796337688; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=cS2Ca/5BxRLAQL3/x7oo1qvDKeBzabVDHWGmCzAgQv8=; b=nPiAmFPvJwOSLfXFOHakmrCvqT6X3YBDy3s2OcolworThoDAt18ZZiv/R8+tvHA2Pfxe Y22HR8Zo/VNuylM+8iDhfsOiCnnRweupSli6tFKJSwE09eun2Vv0DajyTLVrpQkUr2nWE +rjqhtF6V+dLLUwFOzt72ZM7hkZq+Qf5rJ36YsvSl2KHclb98FfR4K/WkRlXFhgfwUE3f E0q1SQReLsfM/0VKyZA2YsjsxFIdun3sMTrAwshT0PGrE/LmCHVDayCGZ9B3cc+Jn6Jpq EuGb/qMuUosd8AjYexxcVZqv3NDQqJwN174qSFT3oEqRAJUa/La/LBrx6Wu3bOqiBrg== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 41ABC1F0092B; Thu, 3 Dec 2026 22:41:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796337687; bh=cS2Ca/5BxRLAQL3/x7oo1qvDKeBzabVDHWGmCzAgQv8=; h=From:Date:Subject:To:Cc:Reply-To; b=RIA5E1LXSLtL/H6lqH6FxPagM8b/F6eKhBMq6pOG4cD1g+yUGViauS/nhSlNal4kP 3cBQtaEn5oZepgKGa3nx0rmNWtXFBNc155Z1/F+ibMKgJgV0VCbvUl8UAQ9BhDjCi2 D/mi2N4L3nfysmqiD5qkWZ4oVkJnKqeSAPzK9twr0q8DMMfvCZAq2nVGfEQVYO56gt 8x5V6RDtSISf8IISpXAGBi1crvDk7sk6sgH/L0m60V625HyJAjx9Kwsv0H4g9/KZE0 kiUBDXW5A0H6JsoCv6smc1MFq8oSg/wkraUw7GkS4kBFQFiFH6RWkwN+x3+bmi2EKg l4tBejPGPAR5g== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id ADABD4FB3D; Thu, 3 Dec 2026 22:41:26 +0000 (UTC) From: Zijie Zhao via B4 Relay Date: Thu, 03 Dec 2026 16:41:27 -0600 Subject: [PATCH v2] libnvdimm/security: Reject malformed security commands X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261203-b4-l936-v2-v2-1-75186c6af4aa@gmail.com> X-B4-Tracking: v=1; b=H4sIABbwEWsC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyjHQUlJIzE vPSU3UzU4B8JSMDIzNDIwNj3SQT3RxLYzPdMiNdA4skozQjgzSzZAsTJaCGgqLUtMwKsGHRsRB +cWlSVmpyCcgEpdpaAGebTYRuAAAA X-Change-ID: 20261203-b4-l936-v2-08b2f20f6c84 To: Dan Williams , Vishal Verma , Dave Jiang , Alison Schofield Cc: Ira Weiny , nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org, Zijie Zhao X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796337686; l=4890; i=zzjas98@gmail.com; s=20261126; h=from:subject:message-id; bh=CoMVC1UWG49AmAUDHD/+Rrn3ZuiQcD0i8dKtL/0M9NU=; b=v1Y/zEmkss0lN0fXWevvakZovz6pHg/ejKkiySO9TqfclMFvMGaMX9FrTYvH8/fbbbobRfLYZ taf0TXxjSRwACiVFDIZ5Ip1RmAHgftIYS3efgBZNhNFs0vTGL69VYtX X-Developer-Key: i=zzjas98@gmail.com; a=ed25519; pk=JH1SXIjc6vLfCjl6NzH9q8FTNJnOj4FyHWYhz1uELOE= X-Endpoint-Received: by B4 Relay for zzjas98@gmail.com/20261126 with auth_id=1645 X-Original-From: Zijie Zhao Reply-To: zzjas98@gmail.com Content-Transfer-Encoding: 7bit From: Zijie Zhao 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 ") 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 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 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. Fixes: 03b65b22ada8 ("acpi/nfit, libnvdimm: Add disable passphrase support to Intel nvdimm.") Signed-off-by: Zijie Zhao --- 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