Skip to contentSkip to conversation

efi_loader: fix use after free in efi_exit() with tcg2

PATCH Superseded Vincent Stehlé 4 participants 7 messages Jan 23 – Feb 6

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi_loader: fix use after free in efi_exit() with tcg2

The efi_exit() function frees the loaded image memory by calling efi_delete_image(). However, when CONFIG_EFI_TCG2_PROTOCOL is enabled, the image_obj->image_type structure member is accessed after the memory has been freed.

Fix this by keeping a copy of image_type, as is already done for exit_jmp.

Fixes 8fc4e0b4273a ("efi_loader: add boot variable measurement")
Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Tom Rini <[email protected]>
Cc Masahisa Kojima <[email protected]>

---

Hi,

This can be verified with sandbox_defconfig + CONFIG_VALGRIND=y and the following command:

  valgrind --suppressions=scripts/u-boot.supp \
    ./u-boot -T -c "setenv efi_selftest start image return; \
                    bootefi selftest"

Best regards,
Vincent.

 lib/efi_loader/efi_boottime.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/efi_loader/efi_boottime.c +4 -2
index ddc935d2240..0b3f2fd276c 100644
@@ -3453,6 +3453,7 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
 	struct efi_loaded_image_obj *image_obj =
 		(struct efi_loaded_image_obj *)image_handle;
 	jmp_buf *exit_jmp;
+	u16 image_type;
 
 	EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
 		  exit_data_size, exit_data);
@@ -3496,13 +3497,14 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
 	}
 	/* efi_delete_image() frees image_obj. Copy before the call. */
 	exit_jmp = image_obj->exit_jmp;
+	image_type = image_obj->image_type;
 	*image_obj->exit_status = exit_status;
-	if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
+	if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
 	    exit_status != EFI_SUCCESS)
 		efi_delete_image(image_obj, loaded_image_protocol);
 
 	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
-		if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
+		if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
 			ret = efi_tcg2_measure_efi_app_exit();
 			if (ret != EFI_SUCCESS)
 				log_debug("tcg2 measurement fails (0x%lx)\n",
Signature · 1 line
2.51.0
#1 lore raw

Hi Vincent,

Quoted 47 lines · Vincent Stehlé

The efi_exit() function frees the loaded image memory by calling efi_delete_image(). However, when CONFIG_EFI_TCG2_PROTOCOL is enabled, the image_obj->image_type structure member is accessed after the memory has been freed.

Fix this by keeping a copy of image_type, as is already done for exit_jmp.

Fixes 8fc4e0b4273a ("efi_loader: add boot variable measurement")
Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Tom Rini <[email protected]>
Cc Masahisa Kojima <[email protected]>

---

Hi,

This can be verified with sandbox_defconfig + CONFIG_VALGRIND=y and the following command:

  valgrind --suppressions=scripts/u-boot.supp \
    ./u-boot -T -c "setenv efi_selftest start image return; \
                    bootefi selftest"

Best regards,
Vincent.

 lib/efi_loader/efi_boottime.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/efi_loader/efi_boottime.c +1 -0
index ddc935d2240..0b3f2fd276c 100644
@@ -3453,6 +3453,7 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
        struct efi_loaded_image_obj *image_obj =
                (struct efi_loaded_image_obj *)image_handle;
        jmp_buf *exit_jmp;
+       u16 image_type;
 
        EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
                  exit_data_size, exit_data);
@@ -3496,13 +3497,14 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
        }
        /* efi_delete_image() frees image_obj. Copy before the call. */
        exit_jmp = image_obj->exit_jmp;

lib/efi_loader/efi_boottime.c static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, · line 3507

Quoted 10 lines
lib/efi_loader/efi_boottime.c +3 -2
+       image_type = image_obj->image_type;
        *image_obj->exit_status = exit_status;
-       if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
+       if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
            exit_status != EFI_SUCCESS)
                efi_delete_image(image_obj, loaded_image_protocol);
 
        if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
