Skip to contentSkip to conversation

efi: Protect against unnecessary image unloading

PATCH In review Andrew Cooper 4 participants 6 messages Oct 14, 2025 – Oct 15, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 efi: Protect against unnecessary image unloading

From: Gerald Elder-Vass <[email protected]>

Commit 59a1d6d3ea1e introduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: Daniel P. Smith <[email protected]>
CC: Oleksii Kurochko <[email protected]>

Gerald is OoO and time is tight on Xen 4.21, so I've picked the patch up.

Oleksii: This addresses follow-on feedback for a new feature in Xen 4.21, so really does want fixing before the release. I forgot to put it on the tracking list, sorry.

v2:
* Apply feedback as Marek wants it.

---
 xen/common/efi/boot.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
xen/common/efi/boot.c +6 -5
index 5b84dbf26e5e..3a78e7571a5e 100644
@@ -1062,7 +1062,7 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
     static EFI_GUID __initdata shim_image_guid = SHIM_IMAGE_LOADER_GUID;
     static EFI_GUID __initdata shim_lock_guid = SHIM_LOCK_PROTOCOL_GUID;
     SHIM_IMAGE_LOADER *shim_loader;
-    EFI_HANDLE loaded_kernel;
+    EFI_HANDLE loaded_kernel = NULL;
     EFI_SHIM_LOCK_PROTOCOL *shim_lock;
     EFI_STATUS status;
     bool verified = false;
@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
             verified = true;
 
         /*
-         * Always unload the image.  We only needed LoadImage() to perform
-         * verification anyway, and in the case of a failure there may still
-         * be cleanup needing to be performed.
+         * If the kernel was loaded, unload it. We only needed LoadImage() to
+         * perform verification anyway, and in the case of a failure there may
+         * still be cleanup needing to be performed.
          */
-        shim_loader->UnloadImage(loaded_kernel);
+        if ( !EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION) )
+            shim_loader->UnloadImage(loaded_kernel);
     }
 
     /* Otherwise, fall back to SHIM_LOCK. */

base-commit: 53859596c0d34dbca776ec1e47bac8dd90552530

Signature · 1 line
2.39.5
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Andrew Cooper

From: Gerald Elder-Vass <[email protected]>

Commit 59a1d6d3ea1e introduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.

with one comment below (I'm okay with the patch either way)

Quoted 22 lines

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: Daniel P. Smith <[email protected]>
CC: Oleksii Kurochko <[email protected]>

Gerald is OoO and time is tight on Xen 4.21, so I've picked the patch up.

Oleksii: This addresses follow-on feedback for a new feature in Xen 4.21, so really does want fixing before the release. I forgot to put it on the tracking list, sorry.

v2:
* Apply feedback as Marek wants it.

---
 xen/common/efi/boot.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
xen/common/efi/boot.c +0 -0
index 5b84dbf26e5e..3a78e7571a5e 100644

xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1065

Quoted 5 lines
xen/common/efi/boot.c +1 -1
@@ -1062,7 +1062,7 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
     static EFI_GUID __initdata shim_image_guid = SHIM_IMAGE_LOADER_GUID;
     static EFI_GUID __initdata shim_lock_guid = SHIM_LOCK_PROTOCOL_GUID;
     SHIM_IMAGE_LOADER *shim_loader;
-    EFI_HANDLE loaded_kernel;
+    EFI_HANDLE loaded_kernel = NULL;

While this isn't strictly necessary now (assuming correct firmware implementation), it helps just a bit with buggy firmware (that would leave loaded_kernel unset in case of EFI_SECURITY_VIOLATION, possibly leaking some memory). It still assumes UnloadImage() verifies its parameter in that case (spec suggests it should, but doesn't spell it out explicitly).

Quoted 6 lines
xen/common/efi/boot.c +0 -0
     EFI_SHIM_LOCK_PROTOCOL *shim_lock;
     EFI_STATUS status;
     bool verified = false;
@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
             verified = true;
 

xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1085

Quoted 10 lines
xen/common/efi/boot.c +4 -4
         /*
-         * Always unload the image.  We only needed LoadImage() to perform
-         * verification anyway, and in the case of a failure there may still
-         * be cleanup needing to be performed.
+         * If the kernel was loaded, unload it. We only needed LoadImage() to
+         * perform verification anyway, and in the case of a failure there may
+         * still be cleanup needing to be performed.
          */
-        shim_loader->UnloadImage(loaded_kernel);
+        if ( !EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION) )

So, just in case of double-buggy firmware, check loaded_kernel here too?

Quoted 9 lines
xen/common/efi/boot.c +1 -0
+            shim_loader->UnloadImage(loaded_kernel);
     }
 
     /* Otherwise, fall back to SHIM_LOCK. */

base-commit: 53859596c0d34dbca776ec1e47bac8dd90552530

Signature · 1 line
2.39.5
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#2 lore raw
Quoted 9 lines · Marek Marczykowski-Górecki
Quoted 8 lines · Andrew Cooper

From: Gerald Elder-Vass <[email protected]>

Commit 59a1d6d3ea1e introduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.

Quoted 3 lines
Quoted 2 lines

Thanks.

Quoted 8 lines

