All of 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 v5 5/9] nvmet-auth: parse dhchap key from configfs attribute
Date: Mon, 23 Nov 2026 12:29:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-229218@kernel.org> (raw)
In-Reply-To: <20261123-dhchap-keyring-v4-v5-5-81eefb4c0724@kernel.org>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] nvmet-auth: NULL pointer dereference in nvmet_auth_set_key()
  when clearing key
- [High] nvmet-auth: invalid DH-CHAP status and partial key update in
  nvmet_setup_auth()
- [High] nvmet-auth: shared key revocation in nvmet_auth_revoke_key()
  breaks active ctrls
- [High] nvmet-auth: uninitialized offset and OOB read in
  nvme_dhchap_psk_preparse()
- [Medium] nvmet-auth: missing DH-CHAP version and hash ID check in
  nvmet_auth_set_key()
--

commit 46da02dd0755f9ec506cfdb7b2885930c4ee4ffd
Author: Hannes Reinecke <hare@kernel.org>

nvmet-auth: parse dhchap key from configfs attribute

This commit converts the nvmet configfs DH-HMAC-CHAP key interface to
store keys as struct key pointers on struct nvmet_host rather than raw
secret strings.

> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index 26457ce4acc2a..8b77164512c9c 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c
> @@ -19,52 +19,63 @@
>  
>  #include "nvmet.h"
>  
> +void nvmet_auth_revoke_key(struct nvmet_host *host, bool set_ctrl)
> +{
> +	struct key *key = NULL;
> +
> +	if (set_ctrl) {
> +		if (host->dhchap_ctrl_key) {
> +			key = host->dhchap_ctrl_key;
> +			host->dhchap_ctrl_key = NULL;
> +		}
> +	} else {
> +		if (host->dhchap_key) {
> +			key = host->dhchap_key;
> +			host->dhchap_key = NULL;
> +		}
> +	}
> +	if (key) {
> +		pr_debug("%s: revoke %s key %08x\n",
> +			 __func__, set_ctrl ? "ctrl" : "host",
> +			 key_serial(key));
> +		key_revoke(key);
> +		key_put(key);
> +	}
> +}

[Severity: High]
Does revoking the key here break active controllers that still hold a
reference to it?

nvmet_setup_auth() shares host->dhchap_key and host->dhchap_ctrl_key with
connected controllers via key_get().  When a key is updated or cleared in
configfs, calling key_revoke() here marks that shared key revoked and
clears its payload, even though nvmet_host_dhchap_key_store() notes that
existing authentication must stay valid until the host requests
re-authentication.  Because nvmet_execute_auth_send() only re-runs
nvmet_setup_auth() on the admin queue, in-flight authentication or later
I/O queue authentication on an existing controller continues using
ctrl->host_key and fails in key_validate() with -EKEYREVOKED.

In addition, when host->dhchap_key is cleared to NULL, nvmet_setup_auth()
returns early without dropping ctrl->host_key or ctrl->ctrl_key, leaving
the controller pointing to the revoked key on re-authentication.

> +
>  int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
>  		       bool set_ctrl)
>  {
> -	unsigned char key_hash;
> -	char *dhchap_secret;
> -	int offset = 0;
> +	struct key *key;
> +	size_t len;
>  
>  	if (!strlen(secret)) {
>  		down_write(&nvmet_config_sem);
> -		if (set_ctrl) {
> -			kfree(host->dhchap_ctrl_secret);
> -			host->dhchap_ctrl_secret = NULL;
> -			host->dhchap_ctrl_key_hash = 0;
> -			host->dhchap_ctrl_key_offset = 0;
> -		} else {
> -			kfree(host->dhchap_secret);
> -			host->dhchap_secret = NULL;
> -			host->dhchap_key_hash = 0;
> -			host->dhchap_key_offset = 0;
> -		}
> +		nvmet_auth_revoke_key(host, set_ctrl);
>  		up_write(&nvmet_config_sem);
>  		return 0;
>  	}
> -	if (sscanf(secret, "DHHC-1:%hhd:%n", &key_hash, &offset) != 1 ||
> -	    !offset)
> -		return -EINVAL;

[Severity: High]
Does removing the offset check here expose an uninitialized read and an
out-of-bounds read in nvme_dhchap_psk_preparse()?

Commit 62dae895f986 ("nvmet-auth: store the DH-CHAP key offset instead of
hardcoding it") initialized offset = 0 and used the parsed %n offset.
Delegating parsing to nvme_auth_extract_key() -> nvme_dhchap_psk_preparse()
loses both fixes:

drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
	int ret, offset;
...
	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
		   &version, &hmac, &offset) != 2 || !offset) {
...
	datalen = strlen(prep->data + offset);
...
	ret = base64_decode(prep->data + 10, datalen, upayload->data,
			    true, BASE64_STD);
}

