Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hannes Reinecke" <hare@kernel.org>,
	"Keith Busch" <kbusch@kernel.org>, "Jens Axboe" <axboe@kernel.dk>,
	"Christoph Hellwig" <hch@lst.de>,
	"Sagi Grimberg" <sagi@grimberg.me>,
	"Hannes Reinecke" <hare@suse.de>,
	"Chaitanya Kulkarni" <kch@nvidia.com>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"David Howells" <dhowells@redhat.com>,
	"Jarkko Sakkinen" <jarkko@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Kees Cook" <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-nvme@lists.infradead.org, keyrings@vger.kernel.org
Subject: Re: [PATCH v7 02/14] nvme-keyring: add 'dhchap' key type
Date: Thu, 26 Nov 2026 14:32:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-235714@kernel.org> (raw)
In-Reply-To: <20261126-dhchap-keyring-v4-v7-2-ee438ffea527@kernel.org>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Critical] nvme-keyring: out-of-bounds read in
  nvme_dhchap_psk_preparse() via sscanf()
- [Medium] nvme-keyring: nvme_dhchap_psk_read() returns -EINVAL when
  buflen < keylen
- [Medium] nvme-keyring: missing .update callback in
  nvme_dhchap_psk_key_type
- [Low] nvme-keyring: inverted kernel-doc for
  nvme_dhchap_encoded_key_size()
- [Low] nvme-keyring: unbounded kzalloc() in nvme_dhchap_psk_preparse()
--

commit 96ec6bac915bae99bd1eb311a1d2b97cd0c724f7
Author: Hannes Reinecke <hare@kernel.org>

nvme-keyring: add 'dhchap' key type

This commit registers a new dhchap key type to store DH-HMAC-CHAP secret
keys with a user-type compatible payload and base64 read formatting.

> diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
> index 87cb51e1f5a7f..de8dab10fa105 100644
> --- a/drivers/nvme/common/keyring.c
> +++ b/drivers/nvme/common/keyring.c
[ ... ]
> @@ -253,6 +256,205 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
>  }
>  EXPORT_SYMBOL_GPL(nvme_tls_psk_default);
>  
> +static void nvme_dhchap_psk_describe(const struct key *key, struct seq_file *m)
> +{
> +	seq_puts(m, key->description);
> +	seq_printf(m, ": %u", key->datalen);
> +}
> +
> +/**
> + * nvme_dhchap_psk_preparse - prepare DH-HMAC-CHAP key data
> + * @prep: preparsed payload of the key data
> + *
> + * Decode the DH-HMAC-CHAP key data passed in @prep and
> + * store the resulting binary data. The binary data includes
> + * space for the CRC, the version, and the hmac identifier,
> + * but the data length is just the key data without the CRC.
> + * This allows the user to read the key data via the
> + * 'user_read()' function. The additional 'version' ahd 'hmac'
> + * data is used in the ->read() callback to generate the
> + * base64 encoded key.
> + */
> +static int nvme_dhchap_psk_preparse(struct key_preparsed_payload *prep)
> +{
> +	struct user_key_payload *upayload;
> +	size_t datalen = prep->datalen;
> +	int ret = -EINVAL, offset = 0;
> +	char *p;
> +	u32 crc;
> +	u8 version, hmac;
> +
> +	if (!prep->data || prep->datalen < 10) {
> +		pr_debug("%s: Invalid key data", __func__);
> +		goto out_err;
> +	}
> +	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
> +		   &version, &hmac, &offset) != 2 || offset != 10) {
> +		pr_debug("%s: invalid key header\n", __func__);
> +		goto out_err;
> +	}

[Severity: Critical]
As reported by Martin Belanger on v5 [1], can sscanf() read past the end
of prep->data?

add_key() copies plen bytes from userspace into prep->data without
appending a NUL terminator:

