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

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