Because %n is not counted in the sscanf() return value, an input missing
the trailing colon (such as "DHHC-1:1") returns 2 without writing offset,
so strlen(prep->data + offset) uses an uninitialized stack variable.  And
when a single-digit HMAC ID is used (such as "DHHC-1:1:...", where offset
is 9), base64_decode() hardcodes prep->data + 10 with datalen computed
from offset 9, reading one byte past the NUL terminator.

> -	if (key_hash > 3) {
> -		pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
> -			 key_hash);
> -		return -EINVAL;
> -	}
> -	dhchap_secret = kstrdup(secret, GFP_KERNEL);
> -	if (!dhchap_secret)
> -		return -ENOMEM;
> +	len = strcspn(secret, "\n");
> +	key = nvme_auth_extract_key(NULL, secret, len);
> +	if (IS_ERR(key)) {
> +		pr_debug("%s: invalid key specification\n", __func__);
> +		return PTR_ERR(key);
> +	}

[Severity: Medium]
Does switching to nvme_auth_extract_key() drop the DH-CHAP version and
hash ID validation?

Previously, nvmet_auth_set_key() required the "DHHC-1:" prefix and
rejected key_hash > 3 with -EINVAL.  nvme_dhchap_psk_preparse() parses
"DHHC-%01hhu:%02hhu:%n" into version and hmac without checking that
version == 1 or hmac <= 3, allowing keys with unsupported versions or
hash IDs to be stored via configfs and deferring failure until
authentication time.

[ ... ]

