Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-sysfs: serialize DH-HMAC-CHAP secret updates
@ 2026-11-18 10:03 Martin George
  0 siblings, 0 replies; only message in thread
From: Martin George @ 2026-11-18 10:03 UTC (permalink / raw)
  To: linux-nvme; +Cc: hch, kbusch, sagi, hare

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



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-18 10:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 10:03 [PATCH] nvme-sysfs: serialize DH-HMAC-CHAP secret updates Martin George

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox