Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hannes Reinecke" <hare@kernel.org>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret
Date: Fri, 20 Nov 2026 11:31:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-225046@kernel.org> (raw)
In-Reply-To: <20261120-dhchap-keyring-v4-v4-6-5f8c7d9926ee@kernel.org>

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] nvme-fabrics: wrong key revoked in nvmf_free_options() for
  dhchap_ctrl_key
- [High] nvme-auth: empty/prefix match and self-revocation in
  nvme_auth_extract_key()
- [High] nvme-fabrics: link failure calling nvme_auth_extract_key()
  without NVME_AUTH
- [Medium] nvme: unlocked access to dhchap keys and flags in sysfs show
  callbacks
- [Medium] nvme: broken sysfs visibility for dhchap_key in
  nvme_dev_attrs_are_visible()
- [Medium] nvme-auth: host_key leak and stale flags on
  nvme_auth_init_ctrl() error
- [Medium] nvmet: memory leak of keydata buffer in
  nvmet_host_dhchap_ctrl_key_store()
- [Low] nvme: unsigned count and missing newline in
  dhchap_ctrl_secret_show()
- [Low] nvme-auth: missing keyring param in nvme_auth_extract_key()
  kernel-doc
--

commit 2b7a36fa0b1a7939d6a4d7f1cdc10818701d7d40
Author: Hannes Reinecke <hare@kernel.org>

nvme: allow to pass in key description as dhchap secret

This commit updates nvme_auth_extract_key() to look up pre-populated
DH-HMAC-CHAP keys in the keyring before falling back to generating an
ephemeral key from raw secret material, and tracks whether keys were
auto-generated across the host and target drivers so only generated keys
are revoked on release.

> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
> index b7092deed9b06..c65bfafe9e3fc 100644
> --- a/drivers/nvme/common/auth.c
> +++ b/drivers/nvme/common/auth.c
> @@ -143,20 +143,32 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
>  /**
>   * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
>   *
> - * @secret: key data
> - * @secret_len: length of @secret
> + * @input: key serial or key data
> + * @input_len: length of @input
> + * @generated: indicate whether a key has been generated
>   *
> - * Extracts a dhchap key from @secret.
> + * Extracts or generates a key from @input.
> + * @input can either be a key serial or raw key data; in
> + * the latter case a key is generated from @input and
> + * @generated is set to 'true'.
>   *
>   * Returns the dhchap key or an error pointer on failure.
>   */
> -struct key *nvme_auth_extract_key(struct key *keyring, const char *secret,
> -				  size_t secret_len)
> +struct key *nvme_auth_extract_key(struct key *keyring, const char *input,
> +				  size_t input_len, bool *generated)

[Severity: Low]
The kernel-doc comment for nvme_auth_extract_key() omits the @keyring
parameter, which triggers a W=1 kernel-doc warning.  Also, the comment and
commit message describe @input as a key serial number even though
nvme_dhchap_psk_lookup() searches by key description, and the prototype in
include/linux/nvme-auth.h still names the parameters secret and secret_len.

