From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga02.intel.com (mga02.intel.com []) by mx.groups.io with SMTP id smtpd.web11.12696.1580998775768725774 for ; Thu, 06 Feb 2020 06:19:43 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=fail (domain: intel.com, ip: , mailfrom: jian.j.wang@intel.com) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Feb 2020 06:19:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,409,1574150400"; d="scan'208";a="226160707" Received: from shwdeopensfp777.ccr.corp.intel.com ([10.239.158.78]) by fmsmga008.fm.intel.com with ESMTP; 06 Feb 2020 06:19:42 -0800 From: "Wang, Jian J" To: devel@edk2.groups.io Cc: Jiewen Yao , Chao Zhang Subject: [PATCH 9/9] SecurityPkg/DxeImageVerificationLib: Differentiate error and search result in IsSignatureFoundInDatabase(CVE-2019-14575) Date: Thu, 6 Feb 2020 22:19:33 +0800 Message-Id: <20200206141933.356-10-jian.j.wang@intel.com> X-Mailer: git-send-email 2.24.0.windows.2 In-Reply-To: <20200206141933.356-1-jian.j.wang@intel.com> References: <20200206141933.356-1-jian.j.wang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1608 To avoid false-negative issue in check hash against dbx, both error condition (as return value) and check result (as out parameter) of IsSignatureFoundInDatabase() are added. So the caller of this function will know exactly if a failure is caused by a black list hit or other error happening, and enforce a more secure operation to prevent secure boot from being bypassed. For a white list check (db), there's no such necessity. All intermediate results inside this function will be checked and returned immediately upon any failure or error, like out-of-resource, hash calculation error or certificate retrieval failure. Cc: Jiewen Yao Cc: Chao Zhang Signed-off-by: Jian J Wang --- .../DxeImageVerificationLib.c | 77 ++++++++++++++----- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificati= onLib.c b/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationL= ib.c index 5b7a67f811..8e599ca0be 100644 --- a/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c +++ b/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c @@ -955,17 +955,19 @@ Done: @param[in] Signature Pointer to signature that is searched fo= r.=0D @param[in] CertType Pointer to hash algorithm.=0D @param[in] SignatureSize Size of Signature.=0D + @param[out] IsFound Search result. Only valid if EFI_SUCCESS= returned=0D =0D - @return TRUE Found the signature in the variable data= base.=0D - @return FALSE Not found the signature in the variable = database.=0D + @retval EFI_SUCCESS Finished the search without any error.=0D + @retval Others Error occurred in the search of database= .=0D =0D **/=0D -BOOLEAN=0D +EFI_STATUS=0D IsSignatureFoundInDatabase (=0D - IN CHAR16 *VariableName,=0D - IN UINT8 *Signature,=0D - IN EFI_GUID *CertType,=0D - IN UINTN SignatureSize=0D + IN CHAR16 *VariableName,=0D + IN UINT8 *Signature,=0D + IN EFI_GUID *CertType,=0D + IN UINTN SignatureSize,=0D + OUT BOOLEAN *IsFound=0D )=0D {=0D EFI_STATUS Status;=0D @@ -975,22 +977,28 @@ IsSignatureFoundInDatabase ( UINT8 *Data;=0D UINTN Index;=0D UINTN CertCount;=0D - BOOLEAN IsFound;=0D =0D //=0D // Read signature database variable.=0D //=0D - IsFound =3D FALSE;=0D + *IsFound =3D FALSE;=0D Data =3D NULL;=0D DataSize =3D 0;=0D Status =3D gRT->GetVariable (VariableName, &gEfiImageSecurityDatabase= Guid, NULL, &DataSize, NULL);=0D if (Status !=3D EFI_BUFFER_TOO_SMALL) {=0D - return FALSE;=0D + if (Status =3D=3D EFI_NOT_FOUND) {=0D + //=0D + // No database, no need to search.=0D + //=0D + Status =3D EFI_SUCCESS;=0D + }=0D +=0D + return Status;=0D }=0D =0D Data =3D (UINT8 *) AllocateZeroPool (DataSize);=0D if (Data =3D=3D NULL) {=0D - return FALSE;=0D + return EFI_OUT_OF_RESOURCES;=0D }=0D =0D Status =3D gRT->GetVariable (VariableName, &gEfiImageSecurityDatabaseGui= d, NULL, &DataSize, Data);=0D @@ -1010,7 +1018,7 @@ IsSignatureFoundInDatabase ( //=0D // Find the signature in database.=0D //=0D - IsFound =3D TRUE;=0D + *IsFound =3D TRUE;=0D //=0D // Entries in UEFI_IMAGE_SECURITY_DATABASE that are used to vali= date image should be measured=0D //=0D @@ -1023,7 +1031,7 @@ IsSignatureFoundInDatabase ( Cert =3D (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->Signat= ureSize);=0D }=0D =0D - if (IsFound) {=0D + if (*IsFound) {=0D break;=0D }=0D }=0D @@ -1037,7 +1045,7 @@ Done: FreePool (Data);=0D }=0D =0D - return IsFound;=0D + return Status;=0D }=0D =0D /**=0D @@ -1642,6 +1650,8 @@ DxeImageVerificationHandler ( CHAR16 *NameStr;=0D RETURN_STATUS PeCoffStatus;=0D EFI_STATUS HashStatus;=0D + EFI_STATUS DbStatus;=0D + BOOLEAN IsFound;=0D =0D SignatureList =3D NULL;=0D SignatureListSize =3D 0;=0D @@ -1650,7 +1660,7 @@ DxeImageVerificationHandler ( PkcsCertData =3D NULL;=0D Action =3D EFI_IMAGE_EXECUTION_AUTH_UNTESTED;=0D IsVerified =3D FALSE;=0D -=0D + IsFound =3D FALSE;=0D =0D //=0D // Check the image type and get policy setting.=0D @@ -1792,7 +1802,14 @@ DxeImageVerificationHandler ( goto Failed;=0D }=0D =0D - if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDi= gest, &mCertType, mImageDigestSize)) {=0D + DbStatus =3D IsSignatureFoundInDatabase (=0D + EFI_IMAGE_SECURITY_DATABASE1,=0D + mImageDigest,=0D + &mCertType,=0D + mImageDigestSize,=0D + &IsFound=0D + );=0D + if (EFI_ERROR (DbStatus) || IsFound) {=0D //=0D // Image Hash is in forbidden database (DBX).=0D //=0D @@ -1800,7 +1817,14 @@ DxeImageVerificationHandler ( goto Failed;=0D }=0D =0D - if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDig= est, &mCertType, mImageDigestSize)) {=0D + DbStatus =3D IsSignatureFoundInDatabase (=0D + EFI_IMAGE_SECURITY_DATABASE,=0D + mImageDigest,=0D + &mCertType,=0D + mImageDigestSize,=0D + &IsFound=0D + );=0D + if (!EFI_ERROR (DbStatus) && IsFound) {=0D //=0D // Image Hash is in allowed database (DB).=0D //=0D @@ -1888,14 +1912,29 @@ DxeImageVerificationHandler ( //=0D // Check the image's hash value.=0D //=0D - if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDi= gest, &mCertType, mImageDigestSize)) {=0D + DbStatus =3D IsSignatureFoundInDatabase (=0D + EFI_IMAGE_SECURITY_DATABASE1,=0D + mImageDigest,=0D + &mCertType,=0D + mImageDigestSize,=0D + &IsFound=0D + );=0D + if (EFI_ERROR (DbStatus) || IsFound) {=0D Action =3D EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;=0D DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but %s= hash of image is found in DBX.\n", mHashTypeStr));=0D IsVerified =3D FALSE;=0D break;=0D }=0D +=0D if (!IsVerified) {=0D - if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageD= igest, &mCertType, mImageDigestSize)) {=0D + DbStatus =3D IsSignatureFoundInDatabase (=0D + EFI_IMAGE_SECURITY_DATABASE,=0D + mImageDigest,=0D + &mCertType,=0D + mImageDigestSize,=0D + &IsFound=0D + );=0D + if (!EFI_ERROR (DbStatus) && IsFound) {=0D IsVerified =3D TRUE;=0D } else {=0D DEBUG ((DEBUG_INFO, "DxeImageVerificationLib: Image is signed but = signature is not allowed by DB and %s hash of image is not found in DB/DBX.= \n", mHashTypeStr));=0D --=20 2.24.0.windows.2