Skip to contentSkip to conversation

efi_loader: EFI TCG2 free efi memory on protocol failure

PATCH New Ilias Apalodimas 2 participants 3 messages Mar 24, 2021 – Mar 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi_loader: EFI TCG2 free efi memory on protocol failure

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")
---
 lib/efi_loader/efi_tcg2.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
lib/efi_loader/efi_tcg2.c +17 -4
index 797d6eb134f6..02de63808f9a 100644
@@ -988,6 +988,7 @@ static efi_status_t create_final_event(void)
 
 	return EFI_SUCCESS;
 out:
+	efi_free_pool(event_log.final_buffer);
 	return ret;
 }
 
@@ -1041,8 +1042,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:
+	efi_free_pool(event_log.buffer);
 	return ret;
 }
 
@@ -1055,23 +1060,31 @@ 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:
+	efi_free_pool(event_log.final_buffer);
+	efi_free_pool(event_log.buffer);
 	return ret;
 }
Signature · 1 line
2.31.0
#1 lore raw

PATCH 1/1 efi_loader: EFI TCG2 free efi memory on protocol failure

Quoted 22 lines · Ilias Apalodimas

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")
---
 lib/efi_loader/efi_tcg2.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
lib/efi_loader/efi_tcg2.c +1 -0
index 797d6eb134f6..02de63808f9a 100644
@@ -988,6 +988,7 @@ static efi_status_t create_final_event(void)
 
 	return EFI_SUCCESS;
 out:
+	efi_free_pool(event_log.final_buffer);
 	return ret;
 }
 

lib/efi_loader/efi_tcg2.c static efi_status_t efi_init_event_log(void) · line 1044

Quoted 3 lines
lib/efi_loader/efi_tcg2.c +0 -0
@@ -1041,8 +1042,12 @@ static efi_status_t efi_init_event_log(void)
 	event_log.last_event_size = event_log.pos;
 
 	ret = create_final_event();

Shouldn't create_final_event() should free event_log.final_buffer if efi_install_configuration_table() fails?

lib/efi_loader/efi_tcg2.c static efi_status_t efi_init_event_log(void) · line 1050

Quoted 6 lines
lib/efi_loader/efi_tcg2.c +4 -0
+	if (ret != EFI_SUCCESS)
+		goto out;
 
+	return EFI_SUCCESS;
 out:
+	efi_free_pool(event_log.buffer);

We must set event_log.buffer to NULL to avoid double free?

Quoted 25 lines
lib/efi_loader/efi_tcg2.c +4 -3
 	return ret;
 }
 
@@ -1055,23 +1060,31 @@ 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,

lib/efi_loader/efi_tcg2.c out: · line 1086

Quoted 10 lines
lib/efi_loader/efi_tcg2.c +6 -1
 			       (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:

In create_final_event() you have installed a configuration table EFI_TCG2_FINAL_EVENTS_TABLE_GUID. You should not free the memory of the configuration table without uninstalling the table.

Should we create separate uninit function which takes care of the logic?

Best regards

Heinrich

Quoted 5 lines
lib/efi_loader/efi_tcg2.c +2 -0
+	efi_free_pool(event_log.final_buffer);
+	efi_free_pool(event_log.buffer);
 	return ret;
 }
#2 lore raw

PATCH 1/1 efi_loader: EFI TCG2 free efi memory on protocol failure

Quoted 22 lines · Heinrich Schuchardt
Quoted 21 lines · Ilias Apalodimas

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")
---
 lib/efi_loader/efi_tcg2.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
lib/efi_loader/efi_tcg2.c +1 -0
index 797d6eb134f6..02de63808f9a 100644
@@ -988,6 +988,7 @@ static efi_status_t create_final_event(void)
 
 	return EFI_SUCCESS;
 out:
+	efi_free_pool(event_log.final_buffer);
 	return ret;
 }
 
Quoted 8 lines

lib/efi_loader/efi_tcg2.c static efi_status_t efi_init_event_log(void) · line 1044

Quoted 4 lines
lib/efi_loader/efi_tcg2.c +0 -0
@@ -1041,8 +1042,12 @@ static efi_status_t efi_init_event_log(void)
 	event_log.last_event_size = event_log.pos;
 
 	ret = create_final_event();

Shouldn't create_final_event() should free event_log.final_buffer if efi_install_configuration_table() fails?

I am not sure I undetstand you here.
create_final_event() frees event_log.final_buffer if installing the config table fails.

Quoted 8 lines

lib/efi_loader/efi_tcg2.c static efi_status_t efi_init_event_log(void) · line 1050

Quoted 6 lines
lib/efi_loader/efi_tcg2.c +4 -0
+	if (ret != EFI_SUCCESS)
+		goto out;
 
+	return EFI_SUCCESS;
 out:
+	efi_free_pool(event_log.buffer);

We must set event_log.buffer to NULL to avoid double free?

Yea sure

Quoted 41 lines
Quoted 26 lines
lib/efi_loader/efi_tcg2.c +4 -3
 	return ret;
 }
 
@@ -1055,23 +1060,31 @@ 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,

lib/efi_loader/efi_tcg2.c out: · line 1086

Quoted 10 lines
lib/efi_loader/efi_tcg2.c +6 -1
 			       (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:

In create_final_event() you have installed a configuration table EFI_TCG2_FINAL_EVENTS_TABLE_GUID. You should not free the memory of the configuration table without uninstalling the table.

Quoted 1 line

Should we create separate uninit function which takes care of the logic?

I guess I could gather all the frees/uninstalles in one function and call that explicitly

Quoted 11 lines

Best regards

Heinrich

Quoted 5 lines
lib/efi_loader/efi_tcg2.c +2 -0
+	efi_free_pool(event_log.final_buffer);
+	efi_free_pool(event_log.buffer);
 	return ret;
 }