Skip to contentSkip to conversation

KEYS: fix overflow in keyctl_pkey_params_get_2()

PATCH In review Jarkko Sakkinen 1 participant 3 messages May 31 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH 1/1 KEYS: fix overflow in keyctl_pkey_params_get_2()

The length for the internal output buffer is calculated incorrectly, which can result overflow when a too small buffer is provided.

Fix the bug by allocating internal output with the size of the maximum length of the cryptographic primitive instead of caller provided size.

Cc [email protected] # v4.20+
Fixes 00d60fd3b932 ("KEYS: Provide keyctls to drive the new key type ops for asymmetric keys [ver #2]")

---
Alessandro, please correct if I put the last name correctly (and sincere apologies if not).

 security/keys/keyctl_pkey.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
security/keys/keyctl_pkey.c +8 -1
index 97bc27bbf079..ba150ee2d4a3 100644
@@ -138,28 +138,35 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par
 		if (uparams.in_len  > info.max_dec_size ||
 		    uparams.out_len > info.max_enc_size)
 			return -EINVAL;
+
+		params->out_len = info.max_enc_size;
 		break;
 	case KEYCTL_PKEY_DECRYPT:
 		if (uparams.in_len  > info.max_enc_size ||
 		    uparams.out_len > info.max_dec_size)
 			return -EINVAL;
+
+		params->out_len = info.max_dec_size;
 		break;
 	case KEYCTL_PKEY_SIGN:
 		if (uparams.in_len  > info.max_data_size ||
 		    uparams.out_len > info.max_sig_size)
 			return -EINVAL;
+
+		params->out_len = info.max_sig_size;
 		break;
 	case KEYCTL_PKEY_VERIFY:
 		if (uparams.in_len  > info.max_data_size ||
 		    uparams.in2_len > info.max_sig_size)
 			return -EINVAL;
+
+		params->out_len = info.max_sig_size;
 		break;
 	default:
 		BUG();
 	}
 
 	params->in_len  = uparams.in_len;
-	params->out_len = uparams.out_len; /* Note: same as in2_len */
 	return 0;
 }
 

--
2.47.3

author maintainer
#1 lore raw
Quoted 10 lines · Jarkko Sakkinen

The length for the internal output buffer is calculated incorrectly, which can result overflow when a too small buffer is provided.

Fix the bug by allocating internal output with the size of the maximum length of the cryptographic primitive instead of caller provided size.

Cc [email protected] # v4.20+
Fixes 00d60fd3b932 ("KEYS: Provide keyctls to drive the new key type ops for asymmetric keys [ver #2]")

Should be available in -next within a day or along the lines so please be quick with tags/feedback. I'll forward a PR as soon as all is good.

BR, Jarkko

author maintainer
#3 lore raw
In reply to a message not in the archive
Quoted 8 lines · Alessandro G

Hi Jarkko,

The surname is “Groppo” instead of “Grupp”, don’t worry and thanks for asking!

Thanks also for the fix!

BR,
Alessandro

Thank you! This was a super good bug report.

Since I cannot do it without permission, can I add your tested-by to the patch?

BR, Jarkko

Quoted 23 lines

Il giorno dom 31 mag 2026 alle 05:25 Jarkko Sakkinen <[email protected]> ha scritto:

On Sun, May 31, 2026 at 05:49:13AM +0300, Jarkko Sakkinen wrote:
> The length for the internal output buffer is calculated incorrectly, which
> can result overflow when a too small buffer is provided.
>
> Fix the bug by allocating internal output with the size of the maximum > length of the cryptographic primitive instead of caller provided size. >
> Cc: [email protected] # v4.20+
> Fixes: 00d60fd3b932 ("KEYS: Provide keyctls to drive the new key type ops for asymmetric keys [ver #2]")
> Reported-by: Alessandro Grupp <[email protected]>
> Signed-off-by: Jarkko Sakkinen <[email protected]>

Should be available in -next within a day or along the lines so please be quick with tags/feedback. I'll forward a PR as soon as all is good.

BR, Jarkko