Skip to contentSkip to conversation

nvme-sysfs: serialize DH-HMAC-CHAP secret updates

PATCH Applied Martin George 3 participants 3 messages Nov 18 – Nov 20 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-sysfs: serialize DH-HMAC-CHAP secret updates

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.

---
 drivers/nvme/host/sysfs.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
drivers/nvme/host/sysfs.c +26 -8
index 35f871e3e0c0..5801431f4649 100644
@@ -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);
Signature · 1 line
2.53.0
maintainer Applied
#2 lore raw
In reply to Martin George
Quoted 8 lines · Martin George

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).

[...]

Thanks, applied to nvme-7.4.

[1/1] nvme-sysfs: serialize DH-HMAC-CHAP secret updates

      commit: bdd56686afbc4e84eebff90ae7be7c1c62e21c49

--
Keith Busch <[email protected]>