-               if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
+               if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {

This looks ok, but I don't remember the TCG spec on top of my head. Can't we just move the measurement before deleting the handle?

Thanks
/Ilias

Quoted 6 lines
lib/efi_loader/efi_boottime.c +0 -1
                        ret = efi_tcg2_measure_efi_app_exit();
                        if (ret != EFI_SUCCESS)
                                log_debug("tcg2 measurement fails (0x%lx)\n",
--

2.51.0

#2 lore raw

(Updated Kojima-san's e-mail address.)

Quoted 1 line · Ilias Apalodimas

Hi Vincent,

Hi Ilias,

Thank you for having a look!
My comments below.

Quoted 55 lines · Vincent Stehlé
Quoted 54 lines

The efi_exit() function frees the loaded image memory by calling efi_delete_image(). However, when CONFIG_EFI_TCG2_PROTOCOL is enabled, the image_obj->image_type structure member is accessed after the memory has been freed.

Fix this by keeping a copy of image_type, as is already done for exit_jmp.

Fixes 8fc4e0b4273a ("efi_loader: add boot variable measurement")
Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Tom Rini <[email protected]>
Cc Masahisa Kojima <[email protected]>

---

Hi,

This can be verified with sandbox_defconfig + CONFIG_VALGRIND=y and the following command:

  valgrind --suppressions=scripts/u-boot.supp \
    ./u-boot -T -c "setenv efi_selftest start image return; \
                    bootefi selftest"

Best regards,
Vincent.

 lib/efi_loader/efi_boottime.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/efi_loader/efi_boottime.c +3 -1
index ddc935d2240..0b3f2fd276c 100644
@@ -3453,6 +3453,7 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
        struct efi_loaded_image_obj *image_obj =
                (struct efi_loaded_image_obj *)image_handle;
        jmp_buf *exit_jmp;
+       u16 image_type;
 
        EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
                  exit_data_size, exit_data);
@@ -3496,13 +3497,14 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
        }
        /* efi_delete_image() frees image_obj. Copy before the call. */
        exit_jmp = image_obj->exit_jmp;
+       image_type = image_obj->image_type;
        *image_obj->exit_status = exit_status;
-       if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
+       if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
            exit_status != EFI_SUCCESS)
                efi_delete_image(image_obj, loaded_image_protocol);
 
Quoted 6 lines

lib/efi_loader/efi_boottime.c static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, · line 3507

Quoted 3 lines
lib/efi_loader/efi_boottime.c +1 -1
        if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
-               if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
+               if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {

This looks ok, but I don't remember the TCG spec on top of my head. Can't we just move the measurement before deleting the handle?

I think this can work indeed, and it is more elegant; I will send a v2, thanks!

If I read the specification[1] correctly, the measurements sequence in 8.2.4 is the following:

f. Execute UEFI application
i. If additional loads
measure
g. Measure EV_EFI_ACTION "Returning ..."
h. If select next
goto a
i. If ExitBootServices
measure EV_EFI_ACTION "Exit Boot Services ..."

Everything efi_exit() is doing is happening in step "g", therefore moving code around efi_delete_image() should have no effect on the measurements.

Best regards,
Vincent.

[1] https://trustedcomputinggroup.org/wp-content/uploads/PC-Client-Platform-Firmware-Profile-Version-1.06-Revision-52_pub.pdf

Quoted 9 lines

Thanks
/Ilias

Quoted 6 lines
lib/efi_loader/efi_boottime.c +0 -1
                        ret = efi_tcg2_measure_efi_app_exit();
                        if (ret != EFI_SUCCESS)
                                log_debug("tcg2 measurement fails (0x%lx)\n",
--

2.51.0

#3 lore raw

PATCH v2 1/1 efi_loader: fix use after free in efi_exit() with tcg2

In reply to Vincent Stehlé

The efi_exit() function frees the loaded image memory by calling efi_delete_image(). However, when CONFIG_EFI_TCG2_PROTOCOL is enabled, the image_obj->image_type structure member is accessed after the memory has been freed.

Fix this by performing the tcg2 measurement before the image deletion.

Fixes 8fc4e0b4273a ("efi_loader: add boot variable measurement")
Cc Heinrich Schuchardt <[email protected]>
Cc Tom Rini <[email protected]>
Cc Masahisa Kojima <[email protected]>

---

Hi,

Here is a respin after feedbacks. [1]

Changes for v2:
- Move the event measurement before image deletion instead of keeping a copy of image_type (thanks Ilias!)

This can be verified with sandbox_defconfig + CONFIG_VALGRIND=y and the following command:

  valgrind --suppressions=scripts/u-boot.supp \
    ./u-boot -T -c "setenv efi_selftest start image return; \
                    bootefi selftest"

This was lightly tested for regression with sandbox_defconfig and the following commands:

./u-boot -T -c "ut measurement"

./test/py/test.py --build-dir="$PWD" -s -k "test_efi_bootmgr \
or test_efi_loader or test_efi_selftest or test_efi_secboot"

Adding some instrumentation in efi_exit() and tcg2_log_append() shows no change in the event measurements sequence.

[1] https://lore.kernel.org/u-boot/[email protected]/

 lib/efi_loader/efi_boottime.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
lib/efi_loader/efi_boottime.c +7 -6
index ddc935d2240..b424d924896 100644
@@ -3494,12 +3494,6 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
 		if (ret != EFI_SUCCESS)
 			EFI_PRINT("%s: out of memory\n", __func__);
 	}
-	/* efi_delete_image() frees image_obj. Copy before the call. */
-	exit_jmp = image_obj->exit_jmp;
-	*image_obj->exit_status = exit_status;
-	if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
-	    exit_status != EFI_SUCCESS)
-		efi_delete_image(image_obj, loaded_image_protocol);
 
 	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
 		if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
