Blame SOURCES/0058-Handle-binaries-with-multiple-signatures.patch

d84fc6
From 76c0447e204c7e4ce918c4887ce8aae0e0816271 Mon Sep 17 00:00:00 2001
d84fc6
From: Peter Jones <pjones@redhat.com>
d84fc6
Date: Thu, 23 Jul 2020 16:32:05 -0400
d84fc6
Subject: [PATCH 58/62] Handle binaries with multiple signatures.
d84fc6
d84fc6
This adds support for multiple signatures.  It first tries validating
d84fc6
the binary by hash, first against our dbx lists, then against our db
d84fc6
lists.  If it isn't allowed or rejected at that step, it continues to
d84fc6
the normal routine of checking all the signatures.
d84fc6
d84fc6
At this point it does *not* reject a binary just because a signature is
d84fc6
by a cert on a dbx list, though that will override any db list that
d84fc6
certificate is listed on.  If at any point any assertion about the
d84fc6
binary or signature list being well-formed fails, the binary is
d84fc6
immediately rejected, though we do allow skipping over signatures
d84fc6
which have an unsupported sig->Hdr.wCertificateType.
d84fc6
d84fc6
Signed-off-by: Peter Jones <pjones@redhat.com>
d84fc6
Upstream: pr#210
d84fc6
---
d84fc6
 shim.c | 287 +++++++++++++++++++++++++++++++++++++++------------------
d84fc6
 1 file changed, 198 insertions(+), 89 deletions(-)
d84fc6
d84fc6
diff --git a/shim.c b/shim.c
d84fc6
index ee62248ca4e..d10a1ba1cac 100644
d84fc6
--- a/shim.c
d84fc6
+++ b/shim.c
d84fc6
@@ -690,7 +690,7 @@ static EFI_STATUS check_whitelist (WIN_CERTIFICATE_EFI_PKCS *cert,
d84fc6
 	}
d84fc6
 
d84fc6
 	update_verification_method(VERIFIED_BY_NOTHING);
d84fc6
-	return EFI_SECURITY_VIOLATION;
d84fc6
+	return EFI_NOT_FOUND;
d84fc6
 }
d84fc6
 