>  {
>  	struct key *key;
>  
> -	key = nvme_dhchap_psk_create(keyring, secret, secret_len);
> +	/* Check if @input is a key serial number */
> +	key = nvme_dhchap_psk_lookup(keyring, input);
>  	if (!IS_ERR(key)) {
> +		*generated = false;
> +		return key;
> +	}
> +	/* Generate a key from @input data */
> +	key = nvme_dhchap_psk_create(keyring, input, input_len);
> +	if (!IS_ERR(key)) {
> +		*generated = true;
>  		pr_debug("generated dhchap key %s\n",
>  			 key->description);
>  	}

[Severity: High]
Can calling nvme_dhchap_psk_lookup() here match the wrong key, read out of
bounds, or cause the caller to revoke the key it just looked up?

nvme_dhchap_psk_lookup() searches with nvme_dhchap_psk_match():

drivers/nvme/common/keyring.c:nvme_dhchap_psk_match() {
	...
	match_id = match_data->raw_data;
	match_len = strlen(match_id);
	pr_debug("%s: match '%s' '%s' len %zd\n",
		 __func__, match_id, key->description, match_len);

	return !memcmp(key->description, match_id, match_len);
}

Because nvme_dhchap_psk_match() compares match_len bytes with memcmp()
instead of strcmp(), and nvme_auth_extract_key() does not reject
input_len == 0:

1. Writing a single newline to sysfs dhchap_secret_store() or
   dhchap_ctrl_secret_store() passes an empty string (match_len == 0), so
   !memcmp(key->description, "", 0) matches the first dhchap key in the
   keyring with *generated = false.
2. For non-empty inputs, memcmp() prefix-matches any key whose description
   starts with input when match_len < strlen(key->description), and reads
   past the end of the key->description allocation when match_len >
   strlen(key->description) (for example, when passing a raw DHHC-1:...
   secret while a key with a shorter description is in the keyring).
3. If nvme_dhchap_psk_lookup() returns the controller's or target host's
   currently active auto-generated key (either via an empty write or by
   writing the current key's UUID description to dhchap_secret_store(),
   dhchap_ctrl_secret_store(), or nvmet_auth_set_key()), the store path
   sees that old_key was auto-generated and calls key_revoke(old_key)
   without checking key == old_key, revoking the key it then keeps active.

> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
> index c189096f825ea..470a6e7e43644 100644
> --- a/drivers/nvme/host/auth.c
> +++ b/drivers/nvme/host/auth.c
[ ... ]
> @@ -1071,6 +1081,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>  			 key_serial(ctrl->opts->dhchap_key));
>  		return -ENOKEY;
>  	}
> +	ctrl->host_key_generated = ctrl->opts->dhchap_key_generated;
>  	down_read(&ctrl->host_key->sem);
>  	ret = key_validate(ctrl->host_key);
>  	up_read(&ctrl->host_key->sem);
> @@ -1097,6 +1108,8 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>  				 key_serial(ctrl->opts->dhchap_ctrl_key));
>  			return -ENOKEY;
>  		}
> +		ctrl->ctrl_key_generated =
> +			ctrl->opts->dhchap_ctrl_key_generated;
>  		down_read(&ctrl->ctrl_key->sem);
>  		ret = key_validate(ctrl->ctrl_key);
>  		up_read(&ctrl->ctrl_key->sem);

[Severity: Medium]
If acquiring or validating ctrl->ctrl_key fails in nvme_auth_init_ctrl(),
does this leak ctrl->host_key and leave stale *_key_generated flags set?

On failure of key_get(ctrl->opts->dhchap_ctrl_key) or
key_validate(ctrl->ctrl_key), nvme_auth_init_ctrl() returns -ENOKEY or
-EKEYREVOKED without calling nvme_auth_clear_host_key(ctrl).  In
nvme_init_ctrl(), a failure from nvme_auth_init_ctrl() jumps to
out_release_instance before device_initialize(&ctrl->ctrl_device), so
nvme_auth_free() never runs and the ctrl->host_key reference is leaked.
The error paths after setting ctrl->host_key_generated and
ctrl->ctrl_key_generated also clear the key pointer without resetting the
corresponding generated boolean to false.

> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
> index 7ed8618257ca9..cd728768a68fc 100644
> --- a/drivers/nvme/host/fabrics.c
> +++ b/drivers/nvme/host/fabrics.c
[ ... ]
> @@ -1089,7 +1091,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>  	if (host_secret) {
>  		pr_debug("lookup host identity '%s'\n", host_secret);
>  		key = nvme_auth_extract_key(opts->keyring, host_secret,
> -					    strlen(host_secret));
> +					    strlen(host_secret),
> +					    &opts->dhchap_key_generated);
>  		if (IS_ERR(key)) {
>  			ret = PTR_ERR(key);
>  			goto out;
> @@ -1104,7 +1107,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>  		}
>  		pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
>  		key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
> -					    strlen(ctrl_secret));
> +					    strlen(ctrl_secret),
> +					    &opts->dhchap_ctrl_key_generated);
>  		if (IS_ERR(key)) {
>  			ret = PTR_ERR(key);
>  			goto out;

[Severity: High]
Does calling nvme_auth_extract_key() unconditionally in nvmf_parse_options()
break linking when CONFIG_NVME_FABRICS is enabled without CONFIG_NVME_AUTH?

CONFIG_NVME_FABRICS does not select CONFIG_NVME_AUTH, and
include/linux/nvme-auth.h does not provide a !CONFIG_NVME_AUTH stub for
nvme_auth_extract_key(), so builds with CONFIG_NVME_HOST_AUTH disabled (or
CONFIG_NVME_FABRICS=y and CONFIG_NVME_AUTH=m) fail to link.

> @@ -1327,15 +1331,23 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
>  	kfree(opts->host_traddr);
>  	kfree(opts->host_iface);
>  	if (opts->dhchap_key) {
> -		pr_debug("revoke dhchap host key %08x\n",
> +		if (opts->dhchap_key_generated) {
> +			pr_debug("revoke dhchap host key %08x\n",
> +				 key_serial(opts->dhchap_key));
> +			key_revoke(opts->dhchap_key);
> +		}
> +		pr_debug("drop dhchap host key %08x\n",
>  			 key_serial(opts->dhchap_key));
> -		key_revoke(opts->dhchap_key);
>  		key_put(opts->dhchap_key);
>  	}
>  	if (opts->dhchap_ctrl_key) {
> -		pr_debug("revoke dhchap ctrl key %08x\n",
> +		if (opts->dhchap_ctrl_key_generated) {
> +			pr_debug("revoke dhchap ctrl key %08x\n",
> +				 key_serial(opts->dhchap_ctrl_key));
> +			key_revoke(opts->dhchap_key);
> +		}
> +		pr_debug("drop dhchap ctrl key %08x\n",
>  			 key_serial(opts->dhchap_ctrl_key));
> -		key_revoke(opts->dhchap_key);
>  		key_put(opts->dhchap_ctrl_key);
>  	}
>  	kfree(opts);

[Severity: High]
In the opts->dhchap_ctrl_key block, should key_revoke(opts->dhchap_key) be
key_revoke(opts->dhchap_ctrl_key)?

Passing opts->dhchap_key here accesses it after key_put(opts->dhchap_key)
has already dropped the reference (risking a use-after-free), revokes a
pre-populated host key when only the controller key was auto-generated, and
leaves the auto-generated opts->dhchap_ctrl_key unrevoked.

> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
> index 1311f5e29b004..47caed2ce3978 100644
> --- a/drivers/nvme/host/sysfs.c
> +++ b/drivers/nvme/host/sysfs.c
> @@ -828,13 +828,14 @@ static ssize_t dhchap_secret_show(struct device *dev,
>  	down_read(&key->sem);
>  	if (key_validate(key))
>  		count = sysfs_emit(buf, "<invalidated>\n");
> -	else {
> +	else if (ctrl->host_key_generated) {
>  		count = key->type->read(key, buf, PAGE_SIZE);
>  		if (count > 0) {
>  			buf[count] = '\n';
>  			count++;
>  		}
> -	}
> +	} else
> +		count = sysfs_emit(buf, "%s\n", key->description);
>  	up_read(&key->sem);
>  	return count;
>  }
[ ... ]
> @@ -869,19 +872,25 @@ static ssize_t dhchap_secret_store(struct device *dev,
>  	up_read(&key->sem);
>  	if (ret) {
>  		dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
> -		dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
> -		key_revoke(key);
> +		if (generated) {
> +			dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
> +			key_revoke(key);
> +			synchronize_rcu();
> +		}
>  		key_put(key);
>  		kfree_sensitive(dhchap_secret);
>  		return ret;
>  	}
>  	mutex_lock(&ctrl->dhchap_auth_mutex);
>  	old_key = ctrl->host_key;
> -	dev_dbg(ctrl->dev, "revoke host key %08x\n",
> -		key_serial(old_key));
> -	key_revoke(old_key);
> -
> +	if (ctrl->host_key_generated) {
> +		dev_dbg(ctrl->dev, "revoke host key %08x\n",
> +			key_serial(old_key));
> +		key_revoke(old_key);
> +		synchronize_rcu();
> +	}
>  	ctrl->host_key = key;
> +	ctrl->host_key_generated = generated;
>  	mutex_unlock(&ctrl->dhchap_auth_mutex);
>  	key_put(old_key);
>  	kfree_sensitive(dhchap_secret);

[Severity: Medium]
Can dhchap_secret_show(), dhchap_ctrl_secret_show(), dhchap_key_show(), and
dhchap_ctrl_key_show() race with concurrent stores and trigger a
use-after-free or read a mismatched *_key_generated flag?

Unlike nvmet_host_dhchap_key_show() in drivers/nvme/target/configfs.c, the
host sysfs show callbacks read ctrl->host_key / ctrl->ctrl_key and
ctrl->host_key_generated / ctrl->ctrl_key_generated without holding
ctrl->dhchap_auth_mutex or pinning the key with key_get():

CPU0                                  CPU1
dhchap_secret_show()                  dhchap_secret_store()
  key = ctrl->host_key;
                                        mutex_lock(&ctrl->dhchap_auth_mutex);
                                        old_key = ctrl->host_key;
                                        ctrl->host_key = new_key;
                                        ctrl->host_key_generated = true;
                                        mutex_unlock(&ctrl->dhchap_auth_mutex);
                                        key_put(old_key);
  down_read(&key->sem);  <-- use-after-free

Even if old_key still has another reference (such as when old_key was a
pre-populated user key in the keyring), CPU0 can pair old_key with the newly
written ctrl->host_key_generated == true and dump the pre-populated key's
raw secret via key->type->read() instead of its description.

> @@ -906,13 +915,14 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
>  	down_read(&key->sem);
>  	if (key_validate(key))
>  		count = sysfs_emit(buf, "<invalidated>");
> -	else {
> +	else if (ctrl->ctrl_key_generated) {
>  		count = key->type->read(key, buf, PAGE_SIZE);
>  		if (count > 0) {
>  			buf[count] = '\n';
>  			count++;
>  		}
> -	}
> +	} else
> +		count = sysfs_emit(buf, "%s\n", key->description);
>  	up_read(&key->sem);
>  	return count;
>  }

[Severity: Low]
In dhchap_ctrl_secret_show(), count is declared as unsigned size_t (unlike
ssize_t in dhchap_secret_show()), so a negative error return from
key->type->read() would wrap to a large positive value, pass count > 0, and
write out of bounds at buf[count] = '\n'.  Also,
sysfs_emit(buf, "<invalidated>") is missing the trailing '\n' present in the
other show callbacks.

[ ... ]

> @@ -1066,8 +1088,12 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
>  #ifdef CONFIG_NVME_HOST_AUTH
>  	if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
>  		return 0;
> +	if (a == &dev_attr_dhchap_key.attr && ctrl->host_key_generated)
> +		return 0;
>  	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
>  		return 0;
> +	if (a == &dev_attr_dhchap_ctrl_key.attr && ctrl->ctrl_key_generated)
> +		return 0;
>  #endif
>  
>  	return a->mode;

[Severity: Medium]
Should the visibility checks for dev_attr_dhchap_key.attr and
dev_attr_dhchap_ctrl_key.attr also check !ctrl->opts, and how is attribute
visibility updated when keys change at runtime?

Without a !ctrl->opts check, dhchap_key and dhchap_ctrl_key are exposed in
sysfs on non-Fabrics controllers (such as PCIe controllers, where
ctrl->opts is NULL and the *_key_generated fields are false) even though
dhchap_secret and dhchap_ctrl_secret are hidden.  In addition, because
nvme_dev_attrs_are_visible() is only evaluated when the sysfs group is
created and dhchap_secret_store() / dhchap_ctrl_secret_store() do not call
sysfs_update_group(), switching a controller at runtime between an
auto-generated secret and a pre-populated key description leaves dhchap_key
and dhchap_ctrl_key stuck in their initial visibility state.

> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index 880cf5151588a..319947aa0a40f 100644
> --- a/drivers/nvme/target/configfs.c
> +++ b/drivers/nvme/target/configfs.c
[ ... ]
> @@ -2305,13 +2306,14 @@ static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
>  		down_read(&key->sem);
>  		if (key_validate(key))
>  			ret = sprintf(page, "<invalidated>\n");
> -		else {
> +		else if (host->dhchap_ctrl_key_generated) {
>  			ret = key->type->read(key, page, PAGE_SIZE);
>  			if (ret > 0) {
>  				page[ret] = '\n';
>  				ret++;
>  			}
> -		}
> +		} else
> +			ret = sprintf(page, "%s\n", key->description);
>  		up_read(&key->sem);
>  		key_put(key);
>  	}

[Severity: Medium]
Does nvmet_host_dhchap_ctrl_key_store() just below this function leak
keydata on every write?

drivers/nvme/target/configfs.c:nvmet_host_dhchap_ctrl_key_store() {
	...
	keydata = kstrndup(page, len, GFP_KERNEL);
	if (!keydata)
		return -ENOMEM;

	ret = nvmet_auth_set_key(host, keydata, true);
	...
	return ret < 0 ? ret : count;
}

Unlike nvmet_host_dhchap_key_store(), which calls kfree_sensitive(keydata)
after nvmet_auth_set_key(), nvmet_host_dhchap_ctrl_key_store() returns
without freeing keydata, leaking the buffer and leaving key material in slab
memory.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=6


  reply	other threads:[~2026-11-20 11:31 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-20 11:19   ` sashiko-bot
2026-11-20 11:42     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-20 11:28   ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-20 11:31   ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-20 11:32   ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-20 11:28   ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-20 11:31   ` sashiko-bot [this message]
2026-11-20 11:14 ` [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-20 11:26   ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-20 11:37   ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-20 11:39   ` sashiko-bot

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=sashiko-outbox-225046@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=hare@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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