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

Hannes Reinecke hare at suse.de
Mon Nov 23 23:39:30 PST 2026


On 11/23/26 6:31 PM, sashiko-bot at kernel.org wrote:
> 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 at 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.
> 
Fixed with the next version.

>> +	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.
> 
Fixed with the next version.

>> +
>> +	/* 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.
> 
Indeed. Fixed with the next version.

> [ ... ]
> 
>> +/**
>> + * 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).
> 
Fixed with the next version.

>> +
>> +/**
>> + * 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
> 
Fixed with 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