[PATCH v2] efi_loader: EFI TCG2 free efi memory on protocol failure
Ilias Apalodimas
ilias.apalodimas at linaro.org
Thu Mar 25 12:02:41 CET 2021
Current code doesn't free the efi allocated memory in case the protocol
failed to install
Fixes: c8d0fd582576 ("efi_loader: Introduce eventlog support for TCG2_PROTOCOL")
Signed-off-by: Ilias Apalodimas <ilias.apalodimas at linaro.org>
---
changes since v1:
- remove the installed config table as well
- gather all cleanups in a single function and call that instead
include/efi_loader.h | 2 ++
lib/efi_loader/efi_boottime.c | 2 +-
lib/efi_loader/efi_tcg2.c | 43 ++++++++++++++++++++++++++++-------
3 files changed, 38 insertions(+), 9 deletions(-)
diff --git a/include/efi_loader.h b/include/efi_loader.h
index 903bf60bc0a3..3a84b3c75597 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -604,6 +604,8 @@ efi_status_t efi_driver_init(void);
int efi_memory_init(void);
/* Adds new or overrides configuration table entry to the system table */
efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table);
+/* Removes a configuration table from the list */
+void efi_remove_configuration_table(int i);
/* Sets up a loaded image */
efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
struct efi_device_path *file_path,
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 41b8949b0424..46de9d80391d 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -1610,7 +1610,7 @@ static efi_status_t EFIAPI efi_locate_handle_ext(
*
* @i: index of the table entry to be removed
*/
-static void efi_remove_configuration_table(int i)
+void efi_remove_configuration_table(int i)
{
struct efi_configuration_table *this = &systab.tables[i];
struct efi_configuration_table *next = &systab.tables[i + 1];
diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c
index 797d6eb134f6..62f91df0159e 100644
--- a/lib/efi_loader/efi_tcg2.c
+++ b/lib/efi_loader/efi_tcg2.c
@@ -957,6 +957,26 @@ out:
return ret;
}
+/**
+ * tcg2_uninit - remove the final event table and free efi memory on failures
+ */
+void tcg2_uninit(void)
+{
+ int i;
+
+ for (i = 0; i < systab.nr_tables; i++) {
+ if (!guidcmp(&efi_guid_final_events, &systab.tables[i].guid)) {
+ efi_remove_configuration_table(i);
+ break;
+ }
+ }
+
+ efi_free_pool(event_log.buffer);
+ event_log.buffer = NULL;
+ efi_free_pool(event_log.final_buffer);
+ event_log.final_buffer = NULL;
+}
+
/**
* create_final_event() - Create the final event and install the config
* defined by the TCG EFI spec
@@ -983,10 +1003,6 @@ static efi_status_t create_final_event(void)
event_log.final_pos = sizeof(*final_event);
ret = efi_install_configuration_table(&efi_guid_final_events,
final_event);
- if (ret != EFI_SUCCESS)
- goto out;
-
- return EFI_SUCCESS;
out:
return ret;
}
@@ -1041,8 +1057,12 @@ static efi_status_t efi_init_event_log(void)
event_log.last_event_size = event_log.pos;
ret = create_final_event();
+ if (ret != EFI_SUCCESS)
+ goto out;
+ return EFI_SUCCESS;
out:
+ tcg2_uninit();
return ret;
}
@@ -1055,23 +1075,30 @@ out:
*/
efi_status_t efi_tcg2_register(void)
{
- efi_status_t ret;
+ efi_status_t ret = EFI_SUCCESS;
struct udevice *dev;
ret = platform_get_tpm2_device(&dev);
if (ret != EFI_SUCCESS) {
log_warning("Unable to find TPMv2 device\n");
- return EFI_SUCCESS;
+ ret = EFI_SUCCESS;
+ goto out;
}
ret = efi_init_event_log();
if (ret != EFI_SUCCESS)
- return ret;
+ goto fail;
ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol,
(void *)&efi_tcg2_protocol);
- if (ret != EFI_SUCCESS)
+ if (ret != EFI_SUCCESS) {
log_err("Cannot install EFI_TCG2_PROTOCOL\n");
+ goto fail;
+ }
+out:
+ return ret;
+fail:
+ tcg2_uninit();
return ret;
}
--
2.31.0
More information about the U-Boot
mailing list