[PATCH v4 2/9] nvme-keyring: add 'dhchap' key type

Hannes Reinecke hare at suse.de
Fri Nov 20 04:28:27 PST 2026


On 11/20/26 12:28 PM, sashiko-bot at kernel.org wrote:
> 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 at 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.
> 
Correct, the size for both strings should match. I'll fix it up for the
next round.

> [ ... ]
> 
>> +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.
> 
Indeed. Fixed up for the next version.

>> +	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().
> 
Correct. I've updated the parsing code to the improved version in
nvme_auth_extract_key().

> 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).
> 
Ok, fixed up for the next version.

> [ ... ]
> 
>> +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).
> 
Ok, fixed up for the next version.

Cheers,

Hannes
-- 
Dr. Hannes Reinecke                  Kernel Storage Architect
hare at suse.de                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich



More information about the Linux-nvme mailing list