d84fc6
 /*
d84fc6
@@ -1004,6 +1004,103 @@ done:
d84fc6
 	return efi_status;
d84fc6
 }
d84fc6
 
d84fc6
+static EFI_STATUS
d84fc6
+verify_one_signature(WIN_CERTIFICATE_EFI_PKCS *sig,
d84fc6
+		     UINT8 *sha256hash, UINT8 *sha1hash)
d84fc6
+{
d84fc6
+	EFI_STATUS efi_status;
d84fc6
+
d84fc6
+	/*
d84fc6
+	 * Ensure that the binary isn't blacklisted
d84fc6
+	 */
d84fc6
+	drain_openssl_errors();
d84fc6
+	efi_status = check_blacklist(sig, sha256hash, sha1hash);
d84fc6
+	if (EFI_ERROR(efi_status)) {
d84fc6
+		perror(L"Binary is blacklisted: %r\n", efi_status);
d84fc6
+		PrintErrors();
d84fc6
+		ClearErrors();
d84fc6
+		crypterr(efi_status);
d84fc6
+		return efi_status;
d84fc6
+	}
d84fc6
+
d84fc6
+	/*
d84fc6
+	 * Check whether the binary is whitelisted in any of the firmware
d84fc6
+	 * databases
d84fc6
+	 */
d84fc6
+	drain_openssl_errors();
d84fc6
+	efi_status = check_whitelist(sig, sha256hash, sha1hash);
d84fc6
+	if (EFI_ERROR(efi_status)) {
d84fc6
+		if (efi_status != EFI_NOT_FOUND) {
d84fc6
+			dprint(L"check_whitelist(): %r\n", efi_status);
d84fc6
+			PrintErrors();
d84fc6
+			ClearErrors();
d84fc6
+			crypterr(efi_status);
d84fc6
+		}
d84fc6
+	} else {
d84fc6
+		drain_openssl_errors();
d84fc6
+		return efi_status;
d84fc6
+	}
d84fc6
+
d84fc6
+	efi_status = EFI_NOT_FOUND;
d84fc6
+#if defined(ENABLE_SHIM_CERT)
d84fc6
+	/*
d84fc6
+	 * Check against the shim build key
d84fc6
+	 */
d84fc6
+	drain_openssl_errors();
d84fc6
+	if (build_cert && build_cert_size) {
d84fc6
+		dprint("verifying against shim cert\n");
d84fc6
+	}
d84fc6
+	if (build_cert && build_cert_size &&
d84fc6
+	    AuthenticodeVerify(sig->CertData,
d84fc6
+		       sig->Hdr.dwLength - sizeof(sig->Hdr),
d84fc6
+		       build_cert, build_cert_size, sha256hash,
d84fc6
+		       SHA256_DIGEST_SIZE)) {
d84fc6
+		dprint(L"AuthenticodeVerify(shim_cert) succeeded\n");
d84fc6
+		update_verification_method(VERIFIED_BY_CERT);
d84fc6
+		tpm_measure_variable(L"Shim", SHIM_LOCK_GUID,
d84fc6
+				     build_cert_size, build_cert);
d84fc6
+		efi_status = EFI_SUCCESS;
d84fc6
+		drain_openssl_errors();
d84fc6
+		return efi_status;
d84fc6
+	} else {
d84fc6
+		dprint(L"AuthenticodeVerify(shim_cert) failed\n");
d84fc6
+		PrintErrors();
d84fc6
+		ClearErrors();
d84fc6
+		crypterr(EFI_NOT_FOUND);
d84fc6
+	}
d84fc6
+#endif /* defined(ENABLE_SHIM_CERT) */
d84fc6
+
d84fc6
+#if defined(VENDOR_CERT_FILE)
d84fc6
+	/*
d84fc6
+	 * And finally, check against shim's built-in key
d84fc6
+	 */
d84fc6
+	drain_openssl_errors();
d84fc6
+	if (vendor_cert_size) {
d84fc6
+		dprint("verifying against vendor_cert\n");
d84fc6
+	}
d84fc6
+	if (vendor_cert_size &&
d84fc6
+	    AuthenticodeVerify(sig->CertData,
d84fc6
+			       sig->Hdr.dwLength - sizeof(sig->Hdr),
d84fc6
+			       vendor_cert, vendor_cert_size,
d84fc6
+			       sha256hash, SHA256_DIGEST_SIZE)) {
d84fc6
+		dprint(L"AuthenticodeVerify(vendor_cert) succeeded\n");
d84fc6
+		update_verification_method(VERIFIED_BY_CERT);
d84fc6
+		tpm_measure_variable(L"Shim", SHIM_LOCK_GUID,
d84fc6
+				     vendor_cert_size, vendor_cert);
d84fc6
+		efi_status = EFI_SUCCESS;
d84fc6
+		drain_openssl_errors();
d84fc6
+		return efi_status;
d84fc6
+	} else {
d84fc6
+		dprint(L"AuthenticodeVerify(vendor_cert) failed\n");
d84fc6
+		PrintErrors();
d84fc6
+		ClearErrors();
d84fc6
+		crypterr(EFI_NOT_FOUND);
d84fc6
+	}
d84fc6
+#endif /* defined(VENDOR_CERT_FILE) */
d84fc6
+
d84fc6
+	return efi_status;
d84fc6
+}
d84fc6
+
d84fc6
 /*
d84fc6
  * Check that the signature is valid and matches the binary
d84fc6
  */
d84fc6
@@ -1011,40 +1108,14 @@ static EFI_STATUS verify_buffer (char *data, int datasize,
d84fc6
 				 PE_COFF_LOADER_IMAGE_CONTEXT *context,
d84fc6
 				 UINT8 *sha256hash, UINT8 *sha1hash)