> @@ -186,44 +195,76 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
>  		goto out_unlock;
>  	}
>  
> -	if (!host->dhchap_secret) {
> +	if (!host->dhchap_key) {
>  		pr_debug("No authentication provided\n");
>  		goto out_unlock;
>  	}
>  
> -	host_key = nvme_auth_extract_key(NULL, host->dhchap_secret,
> -					 strlen(host->dhchap_secret));
> -	if (IS_ERR(host_key)) {
> -		ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
> +	key = key_get(host->dhchap_key);
> +	if (!key) {
> +		pr_warn("%s: host key %08x not found\n",
> +			__func__, key_serial(host->dhchap_key));
>  		goto out_unlock;
>  	}
> -	if (host->dhchap_ctrl_secret) {
> -		ctrl_key = nvme_auth_extract_key(NULL, host->dhchap_ctrl_secret,
> -						 strlen(host->dhchap_ctrl_secret));
> -		if (IS_ERR(ctrl_key)) {
> -			key_put(host_key);
> -			ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
> -			goto out_unlock;
> +	down_read(&key->sem);
> +	ret = key_validate(key);
> +	if (!ret) {
> +		if (ctrl->host_key) {
> +			pr_debug("%s: drop host key %08x\n",
> +				 __func__, key_serial(ctrl->host_key));
> +			key_put(ctrl->host_key);
>  		}
> +		ctrl->host_key = key;
>  	}
> -	key_put(ctrl->host_key);
> -	ctrl->host_key = host_key;
> -	key_put(ctrl->ctrl_key);
> -	ctrl->ctrl_key = ctrl_key;
> +	key_id = key_serial(key);
> +	up_read(&key->sem);
> +	if (ret) {
> +		pr_warn("host key %08x invalidated\n", key_id);
> +		key_put(key);
> +		key = ERR_PTR(-EKEYREVOKED);
> +		goto out_unlock;
> +	}
>  	ctrl->shash_id = host->dhchap_hash_id;
>  #ifdef CONFIG_NVME_TARGET_AUTH_DEBUG
> -	host_hash = nvme_dhchap_psk_hash(ctrl->host_key);
> -	pr_debug("%s: using hash %s key %u\n", __func__,
> -		 ctrl_hash > 0 ?
> -		 nvme_auth_hmac_name(ctrl_hash) : "none",
> -		 key_serial(ctrl->host_key));
> -	if (ctrl->ctrl_key) {
> -		ctrl_hash = nvme_dhchap_psk_hash(ctrl->ctrl_key);
> -		pr_debug("%s: using ctrl hash %s key %u\n", __func__,
> -			 ctrl_hash > 0 ?
> -			 nvme_auth_hmac_name(ctrl_hash) : "none",
> -			 key_serial(ctrl->ctrl_key));
> +	pr_debug("%s: using dhchap hash %s key %08x\n", __func__,
> +		 nvme_auth_hmac_name(ctrl->shash_id), key_id);
> +#endif
> +	if (!host->dhchap_ctrl_key) {
> +		if (ctrl->ctrl_key) {
> +			pr_debug("%s: drop ctrl key %08x\n",
> +				 __func__, key_serial(ctrl->ctrl_key));
> +			key_put(ctrl->ctrl_key);
> +			ctrl->ctrl_key = NULL;
> +		}
> +		goto out_unlock;
>  	}
> +
> +	key = key_get(host->dhchap_ctrl_key);
> +	if (!key) {
> +		pr_warn("%s: ctrl key %08x not found\n",
> +			__func__, key_serial(host->dhchap_ctrl_key));
> +		goto out_unlock;
> +	}
> +	down_read(&key->sem);
> +	ret = key_validate(key);
> +	if (!ret) {
> +		if (ctrl->ctrl_key) {
> +			pr_debug("%s: drop ctrl key %08x\n",
> +				 __func__, key_serial(ctrl->ctrl_key));
> +			key_put(ctrl->ctrl_key);
> +		}
> +		ctrl->ctrl_key = key;
> +	}
> +	key_id = key_serial(key);
> +	up_read(&key->sem);
> +	if (ret) {
> +		pr_warn("ctrl key %08x invalidated\n", key_id);
> +		key_put(key);
> +		goto out_unlock;
> +	}

[Severity: High]
Does nvmet_setup_auth() return a negative errno instead of a DH-HMAC-CHAP
status code when key_validate() fails, and overwrite ctrl->host_key before
host->dhchap_ctrl_key is validated?

nvmet_execute_auth_send() stores the u8 return value of nvmet_setup_auth()
directly in req->sq->dhchap_status to send in the DH-HMAC-CHAP_Failure1
message.  When key_validate() fails for either key, ret holds a negative
errno (such as -EKEYREVOKED) instead of NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE,
so (u8)ret is sent on the wire as the failure reason code.  Conversely, if
key_get() returns NULL, ret is still 0 and nvmet_setup_auth() reports
success without installing the key.

Also, ctrl->host_key and ctrl->shash_id are replaced before
host->dhchap_ctrl_key is validated, undoing commit 41ad4af45ac9
("nvmet-auth: keep the old keys if the new ones fail") and leaving the
controller with a mismatched host/controller key pair if controller key
validation fails during re-authentication.

[ ... ]

> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index ff3a8d74de599..12c093f4c8363 100644
> --- a/drivers/nvme/target/configfs.c
> +++ b/drivers/nvme/target/configfs.c

[ ... ]

> @@ -2251,9 +2266,19 @@ static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
>  		const char *page, size_t count)
>  {
>  	struct nvmet_host *host = to_host(item);
> +	u8 *keydata = NULL;
> +	size_t len;
>  	int ret;
>  
> -	ret = nvmet_auth_set_key(host, page, false);
> +	len = strcspn(page, "\n");
> +	if (len) {
> +		keydata = kstrndup(page, len, GFP_KERNEL);
> +		if (!keydata)
> +			return -ENOMEM;
> +	}
> +
> +	ret = nvmet_auth_set_key(host, keydata, false);
> +	kfree_sensitive(keydata);

[Severity: High]
Does clearing dhchap_key or dhchap_ctrl_key via configfs trigger a NULL
pointer dereference in nvmet_auth_set_key()?

When an empty string or a newline is written to nvmet_host_dhchap_key_store()
or nvmet_host_dhchap_ctrl_key_store() to clear a key, len is 0 and keydata
remains NULL.  nvmet_auth_set_key() then evaluates if (!strlen(secret))
without checking if secret is NULL first.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-dhchap-keyring-v4-v5-0-81eefb4c0724@kernel.org?part=5


  reply	other threads:[~2026-11-23 12:29 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 12:14 [PATCH v5 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-23 12:36   ` sashiko-bot
2026-11-23 13:18     ` Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-23 12:35   ` sashiko-bot
2026-11-23 13:31     ` Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-23 12:34   ` sashiko-bot
2026-11-23 14:10     ` Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-23 12:29   ` sashiko-bot [this message]
2026-11-23 15:03     ` Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-23 12:36   ` sashiko-bot
2026-11-23 15:25     ` Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-23 12:29   ` sashiko-bot
2026-11-23 15:32     ` Hannes Reinecke
2026-11-23 12:14 ` [PATCH v5 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-23 12:35   ` sashiko-bot
2026-11-23 15:38     ` Hannes Reinecke
2026-11-23 12:59 ` [PATCH v5 0/9] nvme-auth: switch to use the kernel keyring Martin Belanger
2026-11-23 15:32   ` Martin Belanger
2026-11-23 15:41     ` Hannes Reinecke
2026-11-23 15:39   ` Hannes Reinecke

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-229218@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.