From: sashiko-bot@kernel.org
To: "Hannes Reinecke" <hare@kernel.org>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v5 2/9] nvme-keyring: add 'dhchap' key type
Date: Mon, 23 Nov 2026 12:36:50 +0000 [thread overview]
Message-ID: <sashiko-outbox-229232@kernel.org> (raw)
In-Reply-To: <20261123-dhchap-keyring-v4-v5-2-81eefb4c0724@kernel.org>
Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [Critical] nvme-keyring: out-of-bounds read on prep->data in
nvme_dhchap_psk_preparse()
- [High] nvme-keyring: uninitialized offset used in
nvme_dhchap_psk_preparse()
- [High] nvme-keyring: header offset mismatch in
nvme_dhchap_psk_preparse()
- [High] nvme-keyring: memory leak of upayload on error in
nvme_dhchap_psk_preparse()
- [Medium] nvme-keyring: broken key length queries in
nvme_dhchap_psk_read()
- [Low] nvme-keyring: inverted naming and comment for
nvme_dhchap_decoded_key_size()
- [Low] nvme-keyring: missing select CRC32 in Kconfig for
nvme_dhchap_psk_preparse()
- [Low] nvme-keyring: redundant nvme_dhchap_psk_match_preparse() forces
linear scan
--
commit d9b56f363bf407639fb6f4f459820389f42a36f1
Author: Hannes Reinecke <hare@kernel.org>
nvme-keyring: add 'dhchap' key type
This commit registers a 'dhchap' key type in the NVMe keyring module to
parse, store, and read back base64-encoded DH-HMAC-CHAP secret keys.
> diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
> index 87cb51e1f5a7f..d0c36d153afdb 100644
> --- a/drivers/nvme/common/keyring.c
> +++ b/drivers/nvme/common/keyring.c
> @@ -4,6 +4,9 @@
> */
>
> #include <linux/module.h>
> +#include <linux/crc32.h>
> +#include <linux/base64.h>
> +#include <linux/unaligned.h>
> #include <linux/seq_file.h>
> #include <linux/key-type.h>
> #include <keys/user-type.h>
[Severity: Low]
nvme_dhchap_psk_preparse() calls crc32(), which expands to crc32_le(), but
config NVME_KEYRING in drivers/nvme/common/Kconfig only selects KEYS and does
not select CRC32. Should NVME_KEYRING select CRC32 so builds without
CONFIG_CRC32 enabled by another option do not fail at link time?
> @@ -253,6 +256,231 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
[ ... ]
> +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);
> + if (match_len != strlen(key->description)) {
> + pr_debug("%s: match '%s' '%s' different size\n",
> + __func__, match_id, key->description);
> + return false;
> + }
> + pr_debug("%s: match '%s' '%s' len %zd\n",
> + __func__, match_id, key->description, match_len);
> +
> + return !memcmp(key->description, match_id, match_len);
> +}
> +
> +static int nvme_dhchap_psk_match_preparse(struct key_match_data *match_data)
> +{
> + match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
> + match_data->cmp = nvme_dhchap_psk_match;
> + return 0;
> +}
[Severity: Low]
Is nvme_dhchap_psk_match_preparse() needed here? nvme_dhchap_psk_match()
checks for an exact description match identical to key_default_cmp(), so
overriding lookup_type to KEYRING_SEARCH_LOOKUP_ITERATE forces every dhchap
key lookup to do a linear scan of the keyring instead of a direct associative
array lookup.
> +
> +/**
> + * 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, offset;
> + 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) {
> + pr_debug("%s: invalid key data '%s'\n", __func__,
> + (char *)prep->data);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + return -EINVAL;
> + }
[Severity: High]
Can offset be used uninitialized when the second ':' in the header is
missing?
In vsscanf(), %n does not increment the conversion count. An input such as
"DHHC-1:00" converts version and hmac, fails to match the trailing ':', and
returns 2 before reaching %n. Because offset is not initialized to 0,
!offset reads whatever value is on the stack and later passes
prep->data + offset to strlen() and strrchr().
> +
> + /* skip header and final ':' character */
> + datalen = strlen(prep->data + offset);
> + p = strrchr(prep->data + offset, ':');
> + if (p)
> + datalen--;
[Severity: Critical]
Can sscanf(), strlen(), strrchr(), and pr_debug() read past the end of
prep->data?
add_key() and keyctl_update_key() allocate an exact plen-byte buffer with
kvmalloc() and copy the user payload without adding a NUL terminator:
security/keys/keyctl.c:SYSCALL_DEFINE5(add_key) {
...
if (plen) {
ret = -ENOMEM;
payload = kvmalloc(plen, GFP_KERNEL);
if (!payload)
goto error2;
ret = -EFAULT;
if (copy_from_user(payload, _payload, plen) != 0)
goto error3;
}
...
}
nvme_dhchap_psk_preparse() never bounds prep->data by prep->datalen before
treating it as a NUL-terminated string, so a non-NUL-terminated payload from
userspace causes out-of-bounds kernel memory reads.
> +
> + /*
> + * 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->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);
> + prep->payload.data[0] = NULL;
> + return ret;
> + }
[Severity: High]
Can base64_decode() read 1 byte past the key buffer and decode the wrong
slice when offset is not 10?
Because 2 in %02hhu is a maximum field width, a header with a single-digit
HMAC such as "DHHC-1:1:<base64>" sets offset to 9. datalen is computed from
prep->data + offset, but base64_decode() starts at prep->data + 10, skipping
the first base64 character and reading 1 byte past the end of the string.
Also, if a trailing ':' is followed by any other character (such as a
newline), decrementing datalen by 1 instead of using p - (prep->data + offset)
leaves trailing characters in datalen. And when base64_decode() fails it
returns -1, so returning ret directly returns -EPERM to userspace instead of
-EINVAL.
> + if (ret != 36 && ret != 52 && ret != 68) {
> + pr_debug("%s: invalid decoded key size '%d'\n",
> + __func__, ret);
> + prep->payload.data[0] = NULL;
> + 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;
> + return -EKEYREJECTED;
> + }
[Severity: High]
Does clearing prep->payload.data[0] = NULL on the error paths above leak
upayload?
When ->preparse() returns an error, the keyring core calls ->free_preparse(),
which is user_free_preparse() and frees prep->payload.data[0] via
kfree_sensitive(). Setting prep->payload.data[0] = NULL on base64_decode(),
key size, and CRC failures without freeing upayload causes
user_free_preparse() to see NULL and leak the allocation.
[ ... ]
> +/**
> + * 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.
> + */
> +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 binary 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
> + *
> + * Formets 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;
> +
> + if (!buffer || buflen == 0)
> + return -EINVAL;
> +
> + 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 (buflen < keylen)
> + return -EINVAL;
[Severity: Medium]
Should nvme_dhchap_psk_read() return keylen instead of -EINVAL when buffer is
NULL, buflen is 0, or buflen < keylen?
key_type->read() is required to return the needed buffer size even when no
buffer or a too-small buffer is provided. keyctl_read_key() calls
__keyctl_read_key(key, NULL, 0) both when userspace probes the key length
(such as keyctl read or keyctl print) and on the first pass whenever buflen >
PAGE_SIZE, so returning -EINVAL breaks those reads.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-dhchap-keyring-v4-v5-0-81eefb4c0724@kernel.org?part=2
next prev parent reply other threads:[~2026-11-23 12:36 UTC|newest]
Thread overview: 20+ 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 [this message]
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 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 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 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 12:14 ` [PATCH v5 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-23 12:35 ` sashiko-bot
2026-11-23 12:59 ` [PATCH v5 0/9] nvme-auth: switch to use the kernel keyring Martin Belanger
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-229232@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