Blame SOURCES/0024-mok-also-mirror-the-build-cert-to-MokListRT.patch

d1e1c8
From 3d62232feb296b238ca5d7963ba40a2c346767e7 Mon Sep 17 00:00:00 2001
d1e1c8
From: Gary Lin <glin@suse.com>
d1e1c8
Date: Wed, 19 Dec 2018 12:40:02 +0800
d1e1c8
Subject: [PATCH 24/62] mok: also mirror the build cert to MokListRT
d1e1c8
d1e1c8
If the build cert is enabled, we should also mirror it to MokListRT.
d1e1c8
d1e1c8
Signed-off-by: Gary Lin <glin@suse.com>
d1e1c8
Upstream-commit-id: aecbe1f99b6
d1e1c8
---
d1e1c8
 mok.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
d1e1c8
 1 file changed, 72 insertions(+), 6 deletions(-)
d1e1c8
d1e1c8
diff --git a/mok.c b/mok.c
d1e1c8
index 2b9d796a0e8..6150d8c8868 100644
d1e1c8
--- a/mok.c
d1e1c8
+++ b/mok.c
d1e1c8
@@ -68,6 +68,10 @@ struct mok_state_variable {
d1e1c8
 	 */
d1e1c8
 	UINT8 **addend_source;
d1e1c8
 	UINT32 *addend_size;
d1e1c8
+#if defined(ENABLE_SHIM_CERT)
d1e1c8
+	UINT8 **build_cert;
d1e1c8
+	UINT32 *build_cert_size;
d1e1c8
+#endif /* defined(ENABLE_SHIM_CERT) */
d1e1c8
 	UINT32 yes_attr;
d1e1c8
 	UINT32 no_attr;
d1e1c8
 	UINT32 flags;
d1e1c8
@@ -90,6 +94,10 @@ struct mok_state_variable mok_state_variables[] = {
d1e1c8
 	 .no_attr = EFI_VARIABLE_RUNTIME_ACCESS,
d1e1c8
 	 .addend_source = &vendor_cert,
d1e1c8
 	 .addend_size = &vendor_cert_size,
d1e1c8
+#if defined(ENABLE_SHIM_CERT)
d1e1c8
+	 .build_cert = &build_cert,
d1e1c8
+	 .build_cert_size = &build_cert_size,
d1e1c8
+#endif /* defined(ENABLE_SHIM_CERT) */
d1e1c8
 	 .flags = MOK_MIRROR_KEYDB |
d1e1c8
 		  MOK_VARIABLE_LOG,
d1e1c8
 	 .pcr = 14,
d1e1c8
@@ -130,6 +138,22 @@ struct mok_state_variable mok_state_variables[] = {
d1e1c8
 	{ NULL, }
d1e1c8
 };
d1e1c8
 
d1e1c8
+inline BOOLEAN check_vendor_cert(struct mok_state_variable *v)
d1e1c8
+{
d1e1c8
+	return (v->addend_source && v->addend_size &&
d1e1c8
+		*v->addend_source && *v->addend_size) ? TRUE : FALSE;
d1e1c8
+}
d1e1c8
+#if defined(ENABLE_SHIM_CERT)
d1e1c8
+inline BOOLEAN check_build_cert(struct mok_state_variable *v)
d1e1c8
+{
d1e1c8
+	return (v->build_cert && v->build_cert_size &&
d1e1c8
+		*v->build_cert && *v->build_cert_size) ? TRUE : FALSE;
d1e1c8
+}
d1e1c8
+#define check_addend(v) (check_vendor_cert(v) || check_build_cert(v))
d1e1c8
+#else
d1e1c8
+#define check_addend(v) check_vendor_cert(v)
d1e1c8
+#endif /* defined(ENABLE_SHIM_CERT) */
d1e1c8
+
d1e1c8
 static EFI_STATUS nonnull(1)
d1e1c8
 mirror_one_mok_variable(struct mok_state_variable *v)
d1e1c8
 {
d1e1c8
@@ -138,15 +162,27 @@ mirror_one_mok_variable(struct mok_state_variable *v)
d1e1c8
 	UINTN FullDataSize = 0;
d1e1c8
 	uint8_t *p = NULL;
d1e1c8
 
d1e1c8
-	if ((v->flags & MOK_MIRROR_KEYDB) &&
d1e1c8
-	    v->addend_source && *v->addend_source &&
d1e1c8
-	    v->addend_size && *v->addend_size) {
d1e1c8
+	if ((v->flags & MOK_MIRROR_KEYDB) && check_addend(v)) {
d1e1c8
 		EFI_SIGNATURE_LIST *CertList = NULL;
d1e1c8
 		EFI_SIGNATURE_DATA *CertData = NULL;
d1e1c8
+#if defined(ENABLE_SHIM_CERT)
d1e1c8
+		FullDataSize = v->data_size;
d1e1c8
+		if (check_build_cert(v)) {
d1e1c8
+			FullDataSize += sizeof (*CertList)
d1e1c8
+					+ sizeof (EFI_GUID)
d1e1c8
+					+ *v->build_cert_size;
d1e1c8
+		}
d1e1c8
+		if (check_vendor_cert(v)) {
d1e1c8
+			FullDataSize += sizeof (*CertList)
d1e1c8
+					+ sizeof (EFI_GUID)
d1e1c8
+					+ *v->addend_size;
d1e1c8
+		}
d1e1c8
+#else
d1e1c8
 		FullDataSize = v->data_size
d1e1c8
 			     + sizeof (*CertList)
d1e1c8
 			     + sizeof (EFI_GUID)
d1e1c8
 			     + *v->addend_size;
d1e1c8
+#endif /* defined(ENABLE_SHIM_CERT) */
d1e1c8
 		FullData = AllocatePool(FullDataSize);
d1e1c8
 		if (!FullData) {
d1e1c8
 			perror(L"Failed to allocate space for MokListRT\n");
d1e1c8
@@ -158,6 +194,35 @@ mirror_one_mok_variable(struct mok_state_variable *v)
d1e1c8
 			CopyMem(p, v->data, v->data_size);
d1e1c8
 			p += v->data_size;
d1e1c8
 		}
d1e1c8
+
d1e1c8
+#if defined(ENABLE_SHIM_CERT)
d1e1c8
+		if (check_build_cert(v) == FALSE)
d1e1c8
+			goto skip_build_cert;
d1e1c8
+
d1e1c8
+		CertList = (EFI_SIGNATURE_LIST *)p;
d1e1c8
+		p += sizeof (*CertList);
d1e1c8
+		CertData = (EFI_SIGNATURE_DATA *)p;
d1e1c8
+		p += sizeof (EFI_GUID);
d1e1c8
+
d1e1c8
+		CertList->SignatureType = EFI_CERT_TYPE_X509_GUID;
d1e1c8
+		CertList->SignatureListSize = *v->build_cert_size
d1e1c8
+					      + sizeof (*CertList)
d1e1c8
+					      + sizeof (*CertData)
d1e1c8
+					      -1;
d1e1c8
+		CertList->SignatureHeaderSize = 0;
d1e1c8
+		CertList->SignatureSize = *v->build_cert_size +
d1e1c8
+					  sizeof (EFI_GUID);
d1e1c8
+
d1e1c8
+		CertData->SignatureOwner = SHIM_LOCK_GUID;
d1e1c8
+		CopyMem(p, *v->build_cert, *v->build_cert_size);
d1e1c8
+
d1e1c8
+		p += *v->build_cert_size;
d1e1c8
+
d1e1c8
+		if (check_vendor_cert(v) == FALSE)
d1e1c8
+			goto skip_vendor_cert;
d1e1c8
+skip_build_cert:
d1e1c8
+#endif /* defined(ENABLE_SHIM_CERT) */
d1e1c8
+
d1e1c8
 		CertList = (EFI_SIGNATURE_LIST *)p;
d1e1c8
 		p += sizeof (*CertList);
d1e1c8
 		CertData = (EFI_SIGNATURE_DATA *)p;
d1e1c8
@@ -174,6 +239,9 @@ mirror_one_mok_variable(struct mok_state_variable *v)
d1e1c8
 		CertData->SignatureOwner = SHIM_LOCK_GUID;
d1e1c8
 		CopyMem(p, *v->addend_source, *v->addend_size);
d1e1c8
 
d1e1c8
+#if defined(ENABLE_SHIM_CERT)
d1e1c8
+skip_vendor_cert:
d1e1c8
+#endif /* defined(ENABLE_SHIM_CERT) */
d1e1c8
 		if (v->data && v->data_size)
d1e1c8
 			FreePool(v->data);
d1e1c8
 		v->data = FullData;
d1e1c8
@@ -247,9 +315,7 @@ EFI_STATUS import_mok_state(EFI_HANDLE image_handle)
d1e1c8
 		UINT32 attrs = 0;
d1e1c8
 		BOOLEAN delete = FALSE, present, addend;
d1e1c8
 
d1e1c8
-		addend = (v->addend_source && v->addend_size &&
d1e1c8
-			  *v->addend_source && *v->addend_size)
d1e1c8
-			? TRUE : FALSE;
d1e1c8
+		addend = check_addend(v);
d1e1c8
 
d1e1c8
 		efi_status = get_variable_attr(v->name,
d1e1c8
 					       &v->data, &v->data_size,
d1e1c8
-- 
d1e1c8
2.26.2
d1e1c8