From 44f55e2ee6df0b317df3029675afce86219cd9cd Mon Sep 17 00:00:00 2001 From: Ivan Hu Date: Fri, 17 Jan 2020 15:50:28 +0800 Subject: [PATCH] Correctly delete UEFI variables UEFI runtime service GetVariable with DataSize NULL, will fail and get EFI_INVALID_PARAMETER returned. Set DataSize 0 and allocate the buffer for getting attributes for the deleted variable. Also, fix the real reason Boot#### was never found. --- plugins/uefi/efi/fwup-efi.c | 19 +++++++++++++++---- plugins/uefi/efi/fwupdate.c | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/plugins/uefi/efi/fwup-efi.c b/plugins/uefi/efi/fwup-efi.c index 8da6b2021..db864e10e 100644 --- a/plugins/uefi/efi/fwup-efi.c +++ b/plugins/uefi/efi/fwup-efi.c @@ -17,16 +17,27 @@ fwup_delete_variable(CHAR16 *name, EFI_GUID *guid) { EFI_STATUS rc; UINT32 attrs = 0; + UINTN size = 0; /* get the attrs so we can delete it */ - rc = uefi_call_wrapper(RT->GetVariable, 5, name, guid, &attrs, NULL, NULL); + rc = uefi_call_wrapper(RT->GetVariable, 5, name, guid, &attrs, &size, NULL); if (EFI_ERROR(rc)) { - if (rc == EFI_NOT_FOUND) { + if (rc == EFI_BUFFER_TOO_SMALL) { + _cleanup_free VOID *buf = fwup_malloc(size); + if (buf == NULL) + return EFI_OUT_OF_RESOURCES; + rc = uefi_call_wrapper(RT->GetVariable, 5, name, guid, &attrs, &size, buf); + if (EFI_ERROR(rc)) { + fwup_debug(L"Could not get attr: %r", rc); + return rc; + } + } else if (rc == EFI_NOT_FOUND) { fwup_debug(L"Not deleting variable '%s' as not found", name); return EFI_SUCCESS; + } else { + fwup_debug(L"Could not get variable '%s' for delete: %r", name, rc); + return rc; } - fwup_debug(L"Could not get variable '%s' for delete: %r", name, rc); - return rc; } return uefi_call_wrapper(RT->SetVariable, 5, name, guid, attrs, 0, NULL); } diff --git a/plugins/uefi/efi/fwupdate.c b/plugins/uefi/efi/fwupdate.c index f4c740a3d..abacc10bb 100644 --- a/plugins/uefi/efi/fwupdate.c +++ b/plugins/uefi/efi/fwupdate.c @@ -408,7 +408,9 @@ fwup_delete_boot_entry(VOID) /* check if the variable name is Boot#### */ if (CompareGuid(&vendor_guid, &global_variable_guid) != 0) continue; - if (StrCmp(variable_name, L"Boot") != 0) + if (StrLen(variable_name) != 8) + continue; + if (StrnCmp(variable_name, L"Boot", 4) != 0) continue; UINTN info_size = 0;