Blame SOURCES/0064-Fix-some-mokmanager-deletion-paths.patch

6a35ff
From 18843127dc0eace16d43d479bd091e221e8785c4 Mon Sep 17 00:00:00 2001
6a35ff
From: Peter Jones <pjones@redhat.com>
6a35ff
Date: Mon, 17 Aug 2020 15:47:19 -0400
6a35ff
Subject: [PATCH] Fix some mokmanager deletion paths
6a35ff
6a35ff
This fixes several codepaths where MokList and MokListX are supposed to
6a35ff
be deleted, but are not.  It also adds debug logging to much of the
6a35ff
deletion codepath.
6a35ff
6a35ff
---
6a35ff
 MokManager.c | 24 +++++++++++++++++++++++-
6a35ff
 Makefile     |  2 +-
6a35ff
 2 files changed, 24 insertions(+), 2 deletions(-)
6a35ff
6a35ff
diff --git a/MokManager.c b/MokManager.c
6a35ff
index c9949e33bcf..9bae3414fe7 100644
6a35ff
--- a/MokManager.c
6a35ff
+++ b/MokManager.c
6a35ff
@@ -9,6 +9,8 @@
6a35ff
 
6a35ff
 #include "shim.h"
6a35ff
 
6a35ff
+#include "hexdump.h"
6a35ff
+
6a35ff
 #define PASSWORD_MAX 256
6a35ff
 #define PASSWORD_MIN 1
6a35ff
 #define SB_PASSWORD_LEN 16
6a35ff
@@ -1050,9 +1052,11 @@ static EFI_STATUS mok_reset_prompt(BOOLEAN MokX)
6a35ff
 	if (MokX) {
6a35ff
 		LibDeleteVariable(L"MokXNew", &SHIM_LOCK_GUID);
6a35ff
 		LibDeleteVariable(L"MokXAuth", &SHIM_LOCK_GUID);
6a35ff
+		LibDeleteVariable(L"MokListX", &SHIM_LOCK_GUID);
6a35ff
 	} else {
6a35ff
 		LibDeleteVariable(L"MokNew", &SHIM_LOCK_GUID);
6a35ff
 		LibDeleteVariable(L"MokAuth", &SHIM_LOCK_GUID);
6a35ff
+		LibDeleteVariable(L"MokList", &SHIM_LOCK_GUID);
6a35ff
 	}
6a35ff
 
6a35ff
 	return EFI_SUCCESS;
6a35ff
@@ -1075,6 +1079,7 @@ static EFI_STATUS write_back_mok_list(MokListNode * list, INTN key_num,
6a35ff
 	else
6a35ff
 		db_name = L"MokList";
6a35ff
 
6a35ff
+	dprint(L"Writing back %s (%d entries)\n", db_name, key_num);
6a35ff
 	for (i = 0; i < key_num; i++) {
6a35ff
 		if (list[i].Mok == NULL)
6a35ff
 			continue;
6a35ff
@@ -1085,8 +1090,15 @@ static EFI_STATUS write_back_mok_list(MokListNode * list, INTN key_num,
6a35ff
 			DataSize += sizeof(EFI_GUID);
6a35ff
 		DataSize += list[i].MokSize;
6a35ff
 	}
6a35ff
-	if (DataSize == 0)
6a35ff
+	if (DataSize == 0) {
6a35ff
+		dprint(L"DataSize = 0; deleting variable %s\n", db_name);
6a35ff
+		efi_status = gRT->SetVariable(db_name, &SHIM_LOCK_GUID,
6a35ff
+					      EFI_VARIABLE_NON_VOLATILE |
6a35ff
+					      EFI_VARIABLE_BOOTSERVICE_ACCESS,
6a35ff
+					      DataSize, Data);
6a35ff
+		dprint(L"efi_status:%llu\n", efi_status);
6a35ff
 		return EFI_SUCCESS;
6a35ff
+	}
6a35ff
 
6a35ff
 	Data = AllocatePool(DataSize);
6a35ff
 	if (Data == NULL)
6a35ff
@@ -1291,11 +1303,15 @@ static EFI_STATUS delete_keys(void *MokDel, UINTN MokDelSize, BOOLEAN MokX)
6a35ff
 	}
6a35ff
 
6a35ff
 	if (auth_size == PASSWORD_CRYPT_SIZE) {
6a35ff
+		dprint(L"matching password with CRYPT");
6a35ff
 		efi_status = match_password((PASSWORD_CRYPT *) auth, NULL, 0,
6a35ff
 					    NULL, NULL);
6a35ff
+		dprint(L"match_password(0x%llx, NULL, 0, NULL, NULL) = %lu\n", auth, efi_status);
6a35ff
 	} else {
6a35ff
+		dprint(L"matching password as sha256sum");
6a35ff
 		efi_status =
6a35ff
 		    match_password(NULL, MokDel, MokDelSize, auth, NULL);
6a35ff
+		dprint(L"match_password(NULL, 0x%llx, %llu, 0x%llx, NULL) = %lu\n", MokDel, MokDelSize, auth, efi_status);
6a35ff
 	}
6a35ff
 	if (EFI_ERROR(efi_status))
6a35ff
 		return EFI_ACCESS_DENIED;
6a35ff
@@ -1365,12 +1381,17 @@ static EFI_STATUS delete_keys(void *MokDel, UINTN MokDelSize, BOOLEAN MokX)
6a35ff
 	}