d84fc6
 {
d84fc6
-	EFI_STATUS efi_status = EFI_SECURITY_VIOLATION;
d84fc6
-	WIN_CERTIFICATE_EFI_PKCS *cert = NULL;
d84fc6
-	unsigned int size = datasize;
d84fc6
+	EFI_STATUS ret_efi_status;
d84fc6
+	size_t size = datasize;
d84fc6
+	size_t offset = 0;
d84fc6
+	unsigned int i = 0;
d84fc6
 
d84fc6
 	if (datasize < 0)
d84fc6
 		return EFI_INVALID_PARAMETER;
d84fc6
 
d84fc6
-	if (context->SecDir->Size != 0) {
d84fc6
-		if (context->SecDir->Size >= size) {
d84fc6
-			perror(L"Certificate Database size is too large\n");
d84fc6
-			return EFI_INVALID_PARAMETER;
d84fc6
-		}
d84fc6
-
d84fc6
-		cert = ImageAddress (data, size,
d84fc6
-				     context->SecDir->VirtualAddress);
d84fc6
-
d84fc6
-		if (!cert) {
d84fc6
-			perror(L"Certificate located outside the image\n");
d84fc6
-			return EFI_INVALID_PARAMETER;
d84fc6
-		}
d84fc6
-
d84fc6
-		if (cert->Hdr.dwLength > context->SecDir->Size) {
d84fc6
-			perror(L"Certificate list size is inconsistent with PE headers");
d84fc6
-			return EFI_INVALID_PARAMETER;
d84fc6
-		}
d84fc6
-
d84fc6
-		if (cert->Hdr.wCertificateType !=
d84fc6
-		    WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
d84fc6
-			perror(L"Unsupported certificate type %x\n",
d84fc6
-				cert->Hdr.wCertificateType);
d84fc6
-			return EFI_UNSUPPORTED;
d84fc6
-		}
d84fc6
-	}
d84fc6
-
d84fc6
 	/*
d84fc6
 	 * Clear OpenSSL's error log, because we get some DSO unimplemented
d84fc6
 	 * errors during its intialization, and we don't want those to look
d84fc6
@@ -1052,81 +1123,119 @@ static EFI_STATUS verify_buffer (char *data, int datasize,
d84fc6
 	 */
d84fc6
 	drain_openssl_errors();
d84fc6
 
d84fc6
-	efi_status = generate_hash(data, datasize, context, sha256hash, sha1hash);
d84fc6
-	if (EFI_ERROR(efi_status)) {
d84fc6
-		LogError(L"generate_hash: %r\n", efi_status);
d84fc6
-		return efi_status;
d84fc6
+	ret_efi_status = generate_hash(data, datasize, context, sha256hash, sha1hash);
d84fc6
+	if (EFI_ERROR(ret_efi_status)) {
d84fc6
+		dprint(L"generate_hash: %r\n", ret_efi_status);
d84fc6
+		PrintErrors();
d84fc6
+		ClearErrors();
d84fc6
+		crypterr(ret_efi_status);
d84fc6
+		return ret_efi_status;
d84fc6
 	}
d84fc6
 
d84fc6
 	/*
d84fc6
-	 * Ensure that the binary isn't blacklisted
d84fc6
+	 * Ensure that the binary isn't blacklisted by hash
d84fc6
 	 */
d84fc6
-	efi_status = check_blacklist(cert, sha256hash, sha1hash);
d84fc6
-	if (EFI_ERROR(efi_status)) {
d84fc6
+	drain_openssl_errors();
d84fc6
+	ret_efi_status = check_blacklist(NULL, sha256hash, sha1hash);
d84fc6
+	if (EFI_ERROR(ret_efi_status)) {
d84fc6
 		perror(L"Binary is blacklisted\n");
d84fc6
-		LogError(L"Binary is blacklisted: %r\n", efi_status);
d84fc6
-		return efi_status;
d84fc6
+		dprint(L"Binary is blacklisted: %r\n", ret_efi_status);
d84fc6
+		PrintErrors();
d84fc6
+		ClearErrors();
d84fc6
+		crypterr(ret_efi_status);
d84fc6
+		return ret_efi_status;
d84fc6
 	}
d84fc6
 
d84fc6
 	/*
d84fc6
-	 * Check whether the binary is whitelisted in any of the firmware
d84fc6
-	 * databases
d84fc6
+	 * Check whether the binary is whitelisted by hash in any of the
d84fc6
+	 * firmware databases
d84fc6
 	 */
d84fc6
-	efi_status = check_whitelist(cert, sha256hash, sha1hash);
d84fc6
-	if (EFI_ERROR(efi_status)) {
d84fc6
-		LogError(L"check_whitelist(): %r\n", efi_status);
d84fc6
+	drain_openssl_errors();
d84fc6
+	ret_efi_status = check_whitelist(NULL, sha256hash, sha1hash);
d84fc6
+	if (EFI_ERROR(ret_efi_status)) {
d84fc6
+		dprint(L"check_whitelist: %r\n", ret_efi_status);
d84fc6
+		if (ret_efi_status != EFI_NOT_FOUND) {
d84fc6
+			PrintErrors();
d84fc6
+			ClearErrors();
d84fc6
+			crypterr(ret_efi_status);
d84fc6
+			return ret_efi_status;
d84fc6
+		}
d84fc6
 	} else {
d84fc6
 		drain_openssl_errors();
d84fc6
-		return efi_status;
d84fc6
+		return ret_efi_status;
d84fc6
 	}
d84fc6
 
d84fc6
-	if (cert) {
d84fc6
-#if defined(ENABLE_SHIM_CERT)
d84fc6
-		/*
d84fc6
-		 * Check against the shim build key
d84fc6
-		 */
d84fc6
-		if (sizeof(shim_cert) &&
d84fc6
-		    AuthenticodeVerify(cert->CertData,
d84fc6
-			       cert->Hdr.dwLength - sizeof(cert->Hdr),
d84fc6
-			       shim_cert, sizeof(shim_cert), sha256hash,
d84fc6
-			       SHA256_DIGEST_SIZE)) {
d84fc6
-			update_verification_method(VERIFIED_BY_CERT);
d84fc6
-			tpm_measure_variable(L"Shim", SHIM_LOCK_GUID,
d84fc6
-					     sizeof(shim_cert), shim_cert);
d84fc6
-			efi_status = EFI_SUCCESS;
d84fc6
-			drain_openssl_errors();
d84fc6
-			return efi_status;
d84fc6
-		} else {
d84fc6
-			LogError(L"AuthenticodeVerify(shim_cert) failed\n");
d84fc6
+	if (context->SecDir->Size == 0) {
d84fc6
+		dprint(L"No signatures found\n");
d84fc6
+		return EFI_SECURITY_VIOLATION;
d84fc6
+	}
d84fc6
+
d84fc6
+	if (context->SecDir->Size >= size) {
d84fc6
+		perror(L"Certificate Database size is too large\n");
d84fc6
+		return EFI_INVALID_PARAMETER;
d84fc6
+	}
d84fc6
+
d84fc6
+	ret_efi_status = EFI_NOT_FOUND;
d84fc6
+	do {
d84fc6
+		WIN_CERTIFICATE_EFI_PKCS *sig = NULL;
d84fc6
+		size_t sz;
d84fc6
+
d84fc6
+		sig = ImageAddress(data, size,
d84fc6
+				   context->SecDir->VirtualAddress + offset);
d84fc6
+		if (!sig)
d84fc6
+			break;
d84fc6
+
d84fc6
+		sz = offset + offsetof(WIN_CERTIFICATE_EFI_PKCS, Hdr.dwLength)
d84fc6
+		     + sizeof(sig->Hdr.dwLength);
d84fc6
+		if (sz > context->SecDir->Size) {
d84fc6
+			perror(L"Certificate size is too large for secruity database");
d84fc6
+			return EFI_INVALID_PARAMETER;
d84fc6
+		}
d84fc6
+
d84fc6
+		sz = sig->Hdr.dwLength;
d84fc6
+		if (sz > context->SecDir->Size - offset) {
d84fc6
+			perror(L"Certificate size is too large for secruity database");
d84fc6
+			return EFI_INVALID_PARAMETER;
d84fc6
 		}
d84fc6
-#endif /* defined(ENABLE_SHIM_CERT) */
d84fc6
-
d84fc6
-#if defined(VENDOR_CERT_FILE)
d84fc6
-		/*
d84fc6
-		 * And finally, check against shim's built-in key
d84fc6
-		 */
d84fc6
-		if (vendor_authorized_size &&
d84fc6
-		    AuthenticodeVerify(cert->CertData,
d84fc6
-				       cert->Hdr.dwLength - sizeof(cert->Hdr),
d84fc6
-				       vendor_authorized, vendor_authorized_size,
d84fc6
-				       sha256hash, SHA256_DIGEST_SIZE)) {
d84fc6
-			update_verification_method(VERIFIED_BY_CERT);
d84fc6
-			tpm_measure_variable(L"Shim", SHIM_LOCK_GUID,
d84fc6
-					     vendor_authorized_size, vendor_authorized);
d84fc6
-			efi_status = EFI_SUCCESS;
d84fc6
-			drain_openssl_errors();
d84fc6
-			return efi_status;
d84fc6
+
d84fc6
+		if (sz < sizeof(sig->Hdr)) {
d84fc6
+			perror(L"Certificate size is too small for certificate data");
d84fc6
+			return EFI_INVALID_PARAMETER;
d84fc6
+		}
d84fc6
+
d84fc6
+		if (sig->Hdr.wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
d84fc6
+			EFI_STATUS efi_status;
d84fc6
+
d84fc6
+			dprint(L"Attempting to verify signature %d:\n", i++);
d84fc6
+
d84fc6
+			efi_status = verify_one_signature(sig, sha256hash, sha1hash);
d84fc6
+
d84fc6
+			/*
d84fc6
+			 * If we didn't get EFI_SECURITY_VIOLATION from
d84fc6
+			 * checking the hashes above, then any dbx entries are
d84fc6
+			 * for a certificate, not this individual binary.
d84fc6
+			 *
d84fc6
+			 * So don't clobber successes with security violation
d84fc6
+			 * here; that just means it isn't a success.
d84fc6
+			 */
d84fc6
+			if (ret_efi_status != EFI_SUCCESS)
d84fc6
+				ret_efi_status = efi_status;
d84fc6
 		} else {
d84fc6
-			LogError(L"AuthenticodeVerify(vendor_authorized) failed\n");
d84fc6
+			perror(L"Unsupported certificate type %x\n",
d84fc6
+				sig->Hdr.wCertificateType);
d84fc6
 		}
d84fc6
-#endif /* defined(VENDOR_CERT_FILE) */
d84fc6
-	}
d84fc6
+		offset = ALIGN_VALUE(offset + sz, 8);
d84fc6
+	} while (offset < context->SecDir->Size);
d84fc6
 
d84fc6
-	LogError(L"Binary is not whitelisted\n");
d84fc6
-	crypterr(EFI_SECURITY_VIOLATION);
d84fc6
-	PrintErrors();
d84fc6
-	efi_status = EFI_SECURITY_VIOLATION;
d84fc6
-	return efi_status;
d84fc6
+	if (ret_efi_status != EFI_SUCCESS) {
d84fc6
+		dprint(L"Binary is not whitelisted\n");
d84fc6
+		PrintErrors();
d84fc6
+		ClearErrors();
d84fc6
+		crypterr(EFI_SECURITY_VIOLATION);
d84fc6
+		ret_efi_status = EFI_SECURITY_VIOLATION;
d84fc6
+	}
d84fc6
+	drain_openssl_errors();
d84fc6
+	return ret_efi_status;
d84fc6
 }
d84fc6
 
d84fc6
 /*
d84fc6
-- 
d84fc6
2.26.2
d84fc6