@@ -3510,6 +3504,13 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
 		}
 	}
 
+	/* efi_delete_image() frees image_obj. Copy before the call. */
+	exit_jmp = image_obj->exit_jmp;
+	*image_obj->exit_status = exit_status;
+	if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
+	    exit_status != EFI_SUCCESS)
+		efi_delete_image(image_obj, loaded_image_protocol);
+
 	/* Make sure entry/exit counts for EFI world cross-overs match */
 	EFI_EXIT(exit_status);
 
Signature · 1 line
2.51.0
Acked-by on v2 1/1
#4 lore raw

Hi Vincent,

Quoted 84 lines

-----Original Message-----
From: Vincent Stehlé <[email protected]>
Sent: Wednesday, January 28, 2026 1:19 AM
Subject: [PATCH v2] efi_loader: fix use after free in efi_exit() with tcg2

The efi_exit() function frees the loaded image memory by calling efi_delete_image(). However, when CONFIG_EFI_TCG2_PROTOCOL is enabled, the
image_obj->image_type structure member is accessed after the memory has been freed.

Fix this by performing the tcg2 measurement before the image deletion.

Fixes 8fc4e0b4273a ("efi_loader: add boot variable measurement")
Cc Heinrich Schuchardt <[email protected]>
Cc Tom Rini <[email protected]>
Cc Masahisa Kojima <[email protected]>

---

Hi,

Here is a respin after feedbacks. [1]

Changes for v2:
- Move the event measurement before image deletion instead of keeping a copy of image_type (thanks Ilias!)

This can be verified with sandbox_defconfig + CONFIG_VALGRIND=y and the following command:

  valgrind --suppressions=scripts/u-boot.supp \
    ./u-boot -T -c "setenv efi_selftest start image return; \
                    bootefi selftest"

This was lightly tested for regression with sandbox_defconfig and the following commands:

./u-boot -T -c "ut measurement"

./test/py/test.py --build-dir="$PWD" -s -k "test_efi_bootmgr \
or test_efi_loader or test_efi_selftest or test_efi_secboot"

Adding some instrumentation in efi_exit() and tcg2_log_append() shows no change in the event measurements sequence.

[1]
https://lore.kernel.org/u-boot/20260123105814.1083834-1-vincent.stehle@ar
m.com/

 lib/efi_loader/efi_boottime.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
lib/efi_loader/efi_boottime.c +0 -4
index ddc935d2240..b424d924896 100644
@@ -3494,12 +3494,6 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t
 image_handle,
 		if (ret != EFI_SUCCESS)
 			EFI_PRINT("%s: out of memory\n", __func__);
 	}
-	/* efi_delete_image() frees image_obj. Copy before the call. */
-	exit_jmp = image_obj->exit_jmp;
-	*image_obj->exit_status = exit_status;
-	if (image_obj->image_type ==

IMAGE_SUBSYSTEM_EFI_APPLICATION ||

lib/efi_loader/efi_boottime.c +0 -2
-	    exit_status != EFI_SUCCESS)
-		efi_delete_image(image_obj, loaded_image_protocol);
 
 	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
 		if (image_obj->image_type ==

IMAGE_SUBSYSTEM_EFI_APPLICATION) {

lib/efi_loader/efi_boottime.c +4 -0
@@ -3510,6 +3504,13 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t
 image_handle,
 		}
 	}
 
+	/* efi_delete_image() frees image_obj. Copy before the call. */
+	exit_jmp = image_obj->exit_jmp;
+	*image_obj->exit_status = exit_status;
+	if (image_obj->image_type ==

IMAGE_SUBSYSTEM_EFI_APPLICATION ||

lib/efi_loader/efi_boottime.c static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, · line 3515

Quoted 5 lines
lib/efi_loader/efi_boottime.c +3 -0
+	    exit_status != EFI_SUCCESS)
+		efi_delete_image(image_obj, loaded_image_protocol);
+
 	/* Make sure entry/exit counts for EFI world cross-overs match */
 	EFI_EXIT(exit_status);

Thanks for the fix.

Best Regards,
Masahisa Kojima

Quoted 3 lines

--
2.51.0

Reviewed-by on v2 1/1
#5 lore raw
In reply to Vincent Stehlé
Quoted 7 lines · Vincent Stehlé

