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

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