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 v4 5/9] nvmet-auth: parse dhchap key from configfs attribute
Date: Fri, 20 Nov 2026 11:28:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-225043@kernel.org> (raw)
In-Reply-To: <20261120-dhchap-keyring-v4-v4-5-5f8c7d9926ee@kernel.org>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] nvmet-auth: missing error abort and invalid status in
  nvmet_setup_auth()
- [High] nvmet-auth: missing initialization of ctrl->shash_id in
  nvmet_setup_auth()
- [High] nvmet-auth: missing length checks in nvme_dhchap_psk_preparse()
- [Medium] nvmet-auth: memory leak of keydata in
  nvmet_host_dhchap_ctrl_key_store()
- [Medium] nvmet-auth: empty writes rejected in configfs key store
  callbacks
--

commit 4bc851b3a915dfe8fe3d7c3db8fe8c3fa58c01ca
Author: Hannes Reinecke <hare@kernel.org>

nvmet-auth: parse dhchap key from configfs attribute

This commit converts the nvmet configfs DH-HMAC-CHAP key handling to store
host and controller keys as struct key pointers rather than raw secret
strings.

> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index 26457ce4acc2a..33b45b4b202d4 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c
> @@ -19,52 +19,63 @@

[ ... ]

>  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;
> -	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: High]
Can passing unvalidated configfs strings into nvme_auth_extract_key()
underflow datalen and ret in nvme_dhchap_psk_preparse()?

nvme_auth_extract_key() calls nvme_dhchap_psk_create() -> key_create() ->
nvme_dhchap_psk_preparse():

drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
	...
	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
		   &version, &hmac) != 2) {
		...
		return -EINVAL;
	}

	/* skip header and final ':' character */
	datalen -= 11;
	...
	ret = base64_decode(prep->data + 10, datalen, upayload->data,
			    true, BASE64_STD);
	if (ret < 0) {
		...
		return ret;
	}
	ret -= 4;
	crc = ~crc32(~0, upayload->data, ret);
	...
}

Because %02hhu matches 1 or 2 digits and %*s is not counted in sscanf()'s
return value, a short string such as "DHHC-1:1:" (9 bytes) returns 2 with
datalen < 11, so datalen -= 11 underflows size_t datalen and passes a huge
length to base64_decode().  Similarly, a short payload that decodes to 0..3
bytes (such as "DHHC-1:01:AA==:") makes ret -= 4 negative, which is then
converted to size_t in crc32() and reads past upayload->data.

[ ... ]

> @@ -186,44 +195,74 @@ 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_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);
>  	}

[Severity: High]
When key_validate(key) fails on host->dhchap_key, doesn't execution fall
through instead of setting ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE and
jumping to out_unlock?

If host->dhchap_ctrl_key is present and valid, the second key_validate()
overwrites ret with 0, so nvmet_setup_auth() returns 0 while leaving
ctrl->host_key NULL on initial connect, bypassing authentication because
nvmet_has_auth() and nvmet_check_auth_status() treat a NULL ctrl->host_key
as no authentication required.  If host->dhchap_ctrl_key is absent or also
invalid, nvmet_setup_auth() returns the negative errno from key_validate()
truncated to u8 instead of NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE, and if
key_get() returns NULL it jumps to out_unlock with ret == 0.

> -	key_put(ctrl->host_key);
> -	ctrl->host_key = host_key;
> -	key_put(ctrl->ctrl_key);
> -	ctrl->ctrl_key = ctrl_key;
> -	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

[Severity: High]
Does dropping ctrl->shash_id = host->dhchap_hash_id leave ctrl->shash_id at
0?

nvmet_auth_negotiate() matches the host's proposed HMAC IDs against
ctrl->shash_id, so leaving it at 0 ignores the host's configured dhchap_hash
attribute and always falls back to fallback_hash_id (and passes 0 to
nvme_auth_hmac_name() in the debug prints here).

> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index 27c50fa533e70..880cf5151588a 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;
> +	size_t len;
>  	int ret;
>  
> -	ret = nvmet_auth_set_key(host, page, false);
> +	len = strcspn(page, "\n");
> +	if (!len)
> +		return -EINVAL;
> +	keydata = kstrndup(page, len, GFP_KERNEL);
> +	if (!keydata)
> +		return -ENOMEM;
> +
> +	ret = nvmet_auth_set_key(host, keydata, false);
> +	kfree_sensitive(keydata);

[Severity: Medium]
Does returning -EINVAL when !len in nvmet_host_dhchap_key_store() and
nvmet_host_dhchap_ctrl_key_store() prevent clearing a configured key via
configfs?  An empty or newline-only write is rejected before reaching the
!strlen(secret) revocation branch in nvmet_auth_set_key().

[ ... ]

> @@ -2284,9 +2323,18 @@ static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
>  		const char *page, size_t count)
>  {
>  	struct nvmet_host *host = to_host(item);
> +	u8 *keydata;
> +	size_t len;
>  	int ret;
>  
> -	ret = nvmet_auth_set_key(host, page, true);
> +	len = strcspn(page, "\n");
> +	if (!len)
> +		return -EINVAL;
> +	keydata = kstrndup(page, len, GFP_KERNEL);
> +	if (!keydata)
> +		return -ENOMEM;
> +
> +	ret = nvmet_auth_set_key(host, keydata, true);
>  	/*
>  	 * Re-authentication is a soft state, so keep the
>  	 * current authentication valid until the host

[Severity: Medium]
Does nvmet_host_dhchap_ctrl_key_store() leak keydata?  Unlike
nvmet_host_dhchap_key_store(), it does not call kfree_sensitive(keydata)
after nvmet_auth_set_key().

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


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

Thread overview: 30+ 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 12:28     ` Hannes Reinecke
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 13:08     ` Hannes Reinecke
2026-11-20 19:24   ` kernel test robot
2026-11-20 19:37   ` kernel test robot
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 13:55     ` Hannes Reinecke
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 [this message]
2026-11-20 14:15     ` Hannes Reinecke
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
2026-11-20 14:48     ` Hannes Reinecke
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 14:52     ` Hannes Reinecke
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 15:02     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-20 11:39   ` sashiko-bot
2026-11-20 15:27     ` 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-225043@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.