Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvmet-auth: lock DH-HMAC-CHAP key clearing
@ 2026-11-13  9:42 Yao Sang
  2026-11-20 18:01 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Yao Sang @ 2026-11-13  9:42 UTC (permalink / raw)
  To: linux-nvme
  Cc: Hannes Reinecke, Christoph Hellwig, Sagi Grimberg,
	Chaitanya Kulkarni, Keith Busch, Yao Sang

Clearing a key without nvmet_config_sem can race with configfs reads
or authentication and cause a use-after-free.

Use the same locked path to set and clear keys.

Fixes: bb2df18958b4 ("nvmet-auth: allow to clear DH-HMAC-CHAP keys")
Signed-off-by: Yao Sang <sangyao@kylinos.cn>
---
 drivers/nvme/target/auth.c | 43 ++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index ccba84b1344e..3cbe90a432ef 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -22,39 +22,32 @@
 int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
 		       bool set_ctrl)
 {
-	unsigned char key_hash;
-	char *dhchap_secret;
-
-	if (!strlen(secret)) {
-		if (set_ctrl) {
-			kfree(host->dhchap_ctrl_secret);
-			host->dhchap_ctrl_secret = NULL;
-			host->dhchap_ctrl_key_hash = 0;
-		} else {
-			kfree(host->dhchap_secret);
-			host->dhchap_secret = NULL;
-			host->dhchap_key_hash = 0;
+	unsigned char key_hash = 0;
+	char *dhchap_secret = NULL;
+
+	if (*secret) {
+		if (sscanf(secret, "DHHC-1:%hhd:%*s", &key_hash) != 1)
+			return -EINVAL;
+		if (key_hash > 3) {
+			pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
+				key_hash);
+			return -EINVAL;
 		}
-		return 0;
-	}
-	if (sscanf(secret, "DHHC-1:%hhd:%*s", &key_hash) != 1)
-		return -EINVAL;
-	if (key_hash > 3) {
-		pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
-			 key_hash);
-		return -EINVAL;
+
+		dhchap_secret = kstrdup(secret, GFP_KERNEL);
+		if (!dhchap_secret)
+			return -ENOMEM;
+		dhchap_secret = strim(dhchap_secret);
 	}
-	dhchap_secret = kstrdup(secret, GFP_KERNEL);
-	if (!dhchap_secret)
-		return -ENOMEM;
+
 	down_write(&nvmet_config_sem);
 	if (set_ctrl) {
 		kfree(host->dhchap_ctrl_secret);
-		host->dhchap_ctrl_secret = strim(dhchap_secret);
+		host->dhchap_ctrl_secret = dhchap_secret;
 		host->dhchap_ctrl_key_hash = key_hash;
 	} else {
 		kfree(host->dhchap_secret);
-		host->dhchap_secret = strim(dhchap_secret);
+		host->dhchap_secret = dhchap_secret;
 		host->dhchap_key_hash = key_hash;
 	}
 	up_write(&nvmet_config_sem);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] nvmet-auth: lock DH-HMAC-CHAP key clearing
  2026-11-13  9:42 [PATCH] nvmet-auth: lock DH-HMAC-CHAP key clearing Yao Sang
@ 2026-11-20 18:01 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2026-11-20 18:01 UTC (permalink / raw)
  To: Yao Sang
  Cc: linux-nvme, Hannes Reinecke, Sagi Grimberg, Chaitanya Kulkarni,
	Keith Busch

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20 18:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-13  9:42 [PATCH] nvmet-auth: lock DH-HMAC-CHAP key clearing Yao Sang
2026-11-20 18:01 ` Christoph Hellwig

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