Skip to contentSkip to conversation

nvme: use kfree_sensitive() for DH-HMAC-CHAP secrets

PATCH Applied Martin George 5 participants 9 messages Nov 16 – Nov 20 NVMe

lore
Share & tools
lore
#0 lore raw

cover nvme: use kfree_sensitive() for DH-HMAC-CHAP secrets

The host side stores the raw DH-HMAC-CHAP secret ("DHHC-1:...") but frees it with plain kfree() in a few places, leaving the secret in freed slab until the memory is reused. Switch those kfrees to kfree_sensitive() so the contents are wiped on free.

This is a hardening-only series; it fixes no functional bug and changes no behaviour.

Martin George (2):
nvme-sysfs: use kfree_sensitive() for DH-HMAC-CHAP secrets
nvme-fabrics: use kfree_sensitive() for the old DH-HMAC-CHAP secret

 drivers/nvme/host/fabrics.c |  4 ++--
 drivers/nvme/host/sysfs.c   | 12 ++++++------
 2 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 nvme-sysfs: use kfree_sensitive() for DH-HMAC-CHAP secrets

dhchap_secret_store() and dhchap_ctrl_secret_store() free the raw "DHHC-1:..." secret with plain kfree(), leaving it in the freed slab. As these are the runtime rotation handlers, every secret change also leaves the previous secret behind. Free them with kfree_sensitive() instead.

---
 drivers/nvme/host/sysfs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/nvme/host/sysfs.c +6 -6
index 9672ca3f74d2..35f871e3e0c0 100644
@@ -850,10 +850,10 @@ static ssize_t dhchap_secret_store(struct device *dev,
 
 		ret = nvme_auth_parse_key(dhchap_secret, &key);
 		if (ret) {
-			kfree(dhchap_secret);
+			kfree_sensitive(dhchap_secret);
 			return ret;
 		}
-		kfree(opts->dhchap_secret);
+		kfree_sensitive(opts->dhchap_secret);
 		opts->dhchap_secret = dhchap_secret;
 		host_key = ctrl->host_key;
 		mutex_lock(&ctrl->dhchap_auth_mutex);
@@ -861,7 +861,7 @@ static ssize_t dhchap_secret_store(struct device *dev,
 		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		nvme_auth_free_key(host_key);
 	} else
-		kfree(dhchap_secret);
+		kfree_sensitive(dhchap_secret);
 	/* Start re-authentication */
 	dev_info(ctrl->device, "re-authenticating controller\n");
 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
@@ -907,10 +907,10 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
 
 		ret = nvme_auth_parse_key(dhchap_secret, &key);
 		if (ret) {
-			kfree(dhchap_secret);
+			kfree_sensitive(dhchap_secret);
 			return ret;
 		}
-		kfree(opts->dhchap_ctrl_secret);
+		kfree_sensitive(opts->dhchap_ctrl_secret);
 		opts->dhchap_ctrl_secret = dhchap_secret;
 		ctrl_key = ctrl->ctrl_key;
 		mutex_lock(&ctrl->dhchap_auth_mutex);
@@ -918,7 +918,7 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
 		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		nvme_auth_free_key(ctrl_key);
 	} else
-		kfree(dhchap_secret);
+		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
#5 lore raw

PATCH 2/2 nvme-fabrics: use kfree_sensitive() for the old DH-HMAC-CHAP secret

In reply to Martin George

When a connect string overrides dhchap_secret or dhchap_ctrl_secret, nvmf_parse_options() frees the previous raw secret with plain kfree(). The invalid-input path already uses kfree_sensitive(), so do the same for the replaced secret.

---
 drivers/nvme/host/fabrics.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/fabrics.c +2 -2
index c262538523fb..5ed91557f070 100644
@@ -1026,7 +1026,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 				ret = -EINVAL;
 				goto out;
 			}
-			kfree(opts->dhchap_secret);
+			kfree_sensitive(opts->dhchap_secret);
 			opts->dhchap_secret = p;
 			break;
 		case NVMF_OPT_DHCHAP_CTRL_SECRET:
@@ -1041,7 +1041,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 				ret = -EINVAL;
 				goto out;
 			}
-			kfree(opts->dhchap_ctrl_secret);
+			kfree_sensitive(opts->dhchap_ctrl_secret);
 			opts->dhchap_ctrl_secret = p;
 			break;
 		case NVMF_OPT_TLS:
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/2] nvme-sysfs: use kfree_sensitive() for DH-HMAC-CHAP secrets

In reply to Martin George
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] nvme: double-free and…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] nvme: double-free and use-after-free in nvme_ctrl_dhchap_secret_store() [1]
--

commit 86ec9e603dccdb522db4b07402c30b2c18b2a11b
Author: Martin George <[email protected]>

nvme-sysfs: use kfree_sensitive() for DH-HMAC-CHAP secrets

This commit replaces kfree() with kfree_sensitive() when freeing DH-HMAC-CHAP secret strings in dhchap_secret_store() and dhchap_ctrl_secret_store().

