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 2/9] nvme-keyring: add 'dhchap' key type
Date: Fri, 20 Nov 2026 11:28:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-225040@kernel.org> (raw)
In-Reply-To: <20261120-dhchap-keyring-v4-v4-2-5f8c7d9926ee@kernel.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Critical] nvme-keyring: out-of-bounds reads and underflows in
  nvme_dhchap_psk_preparse()
- [High] nvme-keyring: uninitialized keylen used in
  nvme_dhchap_psk_preparse()
- [Medium] nvme-keyring: unsigned error check and size mismatch in
  nvme_dhchap_psk_read()
- [Medium] nvme-keyring: out-of-bounds memcmp() in
  nvme_dhchap_psk_match()
--

commit 329ebc74c917187a04d7c54885592dcb5f74d0dd
Author: Hannes Reinecke <hare@kernel.org>

nvme-keyring: add 'dhchap' key type

This commit adds a 'dhchap' key type to store DH-HMAC-CHAP secret keys with
a user-type compatible payload and a read callback that formats keys in their
base64-encoded representation.

> diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
> index 87cb51e1f5a7f..96561f2dd81b8 100644
> --- a/drivers/nvme/common/keyring.c
> +++ b/drivers/nvme/common/keyring.c
[ ... ]
> @@ -253,6 +256,212 @@ 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);
> +}
> +
> +static bool nvme_dhchap_psk_match(const struct key *key,
> +				  const struct key_match_data *match_data)
> +{
> +	const char *match_id;
> +	size_t match_len;
> +
> +	if (!key->description) {
> +		pr_debug("%s: no key description\n", __func__);
> +		return false;
> +	}
> +	if (!match_data->raw_data) {
> +		pr_debug("%s: no match data\n", __func__);
> +		return false;
> +	}
> +	match_id = match_data->raw_data;
> +	match_len = strlen(match_id);
> +	pr_debug("%s: match '%s' '%s' len %zd\n",
> +		 __func__, match_id, key->description, match_len);
> +
> +	return !memcmp(key->description, match_id, match_len);
> +}

[Severity: Medium]
Can memcmp() read past the end of key->description here?  key_alloc()
allocates strlen(key->description) + 1 bytes, so searching with a match_id
longer than key->description reads out of bounds, while a shorter match_id
matches any key whose description shares that prefix.

[ ... ]

> +static int nvme_dhchap_psk_preparse(struct key_preparsed_payload *prep)
> +{
> +	struct user_key_payload *upayload;
> +	size_t datalen = prep->datalen, keylen;
> +	int ret;
> +	u32 crc;
> +	u8 version, hmac;
> +
> +	if (!prep->data) {
> +		pr_debug("%s: Empty data", __func__);
> +		prep->payload.data[0] = NULL;
> +		prep->quotalen = 0;
> +		return -EINVAL;
> +	}
> +
> +	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> +		   &version, &hmac) != 2) {
> +		pr_debug("%s: invalid key data '%s'\n", __func__,
> +			 (char *)prep->data);
> +		prep->payload.data[0] = NULL;
> +		prep->quotalen = 0;
> +		return -EINVAL;
> +	}
> +
> +	/* skip header and final ':' character */
> +	datalen -= 11;
> +
> +	/*
> +	 * 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) {
> +		prep->payload.data[0] = NULL;
> +		prep->quotalen = 0;
> +		return -ENOMEM;
> +	}
> +
> +	/* decode the data */
> +	prep->quotalen = keylen;

[Severity: High]
Isn't keylen uninitialized when assigned to prep->quotalen?
generic_key_instantiate() passes prep->quotalen to key_payload_reserve(),
corrupting per-user key quota accounting and exposing the lower 16 bits of
the stack value via key->datalen in /proc/keys.

> +	prep->payload.data[0] = upayload;
> +	ret = base64_decode(prep->data + 10, datalen, upayload->data,
> +			    true, BASE64_STD);
> +	if (ret < 0) {
> +		pr_debug("%s: Failed to decode key %s\n",
> +			 __func__, (char *)prep->data + 10);
> +		return ret;
> +	}
> +	ret -= 4;
> +	crc = ~crc32(~0, upayload->data, ret);

[Severity: Critical]
Can malformed or short payloads from add_key() trigger out-of-bounds reads
and integer underflows in nvme_dhchap_psk_preparse()?

add_key() copies plen bytes from userspace into prep->data without appending
a NUL terminator, so sscanf() and the pr_debug() %s calls can read past
prep->data + prep->datalen.  Because %*s is assignment-suppressed and
%02hhu matches one or two digits, sscanf() can also return 2 when
prep->datalen < 11 (for example, "DHHC-1:01"), causing datalen -= 11 to
underflow and wrap sizeof(*upayload) + datalen in kzalloc().

Even when prep->datalen >= 11, if base64_decode() returns 0..3 (such as on
an 11-byte payload "DHHC-1:01::"), ret -= 4 becomes negative and wraps to
SIZE_MAX - 3 .. SIZE_MAX when passed as size_t len to crc32().  In addition,
base64_decode() returns -1 on error, which leaks out as -EPERM instead of
-EINVAL, and the decoded key length is never checked against the valid
DH-HMAC-CHAP sizes (32, 48, or 64 bytes).

[ ... ]

> +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 +
> +	 * CRC length (= 4) +
> +	 * ':' (= 1)
> +	 */
> +	keylen = nvme_dhchap_decoded_key_size(upayload->datalen) + 15;
> +	if (keylen < 0 || !buffer || buflen == 0)
> +		return keylen;
> +
> +	if (buflen < keylen)
> +		return -EINVAL;

[Severity: Medium]
Since keylen is an unsigned size_t, is keylen < 0 always false here?
Adding 15 before checking for an error also turns -EINVAL from
nvme_dhchap_decoded_key_size() into -7 (-E2BIG), and double-counts the
4-byte CRC because nvme_dhchap_decoded_key_size() already returns the
encoded length of the key plus CRC (48, 72, or 92 bytes), so the total
formatted length is 4 bytes larger than what nvme_dhchap_psk_read()
actually writes (+ 11).

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


  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 [this message]
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
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-225040@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.