6a35ff
 
6a35ff
 	/* Search and destroy */
6a35ff
+	dprint(L"deleting certs from %a\n", MokX ? "MokListX" : "MokList");
6a35ff
 	for (i = 0; i < del_num; i++) {
6a35ff
 		type = del_key[i].Type; /* avoid -Werror=address-of-packed-member */
6a35ff
 		if (CompareGuid(&type, &X509_GUID) == 0) {
6a35ff
+			dprint(L"deleting key %d (total %d):\n", i, mok_num);
6a35ff
+			dhexdumpat(del_key[i].Mok, del_key[i].MokSize, 0);
6a35ff
 			delete_cert(del_key[i].Mok, del_key[i].MokSize,
6a35ff
 				    mok, mok_num);
6a35ff
 		} else if (is_sha2_hash(del_key[i].Type)) {
6a35ff
+			dprint(L"deleting hash %d (total %d):\n", i, mok_num);
6a35ff
+			dhexdumpat(del_key[i].Mok, del_key[i].MokSize, 0);
6a35ff
 			delete_hash_list(del_key[i].Type, del_key[i].Mok,
6a35ff
 					 del_key[i].MokSize, mok, mok_num);
6a35ff
 		}
6a35ff
@@ -2564,6 +2585,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE * systab)
6a35ff
 
6a35ff
 	InitializeLib(image_handle, systab);
6a35ff
 
6a35ff
+	setup_verbosity();
6a35ff
 	setup_rand();
6a35ff
 
6a35ff
 	console_mode_handle();
6a35ff
diff --git a/Makefile b/Makefile
6a35ff
index 49e14a26521..a17fa2bef14 100644
6a35ff
--- a/Makefile
6a35ff
+++ b/Makefile
6a35ff
@@ -36,7 +36,7 @@ endif
6a35ff
 OBJS	= shim.o mok.o netboot.o cert.o replacements.o tpm.o version.o errlog.o
6a35ff
 KEYS	= shim_cert.h ocsp.* ca.* shim.crt shim.csr shim.p12 shim.pem shim.key shim.cer
6a35ff
 ORIG_SOURCES	= shim.c mok.c netboot.c replacements.c tpm.c errlog.c shim.h version.h $(wildcard include/*.h)
6a35ff
-MOK_OBJS = MokManager.o PasswordCrypt.o crypt_blowfish.o
6a35ff
+MOK_OBJS = MokManager.o PasswordCrypt.o crypt_blowfish.o errlog.o
6a35ff
 ORIG_MOK_SOURCES = MokManager.c PasswordCrypt.c crypt_blowfish.c shim.h $(wildcard include/*.h)
6a35ff
 FALLBACK_OBJS = fallback.o tpm.o errlog.o
6a35ff
 ORIG_FALLBACK_SRCS = fallback.c
6a35ff
-- 
6a35ff
2.26.2
6a35ff