Quoted 7 lines
drivers/nvme/host/sysfs.c +0 -0
index 10018c6356b62..5a195b8971f21 100644
@@ -849,10 +849,10 @@ static ssize_t dhchap_secret_store(struct device *dev,
 
 		ret = nvme_auth_parse_key(dhchap_secret, &key);

drivers/nvme/host/sysfs.c static ssize_t dhchap_secret_store(struct device *dev, · line 858

Quoted 10 lines
drivers/nvme/host/sysfs.c +2 -2
 		if (ret) {
-			kfree(dhchap_secret);
+			kfree_sensitive(dhchap_secret);
 			return ret;
 		}
-		kfree(opts->dhchap_secret);
+		kfree_sensitive(opts->dhchap_secret);
 		opts->dhchap_secret = dhchap_secret;
 		host_key = ctrl->host_key;
 		mutex_lock(&ctrl->dhchap_auth_mutex);

[Severity: Critical]
This problem wasn't introduced by this patch, but can concurrent sysfs accesses
[1] trigger a double free or use-after-free on opts->dhchap_secret and ctrl->host_key?

In dhchap_secret_store(), opts->dhchap_secret is freed and replaced without holding a lock, and ctrl->host_key is read before acquiring ctrl->dhchap_auth_mutex:

CPU0 CPU1
dhchap_secret_store() dhchap_secret_store()
kfree_sensitive(opts->dhchap_secret)
kfree_sensitive(opts->dhchap_secret)
<-- double free
opts->dhchap_secret = dhchap_secret
host_key = ctrl->host_key

                                        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)
                                        mutex_lock(&ctrl->dhchap_auth_mutex)
                                        ctrl->host_key = key
                                        mutex_unlock(&ctrl->dhchap_auth_mutex)
                                        nvme_auth_free_key(host_key)
                                          <-- double free

A concurrent dhchap_secret_show() or strcmp() in dhchap_secret_store() can also read opts->dhchap_secret while it is being freed, causing a use-after-free. The same unlocked accesses exist in dhchap_ctrl_secret_store() and dhchap_ctrl_secret_show() for opts->dhchap_ctrl_secret and ctrl->ctrl_key.

[1] https://sashiko.dev/#/bug/linux-d9bcd9ea-2b96-4998-b954-e0e2557a158a

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] nvme-sysfs: use kfree_sensitive() for DH-HMAC-CHAP secrets

In reply to Martin George
Quoted 11 lines · Martin George

dhchap_secret_store() and dhchap_ctrl_secret_store() free the raw "DHHC-1:..." secret with plain kfree(), leaving it in the freed slab. As these are the runtime rotation handlers, every secret change also leaves the previous secret behind. Free them with kfree_sensitive() instead.

---
  drivers/nvme/host/sysfs.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] nvme-fabrics: use kfree_sensitive() for the old DH-HMAC-CHAP secret

In reply to Martin George
Quoted 24 lines · Martin George

When a connect string overrides dhchap_secret or dhchap_ctrl_secret, nvmf_parse_options() frees the previous raw secret with plain kfree(). The invalid-input path already uses kfree_sensitive(), so do the same for the replaced secret.

---
  drivers/nvme/host/fabrics.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/fabrics.c +1 -1
index c262538523fb..5ed91557f070 100644
@@ -1026,7 +1026,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  				ret = -EINVAL;
  				goto out;
  			}
-			kfree(opts->dhchap_secret);
+			kfree_sensitive(opts->dhchap_secret);
  			opts->dhchap_secret = p;
  			break;
  		case NVMF_OPT_DHCHAP_CTRL_SECRET:

drivers/nvme/host/fabrics.c static int nvmf_parse_options(struct nvmf_ctrl_options *opts, · line 1047

Quoted 8 lines
drivers/nvme/host/fabrics.c +1 -1
@@ -1041,7 +1041,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  				ret = -EINVAL;
  				goto out;
  			}
-			kfree(opts->dhchap_ctrl_secret);
+			kfree_sensitive(opts->dhchap_ctrl_secret);
  			opts->dhchap_ctrl_secret = p;
  			break;
  		case NVMF_OPT_TLS:

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
maintainer Applied
#8 lore raw
In reply to Martin George
Quoted 9 lines · Martin George

The host side stores the raw DH-HMAC-CHAP secret ("DHHC-1:...") but frees it with plain kfree() in a few places, leaving the secret in freed slab until the memory is reused. Switch those kfrees to kfree_sensitive() so the contents are wiped on free.

This is a hardening-only series; it fixes no functional bug and changes no behaviour.

[...]

Thanks, applied to nvme-7.4.

[1/2] nvme-sysfs: use kfree_sensitive() for DH-HMAC-CHAP secrets

      commit: 7fce38923253c27650835c91201b222214aeb149
[2/2] nvme-fabrics: use kfree_sensitive() for the old DH-HMAC-CHAP secret
      commit: 68099cf27253a8c89d703a282d3d811446d9453b

--
Keith Busch <[email protected]>