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 9/9] nvmet: add configfs attribute 'dhchap_keyring'
Date: Mon, 23 Nov 2026 12:35:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-229228@kernel.org> (raw)
In-Reply-To: <20261123-dhchap-keyring-v4-v5-9-81eefb4c0724@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] nvmet: NULL pointer dereference in nvmet_auth_set_key() when
  clearing key
- [Medium] nvmet: cannot write or reset .nvme in
  nvmet_host_dhchap_keyring_store()
- [Medium] nvmet: 1-byte OOB write in nvmet_host_dhchap_keyring_show()
  via sprintf()
--

commit 1e868858e36190db74c6e7c75fe1ba260a535d71
Author: Hannes Reinecke <hare@kernel.org>

nvmet: add configfs attribute 'dhchap_keyring'

This commit adds a dhchap_keyring configfs attribute to the nvmet host
group so userspace can select which keyring is used to look up DH-HMAC-CHAP
keys.

> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index 7204ace149565..84091803af300 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c
> @@ -68,7 +68,9 @@ 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, &generated);
> +	down_read(&nvmet_config_sem);
> +	key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
> +	up_read(&nvmet_config_sem);
>  	if (IS_ERR(key)) {
>  		pr_debug("%s: invalid key specification\n", __func__);
>  		return PTR_ERR(key);

[Severity: High]
Does nvmet_auth_set_key() dereference a NULL secret when clearing a key via
configfs?

Earlier in this series, commit "nvmet-auth: parse dhchap key from configfs
attribute" updated nvmet_host_dhchap_key_store() and
nvmet_host_dhchap_ctrl_key_store() to leave keydata as NULL when an empty
line is written:

drivers/nvme/target/configfs.c:nvmet_host_dhchap_key_store() {
	...
	len = strcspn(page, "\n");
	if (len) {
		keydata = kstrndup(page, len, GFP_KERNEL);
		if (!keydata)
			return -ENOMEM;
	}

	ret = nvmet_auth_set_key(host, keydata, false);
	...
}

nvmet_auth_set_key() then checks if (!strlen(secret)) without testing
!secret first, triggering a NULL pointer dereference whenever an empty
string or newline is written to dhchap_key or dhchap_ctrl_key to revoke a
configured key.

> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index bc46d760aaa4a..eb20599cf372d 100644
> --- a/drivers/nvme/target/configfs.c
> +++ b/drivers/nvme/target/configfs.c
> @@ -2348,6 +2348,62 @@ static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
>  
>  CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
>  
> +static ssize_t nvmet_host_dhchap_keyring_show(struct config_item *item,
> +		char *page)
> +{
> +	struct nvmet_host *host = to_host(item);
> +	struct key *keyring;
> +	ssize_t ret;
> +
> +	down_read(&nvmet_config_sem);
> +	keyring = key_get(host->dhchap_keyring);
> +	if (!keyring) {
> +		page[0] = '\0';
> +		ret = 0;
> +	} else {
> +		down_read(&keyring->sem);
> +		if (key_validate(keyring))
> +			ret = sprintf(page, "<invalidated>\n");
> +		else
> +			ret = sprintf(page, "%s\n", keyring->description);
> +		up_read(&keyring->sem);
> +		key_put(keyring);
> +	}
> +	up_read(&nvmet_config_sem);
> +	return ret;
> +}

[Severity: Medium]
Can sprintf(page, "%s\n", keyring->description) write one byte past the end
of the PAGE_SIZE configfs read buffer?

add_key() allows keyring descriptions of up to KEY_MAX_DESC_SIZE - 1 (4095)
bytes, and writing a 4095-byte description without a trailing newline to
dhchap_keyring succeeds in nvmet_host_dhchap_keyring_store().  Reading
dhchap_keyring afterwards writes 4095 description bytes, a newline, and a
NUL terminator at page[4096], overflowing the 4096-byte buffer by one byte.
Using snprintf(page, PAGE_SIZE, ...) avoids the out-of-bounds write.

> +
> +static ssize_t nvmet_host_dhchap_keyring_store(struct config_item *item,
> +		const char *page, size_t count)
> +{
> +	struct nvmet_host *host = to_host(item);
> +	struct key *keyring;
> +	char *desc;
> +	size_t len;
> +	int ret = 0;
> +
> +	len = strcspn(page, "\n");
> +	if (!len)
> +		return -EINVAL;
> +	desc = kstrndup(page, len, GFP_KERNEL);
> +	if (!desc)
> +		return -ENOMEM;
> +	keyring = request_key(&key_type_keyring, desc, NULL);
> +	if (IS_ERR(keyring)) {
> +		ret = PTR_ERR(keyring);
> +	} else {
> +		down_write(&nvmet_config_sem);
> +		key_put(host->dhchap_keyring);
> +		host->dhchap_keyring = keyring;
> +		up_write(&nvmet_config_sem);
> +	}
> +	kfree(desc);
> +
> +	return ret < 0 ? ret : count;
> +}

[ ... ]

> @@ -2446,6 +2504,11 @@ static struct config_group *nvmet_hosts_make_group(struct config_group *group,
>  #ifdef CONFIG_NVME_TARGET_AUTH
>  	/* Default to SHA256 */
>  	host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
> +	host->dhchap_keyring = key_lookup(nvme_keyring_id());
> +	if (IS_ERR(host->dhchap_keyring)) {
> +		kfree(host);
> +		return ERR_PTR(-ENOKEY);
> +	}
>  #endif

[Severity: Medium]
Can userspace restore or reset dhchap_keyring back to the default .nvme
keyring through nvmet_host_dhchap_keyring_store()?

nvmet_hosts_make_group() initializes host->dhchap_keyring to the .nvme
keyring via key_lookup(nvme_keyring_id()), so reading dhchap_keyring shows
".nvme" (and the !keyring branch in nvmet_host_dhchap_keyring_show() is
unreachable).  However, nvme_keyring_init() allocates .nvme without linking
it into any process keyring, while nvmet_host_dhchap_keyring_store() looks
up desc via request_key(&key_type_keyring, desc, NULL), which only searches
the calling process's keyrings and rejects empty writes with -EINVAL.

As a result, tools such as nvmetcli that save and restore configfs
attributes fail with -ENOKEY when writing ".nvme" back unless .nvme was
manually linked into the caller's session keyring, and writing an empty
value to reset dhchap_keyring to the default fails with -EINVAL.

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


  reply	other threads:[~2026-11-23 12:35 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
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 [this message]
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-229228@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.