[PATCH v7 13/14] nvme-auth: link pre-provisioned keys into .nvme keyring

sashiko-bot at kernel.org sashiko-bot at kernel.org
Thu Nov 26 06:30:48 PST 2026


Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] nvme-auth: nvme_keyring_unlink() removes shared or .nvme
  pre-provisioned keys
- [Medium] nvmet-auth: nvmet_auth_update_key() mutates host state before
  linking key
- [Medium] nvmet-auth: nvmet_auth_update_key() sets wrong generated flag
  for ctrl key
- [Low] nvmet-auth: nvmet_auth_update_key() logs new_key serial on
  unlink failure
--

commit 03391c2ce04bdc0cd535d7de4d681251fe291e19
Author: Hannes Reinecke <hare at kernel.org>

nvme-auth: link pre-provisioned keys into .nvme keyring

This commit links pre-provisioned DH-HMAC-CHAP keys into the .nvme keyring
when parsed on the host or set on the target, and unlinks them when the
options or host keys are cleared or replaced.

> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
> index 12a30ed16f9ef..e7f51474ab5e1 100644
> --- a/drivers/nvme/host/fabrics.c
> +++ b/drivers/nvme/host/fabrics.c

[ ... ]

> @@ -1346,23 +1366,27 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
>  	struct key *key;
>  
>  	nvmf_host_put(opts->host);
> -	if (opts->dhchap_key &&
> -	    opts->dhchap_key_generated) {
> +	if (opts->dhchap_key) {
>  		key = key_lookup(opts->dhchap_key);
>  		if (!IS_ERR(key)) {
> -			pr_debug("revoke dhchap host key %08x\n",
> -				 opts->dhchap_key);
> -			key_revoke(key);
> +			if (opts->dhchap_key_generated) {
> +				pr_debug("revoke dhchap host key %08x\n",
> +					 opts->dhchap_key);
> +				key_revoke(key);
> +			} else
> +				nvme_keyring_unlink(key);
>  			key_put(key);
>  		}
>  	}

[Severity: High]
Does nvme_keyring_unlink() remove a pre-provisioned key that originally
lived in the .nvme keyring or is shared across multiple controllers?

Keyring links are indexed by key type and description rather than
reference-counted, so key_link() replaces an existing link in .nvme in
place and key_unlink() deletes that sole link.  A pre-provisioned key
placed directly in .nvme (as required when looking up keys by description
via nvme_dhchap_psk_lookup()) is removed from .nvme as soon as the first
controller disconnects or fails to connect, and a key from another keyring
that is shared across controllers or target hosts is unlinked from .nvme
while other users still depend on it.

[ ... ]

> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index 54e79c8a7cae6..04a9177ffa1f4 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c
> @@ -19,19 +19,20 @@
>  
>  #include "nvmet.h"
>  
> -static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
> +static int nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
>  				  struct key *new_key, bool new_generated)
>  {
>  	key_serial_t old_key_id = 0, new_key_id = key_serial(new_key);
>  	struct key *key = NULL;
>  	bool generated = false;
> +	int ret;
>  
>  	if (set_ctrl) {
>  		if (host->dhchap_ctrl_key) {
>  			if (host->dhchap_ctrl_key == new_key_id) {
>  				if (new_key && new_generated)
>  					key_revoke(new_key);
> -				return;
> +				return 0;
>  			}
>  			old_key_id = host->dhchap_ctrl_key;
>  			generated = host->dhchap_ctrl_key_generated;
> @@ -44,7 +45,7 @@ static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,

[Severity: Medium]
In the set_ctrl branch of nvmet_auth_update_key(), does
"host->dhchap_key_generated = new_generated" right after
"host->dhchap_ctrl_key = new_key_id" overwrite the host key's flag instead
of host->dhchap_ctrl_key_generated?

That leaves host->dhchap_ctrl_key_generated false so generated controller
keys are unlinked instead of revoked, while clobbering
host->dhchap_key_generated so a pre-provisioned host key can be revoked by
key_revoke() when later replaced or cleared.

>  			if (host->dhchap_key == new_key_id) {
>  				if (new_key && new_generated)
>  					key_revoke(new_key);
> -				return;
> +				return 0;
>  			}
>  			old_key_id = host->dhchap_key;
>  			generated = host->dhchap_key_generated;
> @@ -53,18 +54,37 @@ static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
>  		host->dhchap_key = new_key_id;
>  		host->dhchap_key_generated = new_generated;
>  	}
> +	if (new_key && !new_generated) {
> +		ret = nvme_keyring_link(new_key);
> +		if (ret < 0) {
> +			pr_warn("%s: failed to link %s key %08x\n",
> +				__func__, set_ctrl ? "ctrl" : "host",
> +				key_serial(new_key));
> +			return ret;
> +		}
> +	}

[Severity: Medium]
If nvme_keyring_link(new_key) fails here, does this early return leave
host->dhchap_key or host->dhchap_ctrl_key updated to new_key_id while
leaking old_key_id?

Because the host fields are overwritten before linking new_key, an error
from nvme_keyring_link() skips revoking or unlinking old_key_id and leaves
the unlinked new_key_id installed on host.

> +
>  	if (!old_key_id)
> -		return;
> +		return 0;
>  
>  	key = key_lookup(old_key_id);
>  	if (IS_ERR(key))
> -		return;
> +		return PTR_ERR(key);
>  	if (generated) {
>  		pr_debug("%s: revoke %s key %08x\n", __func__,
>  			 set_ctrl ? "ctrl" : "host", key_serial(key));
>  		key_revoke(key);
> +		ret = 0;
> +	} else {
> +		ret = nvme_keyring_unlink(key);
> +		if (ret < 0) {
> +			pr_warn("%s: failed to unlink %s key %08x\n",
> +				__func__, set_ctrl ? "ctrl" : "host",
> +				key_serial(new_key));
> +		}
>  	}

[Severity: Low]
Should this pr_warn() log key_serial(key) instead of key_serial(new_key)
when unlinking the old key fails?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-dhchap-keyring-v4-v7-0-ee438ffea527@kernel.org?part=13



More information about the Linux-nvme mailing list