From b3cf397deb769aa44ed12a658f406737c8982f87 Mon Sep 17 00:00:00 2001 From: jiangfangjie 00559066 Date: Tue, 6 Apr 2021 18:19:30 +0800 Subject: [PATCH 1/2] tpm2: CryptSym: fix AES output IV The TPM is supposed to provide the output IV in the ivInOut parameter in CryptSymmetricEncrypt. In the case of using the openssl routines, the output IV is missed, and the resulting output from the TPM is in the input IV. OpenSSL unfortunately does not export EVP_CIPHER_CTX_iv() until tags/OpenSSL_1_1_0, so we have to fall back to the reference code for previous OpenSSL versions. Fixes: CVE-2021-3446 buglink:https://bugzilla.redhat.com/show_bug.cgi?id=1939664 Signed-off-by: William Roberts Signed-off-by: Stefan Berger Signed-off-by: jiangfangjie 00559066 (cherry picked from commit a1a4809abfb68ff104cbcb655744311205067fa0) --- 0001-tpm2-CryptSym-fix-AES-output-IV.patch | 85 ++++++++++++++++++++++ libtpms.spec | 19 ++++- 2 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 0001-tpm2-CryptSym-fix-AES-output-IV.patch diff --git a/0001-tpm2-CryptSym-fix-AES-output-IV.patch b/0001-tpm2-CryptSym-fix-AES-output-IV.patch new file mode 100644 index 0000000..f1c2c36 --- /dev/null +++ b/0001-tpm2-CryptSym-fix-AES-output-IV.patch @@ -0,0 +1,85 @@ +From 8a1716c3bb18bac169f68d24cdd095cf617eb908 Mon Sep 17 00:00:00 2001 +From: root +Date: Tue, 6 Apr 2021 16:22:04 +0800 +Subject: [PATCH] tpm2: CryptSym: fix AES output IV The TPM is supposed to + provide the output IV in the ivInOut parameter in CryptSymmetricEncrypt. In + the case of using the openssl routines, the output IV is missed, and the + resulting output from the TPM is in the input IV. + +OpenSSL unfortunately does not export EVP_CIPHER_CTX_iv() until +tags/OpenSSL_1_1_0, so we have to fall back to the reference code for +previous OpenSSL versions. + +Signed-off-by: William Roberts +Signed-off-by: Stefan Berger +--- + configure.ac | 1 + + src/tpm2/crypto/openssl/CryptSym.c | 19 +++++++++++++++++++ + 2 files changed, 20 insertions(+) + +diff --git a/configure.ac b/configure.ac +index 1bb45d1..0c57ef3 100644 +--- a/configure.ac ++++ b/configure.ac +@@ -165,6 +165,7 @@ AS_IF([test "x$enable_use_openssl_functions" != "xno"], [ + AC_CHECK_LIB([crypto], [EVP_aes_128_cbc],, not_found=1) + AC_CHECK_LIB([crypto], [EVP_des_ede3_cbc],, not_found=1) + AC_CHECK_LIB([crypto], [DES_random_key],, not_found=1) ++ AC_CHECK_LIB([crypto], [EVP_CIPHER_CTX_iv],, not_found=1) + if test "x$not_found" = "x0"; then + use_openssl_functions_symmetric=1 + use_openssl_functions_for="symmetric (AES, TDES) " +diff --git a/src/tpm2/crypto/openssl/CryptSym.c b/src/tpm2/crypto/openssl/CryptSym.c +index 7aa90da..856def6 100644 +--- a/src/tpm2/crypto/openssl/CryptSym.c ++++ b/src/tpm2/crypto/openssl/CryptSym.c +@@ -531,6 +531,7 @@ CryptSymmetricEncrypt( + BYTE keyToUse[MAX_SYM_KEY_BYTES]; + UINT16 keyToUseLen = (UINT16)sizeof(keyToUse); + TPM_RC retVal = TPM_RC_SUCCESS; ++ int ivLen; + + pAssert(dOut != NULL && key != NULL && dIn != NULL); + if(dSize == 0) +@@ -595,6 +596,14 @@ CryptSymmetricEncrypt( + if (EVP_EncryptFinal_ex(ctx, pOut + outlen1, &outlen2) != 1) + ERROR_RETURN(TPM_RC_FAILURE); + ++ if (ivInOut) { ++ ivLen = EVP_CIPHER_CTX_iv_length(ctx); ++ if (ivLen < 0 || (size_t)ivLen > sizeof(ivInOut->t.buffer)) ++ ERROR_RETURN(TPM_RC_FAILURE); ++ ++ ivInOut->t.size = ivLen; ++ memcpy(ivInOut->t.buffer, EVP_CIPHER_CTX_iv(ctx), ivInOut->t.size); ++ } + Exit: + if (retVal == TPM_RC_SUCCESS && pOut != dOut) + memcpy(dOut, pOut, outlen1 + outlen2); +@@ -636,6 +645,7 @@ CryptSymmetricDecrypt( + BYTE keyToUse[MAX_SYM_KEY_BYTES]; + UINT16 keyToUseLen = (UINT16)sizeof(keyToUse); + TPM_RC retVal = TPM_RC_SUCCESS; ++ int ivLen; + + // These are used but the compiler can't tell because they are initialized + // in case statements and it can't tell if they are always initialized +@@ -707,6 +717,15 @@ CryptSymmetricDecrypt( + + pAssert((int)buffersize >= outlen1 + outlen2); + ++ if (ivInOut) { ++ ivLen = EVP_CIPHER_CTX_iv_length(ctx); ++ if (ivLen < 0 || (size_t)ivLen > sizeof(ivInOut->t.buffer)) ++ ERROR_RETURN(TPM_RC_FAILURE); ++ ++ ivInOut->t.size = ivLen; ++ memcpy(ivInOut->t.buffer, EVP_CIPHER_CTX_iv(ctx), ivInOut->t.size); ++ } ++ + Exit: + if (retVal == TPM_RC_SUCCESS) { + pAssert(dSize >= outlen1 + outlen2); +-- +2.27.0 + diff --git a/libtpms.spec b/libtpms.spec index 32e2dd5..5ef5b12 100644 --- a/libtpms.spec +++ b/libtpms.spec @@ -1,8 +1,8 @@ # --- libtpm rpm-spec --- %define name libtpms -%define version 0.7.3 -%define release 1 +%define versionx 0.7.3 +%define release 3 # Valid crypto subsystems are 'freebl' and 'openssl' %if "%{?crypto_subsystem}" == "" @@ -14,14 +14,16 @@ Summary: Library providing Trusted Platform Module (TPM) functionality Name: %{name} -Version: %{version} -Release: %{release}%{?dist} +Version: %{versionx} +Release: 3 License: BSD Group: Development/Libraries Url: http://github.com/stefanberger/libtpms Source: libtpms-%{version}.tar.gz Provides: libtpms-%{crypto_subsystem} = %{version}-%{release} +Patch0: 0001-tpm2-CryptSym-fix-AES-output-IV.patch + %if "%{crypto_subsystem}" == "openssl" BuildRequires: openssl-devel %else @@ -112,6 +114,15 @@ rm -f $RPM_BUILD_ROOT%{_libdir}/libtpms.la %postun -p /sbin/ldconfig %changelog +* Mon Apr 5 2021 jiangfangjie - 0.7.3-3 +- Type:CVE +- ID:NA +- SUG:NA +- DESC: fix CVE-2021-3446 + +* Mon Sep 14 2020 jiangfangjie - 0.7.3-2 +- update spec file including source0 and update source file + * Fri Aug 21 2020 jiangfangjie - 0.7.3-1 - Package init - Version of library is now 0.7.3 -- Gitee From 8f9eb0b8e7141528ac11ecbb04f09fde4e910680 Mon Sep 17 00:00:00 2001 From: jiangfangjie 00559066 Date: Wed, 7 Apr 2021 10:38:52 +0800 Subject: [PATCH 2/2] update spec file Signed-off-by: jiangfangjie 00559066 (cherry picked from commit 1c62fb2410c9e03740f27f7200d2ba3ac0013b28) --- libtpms.spec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libtpms.spec b/libtpms.spec index 5ef5b12..252a389 100644 --- a/libtpms.spec +++ b/libtpms.spec @@ -72,7 +72,8 @@ Libtpms header files and documentation. %attr(644, root, root) %{_mandir}/man3/* %prep -%setup -q +%setup -n %{name}-%{gitcommit} +%autopatch -p1 %build -- Gitee