[PATCH v7 02/14] nvme-keyring: add 'dhchap' key type

Hannes Reinecke hare at suse.de
Thu Nov 26 07:08:45 PST 2026


On 11/26/26 3:32 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 in
>    nvme_dhchap_psk_preparse() via sscanf()
> - [Medium] nvme-keyring: nvme_dhchap_psk_read() returns -EINVAL when
>    buflen < keylen
> - [Medium] nvme-keyring: missing .update callback in
>    nvme_dhchap_psk_key_type
> - [Low] nvme-keyring: inverted kernel-doc for
>    nvme_dhchap_encoded_key_size()
> - [Low] nvme-keyring: unbounded kzalloc() in nvme_dhchap_psk_preparse()
> --
> 
> commit 96ec6bac915bae99bd1eb311a1d2b97cd0c724f7
> Author: Hannes Reinecke <hare at kernel.org>
> 
> nvme-keyring: add 'dhchap' key type
> 
> This commit registers a new dhchap key type to store DH-HMAC-CHAP secret
> keys with a user-type compatible payload and base64 read formatting.
> 
>> diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
>> index 87cb51e1f5a7f..de8dab10fa105 100644
>> --- a/drivers/nvme/common/keyring.c
>> +++ b/drivers/nvme/common/keyring.c
> [ ... ]
>> @@ -253,6 +256,205 @@ 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);
>> +}
>> +
>> +/**
>> + * 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 = -EINVAL, offset = 0;
>> +	char *p;
>> +	u32 crc;
>> +	u8 version, hmac;
>> +
>> +	if (!prep->data || prep->datalen < 10) {
>> +		pr_debug("%s: Invalid key data", __func__);
>> +		goto out_err;
>> +	}
>> +	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
>> +		   &version, &hmac, &offset) != 2 || offset != 10) {
>> +		pr_debug("%s: invalid key header\n", __func__);
>> +		goto out_err;
>> +	}
> 
> [Severity: Critical]
> As reported by Martin Belanger on v5 [1], can sscanf() read past the end
> of prep->data?
> 
> add_key() copies plen bytes from userspace into prep->data without
> appending a NUL terminator:
> 
> security/keys/keyctl.c:SYSCALL_DEFINE5(add_key) {
>      ...
> 		payload = kvmalloc(plen, GFP_KERNEL);
> 		if (!payload)
> 			goto error2;
> 
> 		ret = -EFAULT;
> 		if (copy_from_user(payload, _payload, plen) != 0)
> 			goto error3;
>      ...
> }
> 
> Inside vsscanf(), each integer conversion calls skip_spaces() before
> parsing digits, which scans until a NUL byte and ignores prep->datalen.
> A non-NUL-terminated payload of 10 or more bytes starting with "DHHC-"
> or "DHHC-1:" followed by whitespace causes an out-of-bounds kernel memory
> read before the offset check runs.
> 
Fixed by allocating a temporary, NULL-terminated buffer.

>> +
>> +	if (version != 1 || hmac > 3) {
>> +		pr_debug("%s: invalid key version\n", __func__);
>> +		goto out_err;
>> +	}
>> +
>> +	/* skip header and final ':' character */
>> +	if (offset > prep->datalen) {
>> +		pr_debug("%s: invalid key data\n", __func__);
>> +		goto out_err;
>> +	}
>> +	datalen = prep->datalen - offset;
>> +	p = memchr(prep->data + offset, ':', datalen);
>> +	if (p) {
>> +		datalen = (p - (char *)prep->data);
>> +		datalen -= offset;
>> +	}
>> +
>> +	/*
>> +	 * 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) {
>> +		ret = -ENOMEM;
>> +		goto out_err;
>> +	}
>> +
>> +	/* 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\n", __func__);
>> +		ret = -EINVAL;
>> +		goto out_free;
>> +	}
>> +	if (ret != 36 && ret != 52 && ret != 68) {
> 
> [Severity: Low]
> Should datalen be checked against the valid base64-encoded key lengths
> (48, 72, or 92 bytes) before calling kzalloc() and base64_decode()?
> 
> Because add_key() allows payloads up to almost 1 MiB, an oversized
> payload without an early trailing colon triggers a large physically
> contiguous GFP_KERNEL allocation and decodes the entire buffer before
> being rejected by the decoded length check.
> 
Added a check at the start to catch overly long keys.

> [ ... ]
> 
>> +/**
>> + * nvme_dhchap_encoded_key_size - Size of the base64-encoded 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_encoded_key_size(int size)
> 
> [Severity: Low]
> This isn't a bug, but the kernel-doc for nvme_dhchap_encoded_key_size()
> has the input and return value reversed: @size is the raw decoded key
> length (32, 48, or 64 bytes, excluding the 4-byte CRC), and the return
> value is the base64-encoded length including the 4-byte CRC.  Also,
> nvme_dhchap_psk_describe() above is identical to nvme_tls_psk_describe()
> in the same file (and to user_describe()).
> 
Fixed with 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 +
>> +	 * ':' (= 1)
>> +	 */
>> +	ret = nvme_dhchap_encoded_key_size(upayload->datalen);
>> +	if (ret < 0)
>> +		return ret;
>> +	keylen = ret + 11;
>> +	if (!buffer || !buflen)
>> +		return keylen;
>> +	else if (buflen < keylen)
>> +		return -EINVAL;
> 
> [Severity: Medium]
> Should nvme_dhchap_psk_read() return keylen instead of -EINVAL when
> buflen < keylen?
> 
> The key_type->read() contract and keyctl_read_key() expect ->read() to
> return the required size without copying whenever the caller's buffer is
> too small, so returning -EINVAL breaks keyctl(KEYCTL_READ) callers that
> probe the key size with a small non-NULL buffer.
> 
Fixed with the next version.

