From 01c130031806010f4c563694501e8739f1580228 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Mon, 26 Jan 2026 11:13:44 +0100 Subject: tpm: Fix possible buffer overflow in PKDECRYPT * tpm2d/tpm2.c (tpm2_ecc_decrypt): Bail out on too long CIPHERTEXT. (tpm2_rsa_decrypt): Ditto. -- Cherry pick master commit of: 93fa34d9a346020355cd51d54102d30d4f177323 GnuPG-bug-id: 8045 Co-authored-by: NIIBE Yutaka Reported-by: OpenAI Security Research --- tpm2d/tpm2.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tpm2d/tpm2.c b/tpm2d/tpm2.c index 3e908ddb1..cd0347c6e 100644 --- a/tpm2d/tpm2.c +++ b/tpm2d/tpm2.c @@ -917,10 +917,20 @@ tpm2_ecc_decrypt (ctrl_t ctrl, TSS_CONTEXT *tssc, TPM_HANDLE key, size_t len; int ret; +#if defined(TPM2_MAX_ECC_KEY_BYTES) /* Intel stack */ + if (ciphertext_len > 2*TPM2_MAX_ECC_KEY_BYTES + 1) + return GPG_ERR_TOO_LARGE; +#elif defined(MAX_ECC_KEY_BYTES) /* IBM stack */ + if (ciphertext_len > 2*MAX_ECC_KEY_BYTES + 1) + return GPG_ERR_TOO_LARGE; +#else +# error TMP2 header are not correctly installed +#endif + /* This isn't really a decryption per se. The ciphertext actually * contains an EC Point which we must multiply by the private key number. * - * The reason is to generate a diffe helman agreement on a shared + * The reason is to generate a diffie-hellman agreement on a shared * point. This shared point is then used to generate the per * session encryption key. */ @@ -976,6 +986,16 @@ tpm2_rsa_decrypt (ctrl_t ctrl, TSS_CONTEXT *tssc, TPM_HANDLE key, TPM_HANDLE ah; char *auth; +#if defined(TPM2_MAX_RSA_KEY_BYTES) /* Intel stack */ + if (ciphertext_len > TPM2_MAX_RSA_KEY_BYTES) + return GPG_ERR_TOO_LARGE; +#elif defined(MAX_RSA_KEY_BYTES) /* IBM stack */ + if (ciphertext_len > MAX_RSA_KEY_BYTES) + return GPG_ERR_TOO_LARGE; +#else +# error TMP2 header are not correctly installed +#endif + inScheme.scheme = TPM_ALG_RSAES; /* * apparent gcrypt error: occasionally rsa ciphertext will -- 2.39.5