The efi_exit() function frees the loaded image memory by calling efi_delete_image(). However, when CONFIG_EFI_TCG2_PROTOCOL is enabled, the image_obj->image_type structure member is accessed after the memory has been freed.

Fix this by performing the tcg2 measurement before the image deletion.

Quoted 7 lines
Fixes 8fc4e0b4273a ("efi_loader: add boot variable measurement")
Cc Heinrich Schuchardt <[email protected]>
Cc Tom Rini <[email protected]>
Cc Masahisa Kojima <[email protected]>

---

Quoted 63 lines

Hi,

Here is a respin after feedbacks. [1]

Changes for v2:
- Move the event measurement before image deletion instead of keeping a copy of image_type (thanks Ilias!)

This can be verified with sandbox_defconfig + CONFIG_VALGRIND=y and the following command:

   valgrind --suppressions=scripts/u-boot.supp \
     ./u-boot -T -c "setenv efi_selftest start image return; \
                     bootefi selftest"

This was lightly tested for regression with sandbox_defconfig and the following commands:

./u-boot -T -c "ut measurement"

./test/py/test.py --build-dir="$PWD" -s -k "test_efi_bootmgr \
or test_efi_loader or test_efi_selftest or test_efi_secboot"

Adding some instrumentation in efi_exit() and tcg2_log_append() shows no change in the event measurements sequence.

[1] https://lore.kernel.org/u-boot/[email protected]/

  lib/efi_loader/efi_boottime.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
lib/efi_loader/efi_boottime.c +7 -6
index ddc935d2240..b424d924896 100644
@@ -3494,12 +3494,6 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  		if (ret != EFI_SUCCESS)
  			EFI_PRINT("%s: out of memory\n", __func__);
  	}
-	/* efi_delete_image() frees image_obj. Copy before the call. */
-	exit_jmp = image_obj->exit_jmp;
-	*image_obj->exit_status = exit_status;
-	if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
-	    exit_status != EFI_SUCCESS)
-		efi_delete_image(image_obj, loaded_image_protocol);
 
  	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
  		if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
@@ -3510,6 +3504,13 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  		}
  	}
 
+	/* efi_delete_image() frees image_obj. Copy before the call. */
+	exit_jmp = image_obj->exit_jmp;
+	*image_obj->exit_status = exit_status;
+	if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
+	    exit_status != EFI_SUCCESS)
+		efi_delete_image(image_obj, loaded_image_protocol);
+
  	/* Make sure entry/exit counts for EFI world cross-overs match */
  	EFI_EXIT(exit_status);
 
Reviewed-by Tested-by on v1 1/1
#6 lore raw
In reply to Vincent Stehlé

Thanks Vincent

I also tested that PCR4 remains unchanged after the patch.

Quoted 8 lines · Vincent Stehlé

The efi_exit() function frees the loaded image memory by calling efi_delete_image(). However, when CONFIG_EFI_TCG2_PROTOCOL is enabled, the image_obj->image_type structure member is accessed after the memory has been freed.

Fix this by keeping a copy of image_type, as is already done for exit_jmp.

Quoted 7 lines
Fixes 8fc4e0b4273a ("efi_loader: add boot variable measurement")
Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Tom Rini <[email protected]>
Cc Masahisa Kojima <[email protected]>

---

Quoted 48 lines

Hi,

This can be verified with sandbox_defconfig + CONFIG_VALGRIND=y and the following command:

  valgrind --suppressions=scripts/u-boot.supp \
    ./u-boot -T -c "setenv efi_selftest start image return; \
                    bootefi selftest"

Best regards,
Vincent.

 lib/efi_loader/efi_boottime.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/efi_loader/efi_boottime.c +4 -3
index ddc935d2240..0b3f2fd276c 100644
@@ -3453,6 +3453,7 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
        struct efi_loaded_image_obj *image_obj =
                (struct efi_loaded_image_obj *)image_handle;
        jmp_buf *exit_jmp;
+       u16 image_type;
 
        EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
                  exit_data_size, exit_data);
@@ -3496,13 +3497,14 @@ static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
        }
        /* efi_delete_image() frees image_obj. Copy before the call. */
        exit_jmp = image_obj->exit_jmp;
+       image_type = image_obj->image_type;
        *image_obj->exit_status = exit_status;
-       if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
+       if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
            exit_status != EFI_SUCCESS)
                efi_delete_image(image_obj, loaded_image_protocol);
 
        if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
-               if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
+               if (image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
                        ret = efi_tcg2_measure_efi_app_exit();
                        if (ret != EFI_SUCCESS)
                                log_debug("tcg2 measurement fails (0x%lx)\n",
--

2.51.0