>> +
>> +	memset(buffer, 0, buflen);
>> +	version = upayload->data[upayload->datalen + 4];
>> +	hmac = upayload->data[upayload->datalen + 5];
>> +	ret = sprintf(buffer, "DHHC-%01hhu:%02hhu:", version, hmac);
>> +	if (ret < 0)
>> +		return -ENOKEY;
>> +	/* Include the trailing CRC */
>> +	datalen = upayload->datalen + 4;
>> +	ret += base64_encode(upayload->data, datalen, buffer + ret,
>> +			     true, BASE64_STD);
>> +	buffer[ret] = ':';
>> +	ret++;
>> +	return ret;
>> +}
>> +
>> +static struct key_type nvme_dhchap_psk_key_type = {
>> +	.name           = "dhchap",
>> +	.flags          = KEY_TYPE_NET_DOMAIN,
>> +	.preparse       = nvme_dhchap_psk_preparse,
>> +	.free_preparse  = user_free_preparse,
>> +	.instantiate    = generic_key_instantiate,
>> +	.revoke         = user_revoke,
>> +	.destroy        = user_destroy,
>> +	.describe       = nvme_dhchap_psk_describe,
>> +	.read           = nvme_dhchap_psk_read,
>> +};
> 
> [Severity: Medium]
> As reported by Martin Belanger on v6 [2], should nvme_dhchap_psk_key_type
> define .update = user_update?
> 
> Without .update, keyctl(KEYCTL_UPDATE) fails with -EOPNOTSUPP and
> add_key() allocates a new key with a different serial number instead of
> updating an existing dhchap key in place, breaking key rotation for
> controllers referencing a pre-provisioned key by serial number.  The
> later commit "nvme-auth: allow to update pre-provisioned keys" in this
> series mentions adding .update to the dhchap key type, but adds
> .update = user_update to nvme_tls_psk_key_type instead.
> 
> [1] https://lore.kernel.org/r/463a55e8-c2ef-4d83-94ff-7599c7d931e6@gmail.com
> [2] https://lore.kernel.org/r/a71a3d9f-0f19-45f4-bc98-a5663ad537b2@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