Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Martin George <martinus.gpy@gmail.com>
To: linux-nvme@lists.infradead.org
Cc: hch@lst.de, kbusch@kernel.org, sagi@grimberg.me, hare@kernel.org
Subject: [PATCH] nvme-sysfs: serialize DH-HMAC-CHAP secret updates
Date: Wed, 18 Nov 2026 15:33:47 +0530	[thread overview]
Message-ID: <20261118100347.14832-1-marting@netapp.com> (raw)

dhchap_secret_store() and dhchap_ctrl_secret_store() free/replace the
stored secret and swap ctrl->host_key / ctrl->ctrl_key, but hold
dhchap_auth_mutex only around the key assignment; the free/replace and
the show handlers run unlocked. Concurrent writes to the same attribute
can thus double-free the secret or the old key, and a concurrent show()
can read the secret as it is freed (use-after-free).

Hold dhchap_auth_mutex across the whole update, and in the show handlers.
Keep nvme_auth_stop() outside the mutex, since it waits for
dhchap_auth_work, which itself takes dhchap_auth_mutex.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20261116100244.7824-1-marting@netapp.com?part=1
Signed-off-by: Martin George <marting@netapp.com>
---
 drivers/nvme/host/sysfs.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 35f871e3e0c0..5801431f4649 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -819,10 +819,16 @@ static ssize_t dhchap_secret_show(struct device *dev,
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
 	struct nvmf_ctrl_options *opts = ctrl->opts;
+	int ret;
 
+	mutex_lock(&ctrl->dhchap_auth_mutex);
 	if (!opts->dhchap_secret)
-		return sysfs_emit(buf, "none\n");
-	return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
+		ret = sysfs_emit(buf, "none\n");
+	else
+		ret = sysfs_emit(buf, "%s\n", opts->dhchap_secret);
+	mutex_unlock(&ctrl->dhchap_auth_mutex);
+
+	return ret;
 }
 
 static ssize_t dhchap_secret_store(struct device *dev,
@@ -844,24 +850,27 @@ static ssize_t dhchap_secret_store(struct device *dev,
 		return -ENOMEM;
 	memcpy(dhchap_secret, buf, count);
 	nvme_auth_stop(ctrl);
+	mutex_lock(&ctrl->dhchap_auth_mutex);
 	if (strcmp(dhchap_secret, opts->dhchap_secret)) {
 		struct nvme_dhchap_key *key, *host_key;
 		int ret;
 
 		ret = nvme_auth_parse_key(dhchap_secret, &key);
 		if (ret) {
+			mutex_unlock(&ctrl->dhchap_auth_mutex);
 			kfree_sensitive(dhchap_secret);
 			return ret;
 		}
 		kfree_sensitive(opts->dhchap_secret);
 		opts->dhchap_secret = dhchap_secret;
 		host_key = ctrl->host_key;
-		mutex_lock(&ctrl->dhchap_auth_mutex);
 		ctrl->host_key = key;
 		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		nvme_auth_free_key(host_key);
-	} else
+	} else {
+		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		kfree_sensitive(dhchap_secret);
+	}
 	/* Start re-authentication */
 	dev_info(ctrl->device, "re-authenticating controller\n");
 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
@@ -876,10 +885,16 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
 	struct nvmf_ctrl_options *opts = ctrl->opts;
+	int ret;
 
+	mutex_lock(&ctrl->dhchap_auth_mutex);
 	if (!opts->dhchap_ctrl_secret)
-		return sysfs_emit(buf, "none\n");
-	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
+		ret = sysfs_emit(buf, "none\n");
+	else
+		ret = sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
+	mutex_unlock(&ctrl->dhchap_auth_mutex);
+
+	return ret;
 }
 
 static ssize_t dhchap_ctrl_secret_store(struct device *dev,
@@ -901,24 +916,27 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
 		return -ENOMEM;
 	memcpy(dhchap_secret, buf, count);
 	nvme_auth_stop(ctrl);
+	mutex_lock(&ctrl->dhchap_auth_mutex);
 	if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
 		struct nvme_dhchap_key *key, *ctrl_key;
 		int ret;
 
 		ret = nvme_auth_parse_key(dhchap_secret, &key);
 		if (ret) {
+			mutex_unlock(&ctrl->dhchap_auth_mutex);
 			kfree_sensitive(dhchap_secret);
 			return ret;
 		}
 		kfree_sensitive(opts->dhchap_ctrl_secret);
 		opts->dhchap_ctrl_secret = dhchap_secret;
 		ctrl_key = ctrl->ctrl_key;
-		mutex_lock(&ctrl->dhchap_auth_mutex);
 		ctrl->ctrl_key = key;
 		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		nvme_auth_free_key(ctrl_key);
-	} else
+	} else {
+		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		kfree_sensitive(dhchap_secret);
+	}
 	/* Start re-authentication */
 	dev_info(ctrl->device, "re-authenticating controller\n");
 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
-- 
2.53.0



                 reply	other threads:[~2026-11-18 10:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261118100347.14832-1-marting@netapp.com \
    --to=martinus.gpy@gmail.com \
    --cc=hare@kernel.org \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /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