with one comment below (I'm okay with the patch either way)

Quoted 6 lines
     EFI_SHIM_LOCK_PROTOCOL *shim_lock;
     EFI_STATUS status;
     bool verified = false;
xen/common/efi/boot.c +0 -0
@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
             verified = true;
 
Quoted 11 lines

xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1085

Quoted 10 lines
xen/common/efi/boot.c +4 -4
         /*
-         * Always unload the image.  We only needed LoadImage() to perform
-         * verification anyway, and in the case of a failure there may still
-         * be cleanup needing to be performed.
+         * If the kernel was loaded, unload it. We only needed LoadImage() to
+         * perform verification anyway, and in the case of a failure there may
+         * still be cleanup needing to be performed.
          */
-        shim_loader->UnloadImage(loaded_kernel);
+        if ( !EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION) )

So, just in case of double-buggy firmware, check loaded_kernel here too?

So to be clear, you're asking for:

loaded_kernel && (!EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION))

here?  Yeah, can fix that up on commit.

~Andrew

#3 lore raw
Quoted 36 lines · Andrew Cooper
Quoted 9 lines · Marek Marczykowski-Górecki
Quoted 8 lines · Andrew Cooper

From: Gerald Elder-Vass <[email protected]>

Commit 59a1d6d3ea1e introduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.

Quoted 3 lines
Quoted 2 lines

Thanks.

Quoted 8 lines

with one comment below (I'm okay with the patch either way)

Quoted 6 lines
     EFI_SHIM_LOCK_PROTOCOL *shim_lock;
     EFI_STATUS status;
     bool verified = false;
xen/common/efi/boot.c +0 -0
@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
             verified = true;
 
Quoted 11 lines

xen/common/efi/boot.c static void __init efi_verify_kernel(EFI_HANDLE ImageHandle) · line 1085

Quoted 10 lines
xen/common/efi/boot.c +4 -4
         /*
-         * Always unload the image.  We only needed LoadImage() to perform
-         * verification anyway, and in the case of a failure there may still
-         * be cleanup needing to be performed.
+         * If the kernel was loaded, unload it. We only needed LoadImage() to
+         * perform verification anyway, and in the case of a failure there may
+         * still be cleanup needing to be performed.
          */
-        shim_loader->UnloadImage(loaded_kernel);
+        if ( !EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION) )

So, just in case of double-buggy firmware, check loaded_kernel here too?

Quoted 5 lines

So to be clear, you're asking for:

loaded_kernel && (!EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION))

here?

Yes.

Quoted 3 lines

Yeah, can fix that up on commit.

~Andrew

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#4 lore raw
In reply to Andrew Cooper
Quoted 15 lines · Andrew Cooper

From: Gerald Elder-Vass<[email protected]>

Commit 59a1d6d3ea1e introduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.

---
CC: Marek Marczykowski-Górecki<[email protected]>
CC: Daniel P. Smith<[email protected]>
CC: Oleksii Kurochko<[email protected]>

Quoted 5 lines

Gerald is OoO and time is tight on Xen 4.21, so I've picked the patch up.

Oleksii: This addresses follow-on feedback for a new feature in Xen 4.21, so really does want fixing before the release. I forgot to put it on the tracking list, sorry.

It seems critical enough as it could lead to undef. behaviour/boot-time crashes/etc. So we really want to have it in 4.21:

Thanks.

~ Oleksii

Quoted 39 lines

v2:
* Apply feedback as Marek wants it.

---
  xen/common/efi/boot.c | 11 ++++++-----
  1 file changed, 6 insertions(+), 5 deletions(-)
xen/common/efi/boot.c +6 -5
index 5b84dbf26e5e..3a78e7571a5e 100644
@@ -1062,7 +1062,7 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
      static EFI_GUID __initdata shim_image_guid = SHIM_IMAGE_LOADER_GUID;
      static EFI_GUID __initdata shim_lock_guid = SHIM_LOCK_PROTOCOL_GUID;
      SHIM_IMAGE_LOADER *shim_loader;
-    EFI_HANDLE loaded_kernel;
+    EFI_HANDLE loaded_kernel = NULL;
      EFI_SHIM_LOCK_PROTOCOL *shim_lock;
      EFI_STATUS status;
      bool verified = false;
@@ -1078,11 +1078,12 @@ static void __init efi_verify_kernel(EFI_HANDLE ImageHandle)
              verified = true;
 
          /*
-         * Always unload the image.  We only needed LoadImage() to perform
-         * verification anyway, and in the case of a failure there may still
-         * be cleanup needing to be performed.
+         * If the kernel was loaded, unload it. We only needed LoadImage() to
+         * perform verification anyway, and in the case of a failure there may
+         * still be cleanup needing to be performed.
           */
-        shim_loader->UnloadImage(loaded_kernel);
+        if ( !EFI_ERROR(status) || (status == EFI_SECURITY_VIOLATION) )
+            shim_loader->UnloadImage(loaded_kernel);
      }
 
      /* Otherwise, fall back to SHIM_LOCK. */

base-commit: 53859596c0d34dbca776ec1e47bac8dd90552530

Reviewed-by on v2 1/1
#5 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

From: Gerald Elder-Vass <[email protected]>

Commit 59a1d6d3ea1e introduced Shim's LoadImage protocol and unloads the image after loading it (for verification purposes) regardless of the returned status. The protocol API implies this is the correct behaviour but we should add a check to protect against the unlikely case this frees any memory in use.

--
Yann Sionneau | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech