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 v6 6/9] nvme: allow to pass in key description as dhchap secret
Date: Mon, 23 Nov 2026 17:32:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-229755@kernel.org> (raw)
In-Reply-To: <20261123-dhchap-keyring-v4-v6-6-5082a49715ab@kernel.org>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] nvmet-auth: self-revocation of active generated key in
  nvmet_auth_set_key()
- [Medium] nvmet-auth: host_key reference leak on ctrl_key error in
  nvmet_setup_auth()
- [Medium] nvme-auth: dhchap_secret_store() returns -EBUSY instead of
  re-authenticating
- [Low] nvme-auth: key_serial(ctrl->host_key) logged before assignment
  in nvme_auth_init_ctrl()
- [Low] nvme: wrong device pointer ctrl->dev used for logging in sysfs
  store helpers
- [Low] nvme-auth: mismatched key serial comments in
  nvme_auth_extract_key()
--

commit bea90f054bc2ef7a55e7fb9326ffa4aa13dd0817
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 a pre-populated key
in the keyring before falling back to generating a new key from raw secret
data, and tracks whether the key was auto-generated so only generated keys
are revoked on cleanup.

> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
> index 158c9cb886c15..8c84595018258 100644
> --- a/drivers/nvme/common/auth.c
> +++ b/drivers/nvme/common/auth.c
> @@ -144,20 +144,32 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
>   * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
>   *
>   * @keyring: keyring to be used for the resulting 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)
>  {
>  	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);

[Severity: Low]
This isn't a bug, but the comments here and the commit message body say
that @input can be a key serial number, whereas nvme_dhchap_psk_lookup()
searches the keyring by key description (matching the commit subject and
the sysfs/configfs show callbacks).

[ ... ]

> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
> index 26e8f321650e7..2c73da4cd3c61 100644
> --- a/drivers/nvme/host/auth.c
> +++ b/drivers/nvme/host/auth.c

[ ... ]

> @@ -1104,6 +1117,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>  	if (ctrl->host_key)
>  		nvme_auth_clear_host_key(ctrl);
>  	ctrl->host_key = host_key;
> +	ctrl->host_key_generated = host_key_generated;

[Severity: Low]
Just above this hunk, pr_debug() logs key_serial(ctrl->host_key) before
ctrl->host_key is assigned host_key:

drivers/nvme/host/auth.c:nvme_auth_init_ctrl() {
    ...
	pr_debug("%s: using dhchap host key %08x\n",
	       __func__, key_serial(ctrl->host_key));
	if (ctrl->host_key)
		nvme_auth_clear_host_key(ctrl);
	ctrl->host_key = host_key;
    ...
}

Should that log key_serial(host_key) instead, like the ctrl_key message a
few lines below?  Otherwise it logs 00000000 on initial controller setup
and the old key serial on re-initialization.

[ ... ]

> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
> index 6fab0d217e749..c94abb901a73c 100644
> --- a/drivers/nvme/host/sysfs.c
> +++ b/drivers/nvme/host/sysfs.c

[ ... ]

> @@ -874,22 +877,34 @@ static ssize_t dhchap_secret_store(struct device *dev,
>  	ret = key_validate(key);
>  	up_read(&key->sem);
>  	if (ret) {
> -		dev_warn(ctrl->device, "key %08x invalidated\n",
> +		dev_warn(ctrl->dev, "key %08x invalidated\n",
>  			 key_serial(key));
> -		dev_dbg(ctrl->device, "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);
> +		}
>  		key_put(key);
>  		kfree_sensitive(dhchap_secret);
>  		return ret;
>  	}
>  	mutex_lock(&ctrl->dhchap_auth_mutex);
>  	old_key = ctrl->host_key;
> -	dev_dbg(ctrl->device, "revoke host key %08x\n",
> -		key_serial(old_key));
> -	key_revoke(old_key);
> -
> +	if (old_key && key_serial(old_key) == key_serial(key)) {
> +		dev_warn(ctrl->device, "key %08x already present\n",
> +			 key_serial(key));
> +		mutex_unlock(&ctrl->dhchap_auth_mutex);
> +		key_put(key);
> +		kfree_sensitive(dhchap_secret);
> +		return -EBUSY;
> +	}
> +	if (ctrl->host_key_generated) {
> +		dev_dbg(ctrl->dev, "revoke host key %08x\n",
> +			key_serial(old_key));
> +		key_revoke(old_key);
> +	}

[Severity: Low]
Should these dev_warn() and dev_dbg() calls (and the one in
dhchap_ctrl_secret_store() below) keep using ctrl->device instead of
ctrl->dev?  ctrl->device is the nvmeX controller class device, whereas
ctrl->dev is the parent device (such as the shared /dev/nvme-fabrics misc
device), so switching to ctrl->dev loses the controller name in the logs.

>  	ctrl->host_key = key;
> +	ctrl->host_key_generated = generated;
>  	mutex_unlock(&ctrl->dhchap_auth_mutex);
>  	key_put(old_key);
>  	kfree_sensitive(dhchap_secret);

[ ... ]

> @@ -960,20 +979,32 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
>  	if (ret) {
>  		dev_warn(ctrl->device, "key %08x invalidated\n",
>  			 key_serial(key));
> -		dev_dbg(ctrl->device, "revoke ctrl key %08x\n",
> -			key_serial(key));
> -		key_revoke(key);
> +		if (generated) {
> +			dev_dbg(ctrl->device, "revoke ctrl key %08x\n",
> +				key_serial(key));
> +			key_revoke(key);
> +		}
>  		key_put(key);
>  		kfree_sensitive(dhchap_secret);
>  		return ret;
>  	}
>  	mutex_lock(&ctrl->dhchap_auth_mutex);
>  	old_key = ctrl->ctrl_key;
> -	dev_dbg(ctrl->device, "revoke ctrl key %08x\n",
> -		key_serial(old_key));
> -	key_revoke(old_key);
> -
> +	if (old_key && key_serial(old_key) == key_serial(key)) {
> +		dev_warn(ctrl->device, "ctrl key %08x already present\n",
> +			 key_serial(key));
> +		mutex_unlock(&ctrl->dhchap_auth_mutex);
> +		key_put(key);
> +		kfree_sensitive(dhchap_secret);
> +		return -EBUSY;
> +	}
> +	if (ctrl->ctrl_key_generated) {
> +		dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
> +			key_serial(old_key));
> +		key_revoke(old_key);
> +	}
>  	ctrl->ctrl_key = key;
> +	ctrl->ctrl_key_generated = generated;
>  	mutex_unlock(&ctrl->dhchap_auth_mutex);
>  	key_put(old_key);
>  	kfree_sensitive(dhchap_secret);

[Severity: Medium]
Does returning -EBUSY when key_serial(old_key) == key_serial(key) in
dhchap_secret_store() and dhchap_ctrl_secret_store() break triggering
re-authentication via sysfs when using pre-populated keys?

Writing the currently active secret to dhchap_secret or dhchap_ctrl_secret
is the documented interface for triggering controller re-authentication.
When a raw secret is written, nvme_dhchap_psk_create() allocates a new key
with a new serial number and queues ctrl->dhchap_auth_work, but when the
active pre-populated key description is written, nvme_dhchap_psk_lookup()
returns the existing key, so these functions stop any pending auth work
via nvme_auth_stop(ctrl) and fail with -EBUSY instead of queueing
ctrl->dhchap_auth_work.

[ ... ]

> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index 6348c810bd168..c3fa4ab78fb94 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c

[ ... ]

> @@ -56,7 +67,7 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
>  		return 0;
>  	}
>  	len = strcspn(secret, "\n");
> -	key = nvme_auth_extract_key(NULL, secret, len);
> +	key = nvme_auth_extract_key(NULL, secret, len, &generated);
>  	if (IS_ERR(key)) {
>  		pr_debug("%s: invalid key specification\n", __func__);
>  		return PTR_ERR(key);
> @@ -72,10 +83,13 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
>  	up_read(&key->sem);
>  	down_write(&nvmet_config_sem);
>  	nvmet_auth_revoke_key(host, set_ctrl);
> -	if (set_ctrl)
> +	if (set_ctrl) {
>  		host->dhchap_ctrl_key = key;
> -	else
> +		host->dhchap_ctrl_key_generated = generated;
> +	} else {
>  		host->dhchap_key = key;
> +		host->dhchap_key_generated = generated;
> +	}
>  	up_write(&nvmet_config_sem);
>  	return 0;
>  }

[Severity: Medium]
Can nvmet_auth_set_key() revoke the newly looked-up key in place if the
host currently has an auto-generated key and its description (UUID) is
written back to configfs?

nvme_auth_extract_key() finds the active key in the keyring via
nvme_dhchap_psk_lookup() and returns it with generated = false, and then
nvmet_auth_revoke_key() sees the old generated flag set and calls
key_revoke() on that same struct key before it is reinstalled on host,
leaving the host with a permanently revoked key that fails all subsequent
authentications.  Also, if key_validate(key) fails when generated is true,
nvmet_auth_set_key() calls key_put(key) without key_revoke(key), leaving
the generated key linked in the keyring.

[Severity: Medium]
Later in drivers/nvme/target/auth.c, does nvmet_setup_auth() leak the
host_key reference when acquiring or validating ctrl_key fails?

drivers/nvme/target/auth.c:nvmet_setup_auth() {
    ...
	host_key = key_get(host->dhchap_key);
    ...
	if (host->dhchap_ctrl_key) {
		ctrl_key = key_get(host->dhchap_ctrl_key);
		if (!ctrl_key) {
			pr_warn("%s: ctrl key %08x not found\n",
				__func__, key_serial(host->dhchap_ctrl_key));
			ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
			goto out_unlock;
		}
		down_read(&ctrl_key->sem);
		ret = key_validate(ctrl_key);
		key_id = key_serial(ctrl_key);
		up_read(&ctrl_key->sem);
		if (ret) {
			pr_warn("ctrl key %08x invalidated\n", key_id);
			key_put(ctrl_key);
			ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
			goto out_unlock;
		}
	}
    ...
out_unlock:
	up_read(&nvmet_config_sem);

	return ret;
}

When a pre-populated ctrl_key in the keyring is revoked or expired,
key_validate(ctrl_key) fails and jumps to out_unlock without calling
key_put(host_key), leaking a reference on host_key on every authentication
attempt.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-dhchap-keyring-v4-v6-0-5082a49715ab@kernel.org?part=6


  reply	other threads:[~2026-11-23 17:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 17:10 [PATCH v6 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-23 17:10 ` [PATCH v6 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-23 17:10 ` [PATCH v6 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-23 17:31   ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-23 17:32   ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-23 17:26   ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-23 17:30   ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-23 17:32   ` sashiko-bot [this message]
2026-11-23 17:10 ` [PATCH v6 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-23 17:10 ` [PATCH v6 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-23 17:30   ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-23 17:33   ` 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-229755@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