security/keys/keyctl.c:SYSCALL_DEFINE5(add_key) {
    ...
		payload = kvmalloc(plen, GFP_KERNEL);
		if (!payload)
			goto error2;

		ret = -EFAULT;
		if (copy_from_user(payload, _payload, plen) != 0)
			goto error3;
    ...
}

Inside vsscanf(), each integer conversion calls skip_spaces() before
parsing digits, which scans until a NUL byte and ignores prep->datalen.
A non-NUL-terminated payload of 10 or more bytes starting with "DHHC-"
or "DHHC-1:" followed by whitespace causes an out-of-bounds kernel memory
read before the offset check runs.

> +
> +	if (version != 1 || hmac > 3) {
> +		pr_debug("%s: invalid key version\n", __func__);
> +		goto out_err;
> +	}
> +
> +	/* skip header and final ':' character */
> +	if (offset > prep->datalen) {
> +		pr_debug("%s: invalid key data\n", __func__);
> +		goto out_err;
> +	}
> +	datalen = prep->datalen - offset;
> +	p = memchr(prep->data + offset, ':', datalen);
> +	if (p) {
> +		datalen = (p - (char *)prep->data);
> +		datalen -= offset;
> +	}
> +
> +	/*
> +	 * payload is < key | version | hmac >
> +	 * base64 decode will always return less data
> +	 * than the encoded data, so allocating the size
> +	 * of the encoded data will be large enough.
> +	 */
> +	upayload = kzalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
> +	if (!upayload) {
> +		ret = -ENOMEM;
> +		goto out_err;
> +	}
> +
> +	/* decode the data */
> +	prep->payload.data[0] = upayload;
> +	ret = base64_decode(prep->data + offset, datalen, upayload->data,
> +			    true, BASE64_STD);
> +	if (ret < 0) {
> +		pr_debug("%s: Failed to decode key\n", __func__);
> +		ret = -EINVAL;
> +		goto out_free;
> +	}
> +	if (ret != 36 && ret != 52 && ret != 68) {

[Severity: Low]
Should datalen be checked against the valid base64-encoded key lengths
(48, 72, or 92 bytes) before calling kzalloc() and base64_decode()?

Because add_key() allows payloads up to almost 1 MiB, an oversized
payload without an early trailing colon triggers a large physically
contiguous GFP_KERNEL allocation and decodes the entire buffer before
being rejected by the decoded length check.

[ ... ]

> +/**
> + * nvme_dhchap_encoded_key_size - Size of the base64-encoded key
> + * @size: size of the encoded key
> + *
> + * Returns the expected size of the key after base64 decoding
> + * include 4 bytes of CRC.
> + */
> +static inline int nvme_dhchap_encoded_key_size(int size)

[Severity: Low]
This isn't a bug, but the kernel-doc for nvme_dhchap_encoded_key_size()
has the input and return value reversed: @size is the raw decoded key
length (32, 48, or 64 bytes, excluding the 4-byte CRC), and the return
value is the base64-encoded length including the 4-byte CRC.  Also,
nvme_dhchap_psk_describe() above is identical to nvme_tls_psk_describe()
in the same file (and to user_describe()).

[ ... ]

> +static long nvme_dhchap_psk_read(const struct key *key,
> +				 char *buffer, size_t buflen)
> +{
> +	const struct user_key_payload *upayload;
> +	size_t datalen, keylen;
> +	u8 version, hmac;
> +	long ret;
> +
> +	upayload = user_key_payload_locked(key);
> +	/*
> +	 * Expected output string length:
> +	 * strlen("DHHC-") +
> +	 * version (= 1) +
> +	 * ':' (= 1) +
> +	 * hmac (= 2) +
> +	 * ':' (= 1) +
> +	 * encoded base64 key length +
> +	 * ':' (= 1)
> +	 */
> +	ret = nvme_dhchap_encoded_key_size(upayload->datalen);
> +	if (ret < 0)
> +		return ret;
> +	keylen = ret + 11;
> +	if (!buffer || !buflen)
> +		return keylen;
> +	else if (buflen < keylen)
> +		return -EINVAL;

[Severity: Medium]
Should nvme_dhchap_psk_read() return keylen instead of -EINVAL when
buflen < keylen?

The key_type->read() contract and keyctl_read_key() expect ->read() to
return the required size without copying whenever the caller's buffer is
too small, so returning -EINVAL breaks keyctl(KEYCTL_READ) callers that
probe the key size with a small non-NULL buffer.

> +
> +	memset(buffer, 0, buflen);
> +	version = upayload->data[upayload->datalen + 4];
> +	hmac = upayload->data[upayload->datalen + 5];
> +	ret = sprintf(buffer, "DHHC-%01hhu:%02hhu:", version, hmac);
> +	if (ret < 0)
> +		return -ENOKEY;
> +	/* Include the trailing CRC */
> +	datalen = upayload->datalen + 4;
> +	ret += base64_encode(upayload->data, datalen, buffer + ret,
> +			     true, BASE64_STD);
> +	buffer[ret] = ':';
> +	ret++;
> +	return ret;
> +}
> +
> +static struct key_type nvme_dhchap_psk_key_type = {
> +	.name           = "dhchap",
> +	.flags          = KEY_TYPE_NET_DOMAIN,
> +	.preparse       = nvme_dhchap_psk_preparse,
> +	.free_preparse  = user_free_preparse,
> +	.instantiate    = generic_key_instantiate,
> +	.revoke         = user_revoke,
> +	.destroy        = user_destroy,
> +	.describe       = nvme_dhchap_psk_describe,
> +	.read           = nvme_dhchap_psk_read,
> +};

[Severity: Medium]
As reported by Martin Belanger on v6 [2], should nvme_dhchap_psk_key_type
define .update = user_update?

Without .update, keyctl(KEYCTL_UPDATE) fails with -EOPNOTSUPP and
add_key() allocates a new key with a different serial number instead of
updating an existing dhchap key in place, breaking key rotation for
controllers referencing a pre-provisioned key by serial number.  The
later commit "nvme-auth: allow to update pre-provisioned keys" in this
series mentions adding .update to the dhchap key type, but adds
.update = user_update to nvme_tls_psk_key_type instead.

[1] https://lore.kernel.org/r/463a55e8-c2ef-4d83-94ff-7599c7d931e6@gmail.com
[2] https://lore.kernel.org/r/a71a3d9f-0f19-45f4-bc98-a5663ad537b2@gmail.com

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


  reply	other threads:[~2026-11-26 14:32 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 01/14] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 02/14] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-26 14:32   ` sashiko-bot [this message]
2026-11-26 14:12 ` [PATCH v7 03/14] nvme-keyring: drop nvme_tls_psk_revoked() Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 04/14] Documentation/security/keys: fixup function name Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring Hannes Reinecke
2026-11-26 14:24   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 06/14] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-26 14:33   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 07/14] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-26 14:29   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-26 14:30   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 09/14] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-26 14:27   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 10/14] nvme-auth: allow to specify the key by key serial Hannes Reinecke
2026-11-26 14:26   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 11/14] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 12/14] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-26 14:31   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 13/14] nvme-auth: link pre-provisioned keys into .nvme keyring Hannes Reinecke
2026-11-26 14:30   ` sashiko-bot
2026-11-26 14:12 ` [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys Hannes Reinecke
2026-11-26 14:31   ` sashiko-bot

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-235714@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=corbet@lwn.net \
    --cc=dhowells@redhat.com \
    --cc=ebiggers@kernel.org \
    --cc=gustavoars@kernel.org \
    --cc=hare@kernel.org \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=jarkko@kernel.org \
    --cc=kbusch@kernel.org \
    --cc=kch@nvidia.com \
    --cc=kees@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=rdunlap@infradead.org \
    --cc=sagi@grimberg.me \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=skhan@linuxfoundation.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox