From: sashiko-bot@kernel.org
To: "Hannes Reinecke" <hare@kernel.org>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v6 2/9] nvme-keyring: add 'dhchap' key type
Date: Mon, 23 Nov 2026 17:31:24 +0000 [thread overview]
Message-ID: <sashiko-outbox-229754@kernel.org> (raw)
In-Reply-To: <20261123-dhchap-keyring-v4-v6-2-5082a49715ab@kernel.org>
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Critical] nvme-keyring: out-of-bounds read on prep->data in
nvme_dhchap_psk_preparse()
- [Medium] nvme-keyring: broken colon handling in
nvme_dhchap_psk_preparse()
- [Medium] nvme-keyring: broken buffer length check in
nvme_dhchap_psk_read()
- [Low] nvme-keyring: use kfree_sensitive() on error in
nvme_dhchap_psk_preparse()
- [Low] nvme-keyring: inverted name and comment for
nvme_dhchap_decoded_key_size()
--
commit 821eb4c808c34e952c59ff2fa90f5898d0eb2667
Author: Hannes Reinecke <hare@kernel.org>
nvme-keyring: add 'dhchap' key type
This commit adds a 'dhchap' key type to the NVMe keyring to store and read
DH-HMAC-CHAP secret keys in a user-key compatible payload.
> diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
> index 87cb51e1f5a7f..fc7f833a7c6b6 100644
> --- a/drivers/nvme/common/keyring.c
> +++ b/drivers/nvme/common/keyring.c
> @@ -253,6 +256,213 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
[ ... ]
> +static int nvme_dhchap_psk_preparse(struct key_preparsed_payload *prep)
> +{
> + struct user_key_payload *upayload;
> + size_t datalen = prep->datalen;
> + int ret, offset = 0;
> + char *p;
> + 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:%n",
> + &version, &hmac, &offset) != 2 || offset != 10) {
> + pr_debug("%s: invalid key header\n", __func__);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + return -EINVAL;
> + }
> +
> + if (version != 1 || hmac > 3) {
> + pr_debug("%s: invalid key version\n", __func__);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + return -EINVAL;
> + }
> +
> + /* skip header and final ':' character */
> + if (offset > prep->datalen) {
> + pr_debug("%s: invalid key data\n", __func__);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + return -EINVAL;
> + }
[Severity: Critical]
As reported by Martin Belanger on v5 [1], can sscanf() and the pr_debug()
below read past the end of prep->data?
add_key() allocates a buffer of size plen (prep->datalen) and copies the raw
userspace payload without appending a NUL terminator:
security/keys/keyctl.c:SYSCALL_DEFINE5(add_key)() {
...
if (_payload) {
payload = kvmalloc(plen, GFP_KERNEL);
if (!payload)
goto error2;
ret = -EFAULT;
if (copy_from_user(payload, _payload, plen) != 0)
goto error3;
}
...
}
When userspace passes a non-NUL-terminated payload (for example, fewer than
10 bytes, or "DHHC-" followed by whitespace), sscanf() scans past
prep->data + prep->datalen into adjacent kernel memory before the
offset > prep->datalen check runs. Similarly, when base64_decode() fails
below, pr_debug("%s: Failed to decode key %s\n", ...) formats
(char *)prep->data + offset with %s without bounding it by datalen.
> + datalen = prep->datalen - offset;
> + p = memchr(prep->data + offset, ':', datalen);
> + if (p)
> + datalen--;
> +
> + /*
> + * 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;
> + }
[Severity: Medium]
Does decrementing datalen by 1 leave the trailing ':' in the base64 input
whenever the payload ends with a NUL byte or newline?
If userspace passes a NUL-terminated or newline-terminated key string, ':'
is not the final byte of prep->data, so datalen-- only strips the trailing
NUL or newline and leaves ':' inside the slice passed to base64_decode(),
which then fails with -EINVAL. Truncating at p (datalen = p - (char *)
(prep->data + offset)) and validating that datalen is a supported encoded
key size before calling kzalloc() would also avoid allocating an arbitrary
buffer up to 1 MB prior to length validation.
> +
> + /* 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 %s\n",
> + __func__, (char *)prep->data + offset);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + kfree(upayload);
> + return -EINVAL;
> + }
> + if (ret != 36 && ret != 52 && ret != 68) {
> + pr_debug("%s: invalid decoded key size '%d'\n",
> + __func__, ret);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + kfree(upayload);
> + return -EINVAL;
> + }
> + ret -= 4;
> + crc = ~crc32(~0, upayload->data, ret);
> + if (get_unaligned_le32(upayload->data + ret) != crc) {
> + pr_debug("%s: CRC mismatch for key\n", __func__);
> + /* CRC mismatch */
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + kfree(upayload);
> + return -EKEYREJECTED;
> + }
[Severity: Low]
Should these three error paths use kfree_sensitive(upayload) instead of
kfree(upayload)? base64_decode() has already written decoded secret key
bytes into upayload->data, and clearing prep->payload.data[0] prevents
user_free_preparse() from zeroizing the buffer.
[ ... ]
> +/**
> + * nvme_dhchap_decoded_key_size - Size of the base64-decoded 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_decoded_key_size(int size)
> +{
> + int keylen = -EINVAL;
> +
> + switch (size) {
> + case 32:
> + keylen = 48;
> + break;
> + case 48:
> + keylen = 72;
> + break;
> + case 64:
> + keylen = 92;
> + break;
> + default:
> + break;
> + }
> + return keylen;
> +}
[Severity: Low]
This isn't a bug, but the function name and kernel-doc comment are inverted:
this helper takes the decoded key size (32, 48, or 64) and returns the
base64-encoded size including the 4-byte CRC (48, 72, or 92).
> +
> +/**
> + * nvme_dhchap_psk_read - read callback for dhchap key types
> + * @key: key to read from
> + * @buffer: buffer for the key contents
> + * @buflen: length of @buffer
> + *
> + * Formats the DH-HMAC-CHAP key in base64-encoded form as
> + * defined in the NVM Express TCP Transport Specification Rev 1.1,
> + * Section 3.6.1.5 'PSK Interchange Format'. The payload of @key
> + * is assumed to be of 'struct user_key_payload', with the data
> + * consisting of the key data of length 'upayload->datalen', followed
> + * by four bytes of CRC, one byte version, and one byte hmac fields.
> + */
> +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_decoded_key_size(upayload->datalen);
> + if (ret < 0)
> + return ret;
> + keylen = ret + 11;
> + if (!buffer && !buflen)
> + return keylen;
> + else if (buflen < keylen)
> + return -EINVAL;
> +
> + memset(buffer, 0, buflen);
[Severity: Medium]
Does this buffer check violate the key_type->read() contract and allow a
NULL pointer dereference in memset()?
Per include/linux/key-type.h and keyctl_read_key(), ->read() should return
the required buffer size whenever buffer is NULL or buflen is too small
(if (!buffer || buflen < keylen) return keylen;). As written, calling
keyctl(KEYCTL_READ) with a non-NULL buffer and 0 < buflen < keylen fails
with -EINVAL instead of returning keylen, and a call with buffer == NULL and
buflen >= keylen falls through to memset(NULL, 0, buflen).
[1] https://lore.kernel.org/r/463a55e8-c2ef-4d83-94ff-7599c7d931e6@gmail.com
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-dhchap-keyring-v4-v6-0-5082a49715ab@kernel.org?part=2
next prev parent reply other threads:[~2026-11-23 17:31 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 17:10 [PATCH v6 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-23 17:10 ` [PATCH v6 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-23 17:10 ` [PATCH v6 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-23 17:31 ` sashiko-bot [this message]
2026-11-23 17:10 ` [PATCH v6 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-23 17:32 ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-23 17:26 ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-23 17:30 ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-23 17:32 ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-23 17:10 ` [PATCH v6 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-23 17:30 ` sashiko-bot
2026-11-23 17:10 ` [PATCH v6 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-23 17:33 ` 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-229754@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox