[PATCH v6 6/9] nvme: allow to pass in key description as dhchap secret
Hannes Reinecke
hare at suse.de
Tue Nov 24 00:31:12 PST 2026
On 11/23/26 6:32 PM, sashiko-bot at kernel.org wrote:
> 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 at 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).
>
Fixed with the next version.
> [ ... ]
>
>> 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:
>
Already fixed with the next version of the previous patch.
> 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.
>
Already fixed with the next version of the previous patch.
> [ ... ]
>
>> 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.
>
Fixed with the next version.
>> 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.
>
Fixed with the next version.
> [ ... ]
>
>> 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.
>
Fixed with the next version.
> [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.
>
Fixed with the next version.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare at suse.de +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
More information about the Linux-nvme
mailing list