Skip to contentSkip to conversation

efi: Add a function to check if Secure Boot mode is enabled

PATCH Superseded Gerald Elder-Vass 4 participants 13 messages Sep 5, 2025 – Sep 9, 2025 1 message not in the archive

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v4 1/2 efi: Add a function to check if Secure Boot mode is enabled

From: Ross Lagerwall <[email protected]>

Also cache it to avoid needing to repeatedly ask the firmware.

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>

v4:
- Fix MISRA warning regarding SecureBoot string
v3:
- Fix build on ARM

---
 xen/common/efi/boot.c    | 24 ++++++++++++++++++++++++
 xen/common/efi/runtime.c |  1 +
 xen/include/xen/efi.h    |  2 ++
 3 files changed, 27 insertions(+)
xen/common/efi/boot.c +24 -0
index e12fa1a7ec04..ccbfc401f7ba 100644
@@ -901,6 +901,28 @@ static void __init pre_parse(const struct file *file)
                    " last line will be ignored.\r\n");
 }
 
+static void __init init_secure_boot_mode(void)
+{
+    static EFI_GUID __initdata gv_uuid = EFI_GLOBAL_VARIABLE;
+    static CHAR16 __initdata str_SecureBoot[] = L"SecureBoot";
+    EFI_STATUS status;
+    uint8_t data = 0;
+    UINTN size = sizeof(data);
+    UINT32 attr = 0;
+
+    status = efi_rs->GetVariable(str_SecureBoot, &gv_uuid, &attr, &size, &data);
+
+    if ( status == EFI_NOT_FOUND ||
+         (status == EFI_SUCCESS &&
+          attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS) &&
+          size == 1 && data == 0) )
+        /* Platform does not support Secure Boot or it's disabled. */
+        efi_secure_boot = false;
+    else
+        /* Everything else play it safe and assume enabled. */
+        efi_secure_boot = true;
+}
+
 static void __init efi_init(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
 {
     efi_ih = ImageHandle;
@@ -915,6 +937,8 @@ static void __init efi_init(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTabl
 
     StdOut = SystemTable->ConOut;
     StdErr = SystemTable->StdErr ?: StdOut;
+
+    init_secure_boot_mode();
 }
 
 static void __init efi_console_set_mode(void)
xen/common/efi/runtime.c +1 -0
index 42386c6bde42..30d649ca5c1b 100644
@@ -41,6 +41,7 @@ void efi_rs_leave(struct efi_rs_state *state);
 unsigned int __read_mostly efi_num_ct;
 const EFI_CONFIGURATION_TABLE *__read_mostly efi_ct;
 
+bool __ro_after_init efi_secure_boot;
 unsigned int __read_mostly efi_version;
 unsigned int __read_mostly efi_fw_revision;
 const CHAR16 *__read_mostly efi_fw_vendor;
xen/include/xen/efi.h +2 -0
index 623ed2ccdf31..723cb8085270 100644
@@ -36,6 +36,8 @@ static inline bool efi_enabled(unsigned int feature)
 }
 #endif
 
+extern bool efi_secure_boot;
+
 void efi_init_memory(void);
 bool efi_boot_mem_unused(unsigned long *start, unsigned long *end);
 bool efi_rs_using_pgtables(void);
Signature · 1 line
2.47.3
Acked-by on v4 1/2
#3 lore raw
In reply to Gerald Elder-Vass
Quoted 6 lines · Gerald Elder-Vass

From: Ross Lagerwall <[email protected]>

Also cache it to avoid needing to repeatedly ask the firmware.

Quoted 33 lines

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>

v4:
- Fix MISRA warning regarding SecureBoot string
v3:
- Fix build on ARM

---
 xen/common/efi/boot.c    | 24 ++++++++++++++++++++++++
 xen/common/efi/runtime.c |  1 +
 xen/include/xen/efi.h    |  2 ++
 3 files changed, 27 insertions(+)
xen/common/efi/boot.c +4 -0
index e12fa1a7ec04..ccbfc401f7ba 100644
@@ -901,6 +901,28 @@ static void __init pre_parse(const struct file *file)
                    " last line will be ignored.\r\n");
 }
 
+static void __init init_secure_boot_mode(void)
+{
+    static EFI_GUID __initdata gv_uuid = EFI_GLOBAL_VARIABLE;
+    static CHAR16 __initdata str_SecureBoot[] = L"SecureBoot";

xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 917

Quoted 10 lines
xen/common/efi/boot.c +10 -0
+    EFI_STATUS status;
+    uint8_t data = 0;
+    UINTN size = sizeof(data);
+    UINT32 attr = 0;
+
+    status = efi_rs->GetVariable(str_SecureBoot, &gv_uuid, &attr, &size, &data);
+
+    if ( status == EFI_NOT_FOUND ||
+         (status == EFI_SUCCESS &&
+          attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS) &&

FYI, I was unsure if checking this is a good idea, but UEFI spec does define exactly those attributes for the "SecureBoot" variable, so it sounds okay.

Quoted 48 lines
xen/common/efi/boot.c +10 -0
+          size == 1 && data == 0) )
+        /* Platform does not support Secure Boot or it's disabled. */
+        efi_secure_boot = false;
+    else
+        /* Everything else play it safe and assume enabled. */
+        efi_secure_boot = true;
+}
+
 static void __init efi_init(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
 {
     efi_ih = ImageHandle;
@@ -915,6 +937,8 @@ static void __init efi_init(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTabl
 
     StdOut = SystemTable->ConOut;
     StdErr = SystemTable->StdErr ?: StdOut;
+
+    init_secure_boot_mode();
 }
 
 static void __init efi_console_set_mode(void)
xen/common/efi/runtime.c +1 -0
index 42386c6bde42..30d649ca5c1b 100644
@@ -41,6 +41,7 @@ void efi_rs_leave(struct efi_rs_state *state);
 unsigned int __read_mostly efi_num_ct;
 const EFI_CONFIGURATION_TABLE *__read_mostly efi_ct;
 
+bool __ro_after_init efi_secure_boot;
 unsigned int __read_mostly efi_version;
 unsigned int __read_mostly efi_fw_revision;
 const CHAR16 *__read_mostly efi_fw_vendor;
xen/include/xen/efi.h +2 -0
index 623ed2ccdf31..723cb8085270 100644
@@ -36,6 +36,8 @@ static inline bool efi_enabled(unsigned int feature)
 }
 #endif
 
+extern bool efi_secure_boot;
+
 void efi_init_memory(void);
 bool efi_boot_mem_unused(unsigned long *start, unsigned long *end);
 bool efi_rs_using_pgtables(void);
Signature · 1 line
2.47.3
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#4 lore raw
In reply to Gerald Elder-Vass
Quoted 32 lines · Gerald Elder-Vass

From: Ross Lagerwall <[email protected]>

Also cache it to avoid needing to repeatedly ask the firmware.

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>

v4:
- Fix MISRA warning regarding SecureBoot string
v3:
- Fix build on ARM

---
 xen/common/efi/boot.c    | 24 ++++++++++++++++++++++++
 xen/common/efi/runtime.c |  1 +
 xen/include/xen/efi.h    |  2 ++
 3 files changed, 27 insertions(+)
xen/common/efi/boot.c +0 -0
index e12fa1a7ec04..ccbfc401f7ba 100644

xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 910

Quoted 10 lines
xen/common/efi/boot.c +7 -0
@@ -901,6 +901,28 @@ static void __init pre_parse(const struct file *file)
                    " last line will be ignored.\r\n");
 }
 
+static void __init init_secure_boot_mode(void)
+{
+    static EFI_GUID __initdata gv_uuid = EFI_GLOBAL_VARIABLE;
+    static CHAR16 __initdata str_SecureBoot[] = L"SecureBoot";
+    EFI_STATUS status;
+    uint8_t data = 0;
+    UINTN size = sizeof(data);

Unlike here, ...

xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 917

Quoted 7 lines
xen/common/efi/boot.c +7 -0
+    UINT32 attr = 0;
+
+    status = efi_rs->GetVariable(str_SecureBoot, &gv_uuid, &attr, &size, &data);
+
+    if ( status == EFI_NOT_FOUND ||
+         (status == EFI_SUCCESS &&
+          attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS) &&

(Nit: Overlong line.)

xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 918

Quoted 1 line
xen/common/efi/boot.c +1 -0
+          size == 1 && data == 0) )

... any reason it's literal 1 here?

Jan

#5 lore raw
Quoted 3 lines
Quoted 1 line

+ size == 1 && data == 0) )

... any reason it's literal 1 here?

The size variable is also used as output from GetVariable and we should verify that the size of the returned data is as expected, it is simply one byte so probably not worth defining any macros to make it clearer

*Gerald Elder-Vass*
Senior Software Engineer

XenServer
Cambridge, UK

Quoted 65 lines · Jan Beulich

xen/common/efi/boot.c static void __init pre_parse(const struct file

Quoted 32 lines · Gerald Elder-Vass

From: Ross Lagerwall <[email protected]>

Also cache it to avoid needing to repeatedly ask the firmware.

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>

v4:
- Fix MISRA warning regarding SecureBoot string
v3:
- Fix build on ARM

---
 xen/common/efi/boot.c    | 24 ++++++++++++++++++++++++
 xen/common/efi/runtime.c |  1 +
 xen/include/xen/efi.h    |  2 ++
 3 files changed, 27 insertions(+)
xen/common/efi/boot.c +0 -0
index e12fa1a7ec04..ccbfc401f7ba 100644
@@ -901,6 +901,28 @@ static void __init pre_parse(const struct file

*file)

xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 910

Quoted 10 lines
xen/common/efi/boot.c +7 -0
                    " last line will be ignored.\r\n");
 }
 
+static void __init init_secure_boot_mode(void)
+{
+    static EFI_GUID __initdata gv_uuid = EFI_GLOBAL_VARIABLE;
+    static CHAR16 __initdata str_SecureBoot[] = L"SecureBoot";
+    EFI_STATUS status;
+    uint8_t data = 0;
+    UINTN size = sizeof(data);

Unlike here, ...

xen/common/efi/boot.c static void __init pre_parse(const struct file *file)

Quoted 3 lines
xen/common/efi/boot.c +3 -0
+    UINT32 attr = 0;
+
+    status = efi_rs->GetVariable(str_SecureBoot, &gv_uuid, &attr,

&size, &data);

xen/common/efi/boot.c static void __init pre_parse(const struct file *file)

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+
+    if ( status == EFI_NOT_FOUND ||
+         (status == EFI_SUCCESS &&
+          attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |

EFI_VARIABLE_RUNTIME_ACCESS) &&

(Nit: Overlong line.)

xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 918

Quoted 1 line
xen/common/efi/boot.c +1 -0
+          size == 1 && data == 0) )

... any reason it's literal 1 here?

Jan

#6 lore raw
Quoted 7 lines · Gerald Elder-Vass
Quoted 3 lines
Quoted 1 line

+ size == 1 && data == 0) )

... any reason it's literal 1 here?

The size variable is also used as output from GetVariable and we should verify that the size of the returned data is as expected, it is simply one byte so probably not worth defining any macros to make it clearer

I don't understand this reply. Why would the initializer of the variable use one thing (sizeof()) and the checking of the variable another (literal 1)? Even consistently using 1 would already be better imo; consistently using sizeof() is what I think would be best.

Jan

#7 lore raw
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Gerald Elder-Vass
Quoted 3 lines
Quoted 1 line

+ size == 1 && data == 0) )

... any reason it's literal 1 here?

Quoted 8 lines
Quoted 3 lines

The size variable is also used as output from GetVariable and we should verify that the size of the returned data is as expected, it is simply one byte so probably not worth defining any macros to make it clearer

I don't understand this reply. Why would the initializer of the variable use one thing (sizeof()) and the checking of the variable another (literal 1)? Even consistently using 1 would already be better imo; consistently using sizeof() is what I think would be best.

'size' as input value is the allocated size of the data parameter, so makes sense to be sizeof(data). IOW, 'size' as the input value comes from the size of the 'data' variable, while the output value check comes from UEFI spec. While the size of the 'data' variable should match the spec, IMO changing its type (to a wider one) should not break the behavior here.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#8 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Gerald Elder-Vass
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ size == 1 && data == 0) )

... any reason it's literal 1 here?

The size variable is also used as output from GetVariable and we

should

Quoted 1 line
Quoted 1 line
Quoted 1 line

verify that the size of the returned data is as expected, it is

simply one

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

byte so probably not worth defining any macros to make it clearer

I don't understand this reply. Why would the initializer of the variable use one thing (sizeof()) and the checking of the variable another

(literal

Quoted 9 lines
Quoted 2 lines

1)? Even consistently using 1 would already be better imo; consistently using sizeof() is what I think would be best.

'size' as input value is the allocated size of the data parameter, so makes sense to be sizeof(data). IOW, 'size' as the input value comes from the size of the 'data' variable, while the output value check comes from UEFI spec. While the size of the 'data' variable should match the spec, IMO changing its type (to a wider one) should not break the behavior here.

The UEFI spec defines the "SecureBoot" global variable as an 8-bit unsigned
integer, in the event the size of the data used for output was not large enough to
contain the output then it would return an EFI_BUFFER_TOO_SMALL status (which the function would then interpret as "play it safe and assume enabled").

The "SecureBoot" variable is defined:
https://uefi.org/specs/UEFI/2.11/03_Boot_Manager.html#globally-defined-variables

So I believe we are correct in using uint8_t here

*Gerald Elder-Vass*
Senior Software Engineer

XenServer
Cambridge, UK

Quoted 29 lines · Marek Marczykowski-Górecki
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Gerald Elder-Vass
Quoted 3 lines
Quoted 1 line

+ size == 1 && data == 0) )

... any reason it's literal 1 here?

The size variable is also used as output from GetVariable and we should verify that the size of the returned data is as expected, it is simply

one

Quoted 4 lines
Quoted 1 line

byte so probably not worth defining any macros to make it clearer

I don't understand this reply. Why would the initializer of the variable use one thing (sizeof()) and the checking of the variable another

(literal

Quoted 2 lines

1)? Even consistently using 1 would already be better imo; consistently using sizeof() is what I think would be best.

'size' as input value is the allocated size of the data parameter, so makes sense to be sizeof(data). IOW, 'size' as the input value comes from the size of the 'data' variable, while the output value check comes from UEFI spec. While the size of the 'data' variable should match the spec, IMO changing its type (to a wider one) should not break the behavior here.

--
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab

#9 lore raw

PATCH v4 2/2 efi: Support using Shim's LoadImage protocol

In reply to a message not in the archive

The existing Verify functionality of the Shim lock protocol is deprecated and will be removed, the alternative it to use the LoadImage interface to perform the verification.

When the loading is successful we won't be using the newly loaded image (as of yet) so we must then immediately unload the image to clean up.

If the LoadImage protocol isn't available then fall back to the Shim Lock (Verify) interface.

Log when the kernel is not verified and fail if this occurs when secure boot mode is enabled.

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>

v4:
- Updated error message when failing due to lack of verification

v3:
- Use Shim Image by default, fall back to Shim Lock

---
 xen/common/efi/boot.c | 59 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 51 insertions(+), 8 deletions(-)
xen/common/efi/boot.c +51 -8
index ccbfc401f7ba..0a72c293301d 100644
@@ -38,6 +38,8 @@
   { 0xf2fd1544U, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94} }
 #define SHIM_LOCK_PROTOCOL_GUID \
   { 0x605dab50U, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }
+#define SHIM_IMAGE_LOADER_GUID \
+  { 0x1f492041U, 0xfadb, 0x4e59, {0x9e, 0x57, 0x7c, 0xaf, 0xe7, 0x3a, 0x55, 0xab} }
 #define APPLE_PROPERTIES_PROTOCOL_GUID \
   { 0x91bd12feU, 0xf6c3, 0x44fb, {0xa5, 0xb7, 0x51, 0x22, 0xab, 0x30, 0x3a, 0xe0} }
 #define EFI_SYSTEM_RESOURCE_TABLE_GUID    \
@@ -70,6 +72,13 @@ typedef struct {
     EFI_SHIM_LOCK_VERIFY Verify;
 } EFI_SHIM_LOCK_PROTOCOL;
 
+typedef struct _SHIM_IMAGE_LOADER {
+    EFI_IMAGE_LOAD LoadImage;
+    EFI_IMAGE_START StartImage;
+    EFI_EXIT Exit;
+    EFI_IMAGE_UNLOAD UnloadImage;
+} SHIM_IMAGE_LOADER;
+
 struct _EFI_APPLE_PROPERTIES;
 
 typedef EFI_STATUS
@@ -1047,6 +1056,46 @@ static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop,
     return gop_mode;
 }
 
+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_SHIM_LOCK_PROTOCOL *shim_lock;
+    EFI_STATUS status;
+    bool verified = false;
+
+    /* Look for LoadImage first */
+    if ( !EFI_ERROR(efi_bs->LocateProtocol(&shim_image_guid, NULL,
+                                           (void **)&shim_loader)) )
+    {
+        status = shim_loader->LoadImage(false, ImageHandle, NULL,
+                                        (void *)kernel.ptr, kernel.size,
+                                        &loaded_kernel);
+        if ( !EFI_ERROR(status) )
+            verified = true;
+
+        /* LoadImage performed verification, now clean up with UnloadImage */
+        shim_loader->UnloadImage(loaded_kernel);
+    }
+
+    /* else fall back to Shim Lock */
+    if ( !verified &&
+         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
+                                           (void **)&shim_lock)) &&
+         !EFI_ERROR(shim_lock->Verify(kernel.ptr, kernel.size)) )
+        verified = true;
+
+    if ( !verified )
+    {
+        PrintStr(L"Kernel was not verified\n");
+
+        if ( efi_secure_boot )
+            blexit(L"Refusing to boot unverified kernel with UEFI SecureBoot enabled");
+    }
+}
+
 static void __init efi_tables(void)
 {
     unsigned int i;
@@ -1334,13 +1383,11 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
                                       EFI_SYSTEM_TABLE *SystemTable)
 {
     static EFI_GUID __initdata loaded_image_guid = LOADED_IMAGE_PROTOCOL;
-    static EFI_GUID __initdata shim_lock_guid = SHIM_LOCK_PROTOCOL_GUID;
     EFI_LOADED_IMAGE *loaded_image;
     EFI_STATUS status;
     unsigned int i;
     CHAR16 *file_name, *cfg_file_name = NULL, *options = NULL;
     UINTN gop_mode = ~0;
-    EFI_SHIM_LOCK_PROTOCOL *shim_lock;
     EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL;
     union string section = { NULL }, name;
     bool base_video = false;
@@ -1591,12 +1638,8 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
      * device tree through the efi_check_dt_boot function, in this stage
      * verify it.
      */
-    if ( kernel.ptr &&
-         !kernel_verified &&
-         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
-                                           (void **)&shim_lock)) &&
-         (status = shim_lock->Verify(kernel.ptr, kernel.size)) != EFI_SUCCESS )
-        PrintErrMesg(L"Dom0 kernel image could not be verified", status);
+    if ( kernel.ptr && !kernel_verified )
+        efi_verify_kernel(ImageHandle);
 
     efi_arch_edd();
 
Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH v4 2/2] efi: Support using Shim's LoadImage protocol

Quoted 19 lines · Gerald Elder-Vass
xen/common/efi/boot.c +11 -0
index ccbfc401f7ba..0a72c293301d 100644
@@ -1047,6 +1056,46 @@ static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop,
     return gop_mode;
 }
 
+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_SHIM_LOCK_PROTOCOL *shim_lock;
+    EFI_STATUS status;
+    bool verified = false;
+
+    /* Look for LoadImage first */

xen/common/efi/boot.c static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, · line 1079

Quoted 10 lines
xen/common/efi/boot.c +10 -0
+    if ( !EFI_ERROR(efi_bs->LocateProtocol(&shim_image_guid, NULL,
+                                           (void **)&shim_loader)) )
+    {
+        status = shim_loader->LoadImage(false, ImageHandle, NULL,
+                                        (void *)kernel.ptr, kernel.size,
+                                        &loaded_kernel);
+        if ( !EFI_ERROR(status) )
+            verified = true;
+
+        /* LoadImage performed verification, now clean up with UnloadImage */

I think this wants a bit of the explanation given in reply to v3. Something like:

/* Always unload the image.  We only wanted LoadImage to perform verification anyway, and in the case of a failure, there may still be cleanup needing to be performed. */

I can fix this on commit if there are no other concerns.

~Andrew

Acked-by on v4 2/2
#11 lore raw

Re: [PATCH v4 2/2] efi: Support using Shim's LoadImage protocol

In reply to Gerald Elder-Vass
Quoted 7 lines · Gerald Elder-Vass

The existing Verify functionality of the Shim lock protocol is deprecated and will be removed, the alternative it to use the LoadImage interface to perform the verification.

When the loading is successful we won't be using the newly loaded image (as of yet) so we must then immediately unload the image to clean up.

Quoted 8 lines

If the LoadImage protocol isn't available then fall back to the Shim Lock (Verify) interface.

Log when the kernel is not verified and fail if this occurs when secure boot mode is enabled.

Preferably with comment adjustment as suggested by Andrew.

Quoted 126 lines

---
CC: Marek Marczykowski-Górecki <[email protected]>
CC: "Daniel P. Smith" <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Julien Grall <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Stefano Stabellini <[email protected]>

v4:
- Updated error message when failing due to lack of verification

v3:
- Use Shim Image by default, fall back to Shim Lock

---
 xen/common/efi/boot.c | 59 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 51 insertions(+), 8 deletions(-)
xen/common/efi/boot.c +51 -8
index ccbfc401f7ba..0a72c293301d 100644
@@ -38,6 +38,8 @@
   { 0xf2fd1544U, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94} }
 #define SHIM_LOCK_PROTOCOL_GUID \
   { 0x605dab50U, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }
+#define SHIM_IMAGE_LOADER_GUID \
+  { 0x1f492041U, 0xfadb, 0x4e59, {0x9e, 0x57, 0x7c, 0xaf, 0xe7, 0x3a, 0x55, 0xab} }
 #define APPLE_PROPERTIES_PROTOCOL_GUID \
   { 0x91bd12feU, 0xf6c3, 0x44fb, {0xa5, 0xb7, 0x51, 0x22, 0xab, 0x30, 0x3a, 0xe0} }
 #define EFI_SYSTEM_RESOURCE_TABLE_GUID    \
@@ -70,6 +72,13 @@ typedef struct {
     EFI_SHIM_LOCK_VERIFY Verify;
 } EFI_SHIM_LOCK_PROTOCOL;
 
+typedef struct _SHIM_IMAGE_LOADER {
+    EFI_IMAGE_LOAD LoadImage;
+    EFI_IMAGE_START StartImage;
+    EFI_EXIT Exit;
+    EFI_IMAGE_UNLOAD UnloadImage;
+} SHIM_IMAGE_LOADER;
+
 struct _EFI_APPLE_PROPERTIES;
 
 typedef EFI_STATUS
@@ -1047,6 +1056,46 @@ static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop,
     return gop_mode;
 }
 
+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_SHIM_LOCK_PROTOCOL *shim_lock;
+    EFI_STATUS status;
+    bool verified = false;
+
+    /* Look for LoadImage first */
+    if ( !EFI_ERROR(efi_bs->LocateProtocol(&shim_image_guid, NULL,
+                                           (void **)&shim_loader)) )
+    {
+        status = shim_loader->LoadImage(false, ImageHandle, NULL,
+                                        (void *)kernel.ptr, kernel.size,
+                                        &loaded_kernel);
+        if ( !EFI_ERROR(status) )
+            verified = true;
+
+        /* LoadImage performed verification, now clean up with UnloadImage */
+        shim_loader->UnloadImage(loaded_kernel);
+    }
+
+    /* else fall back to Shim Lock */
+    if ( !verified &&
+         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
+                                           (void **)&shim_lock)) &&
+         !EFI_ERROR(shim_lock->Verify(kernel.ptr, kernel.size)) )
+        verified = true;
+
+    if ( !verified )
+    {
+        PrintStr(L"Kernel was not verified\n");
+
+        if ( efi_secure_boot )
+            blexit(L"Refusing to boot unverified kernel with UEFI SecureBoot enabled");
+    }
+}
+
 static void __init efi_tables(void)
 {
     unsigned int i;
@@ -1334,13 +1383,11 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
                                       EFI_SYSTEM_TABLE *SystemTable)
 {
     static EFI_GUID __initdata loaded_image_guid = LOADED_IMAGE_PROTOCOL;
-    static EFI_GUID __initdata shim_lock_guid = SHIM_LOCK_PROTOCOL_GUID;
     EFI_LOADED_IMAGE *loaded_image;
     EFI_STATUS status;
     unsigned int i;
     CHAR16 *file_name, *cfg_file_name = NULL, *options = NULL;
     UINTN gop_mode = ~0;
-    EFI_SHIM_LOCK_PROTOCOL *shim_lock;
     EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL;
     union string section = { NULL }, name;
     bool base_video = false;
@@ -1591,12 +1638,8 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
      * device tree through the efi_check_dt_boot function, in this stage
      * verify it.
      */
-    if ( kernel.ptr &&
-         !kernel_verified &&
-         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
-                                           (void **)&shim_lock)) &&
-         (status = shim_lock->Verify(kernel.ptr, kernel.size)) != EFI_SUCCESS )
-        PrintErrMesg(L"Dom0 kernel image could not be verified", status);
+    if ( kernel.ptr && !kernel_verified )
+        efi_verify_kernel(ImageHandle);
 
     efi_arch_edd();
 
Signature · 1 line
2.47.3
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#12 lore raw

Re: [PATCH v4 2/2] efi: Support using Shim's LoadImage protocol

In reply to Gerald Elder-Vass
Quoted 23 lines · Gerald Elder-Vass
xen/common/efi/boot.c +19 -0
@@ -1047,6 +1056,46 @@ static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop,
     return gop_mode;
 }
 
+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_SHIM_LOCK_PROTOCOL *shim_lock;
+    EFI_STATUS status;
+    bool verified = false;
+
+    /* Look for LoadImage first */
+    if ( !EFI_ERROR(efi_bs->LocateProtocol(&shim_image_guid, NULL,
+                                           (void **)&shim_loader)) )
+    {
+        status = shim_loader->LoadImage(false, ImageHandle, NULL,
+                                        (void *)kernel.ptr, kernel.size,
+                                        &loaded_kernel);
+        if ( !EFI_ERROR(status) )
+            verified = true;

xen/common/efi/boot.c static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, · line 1087

Quoted 10 lines
xen/common/efi/boot.c +10 -0
+
+        /* LoadImage performed verification, now clean up with UnloadImage */
+        shim_loader->UnloadImage(loaded_kernel);
+    }
+
+    /* else fall back to Shim Lock */
+    if ( !verified &&
+         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
+                                           (void **)&shim_lock)) &&
+         !EFI_ERROR(shim_lock->Verify(kernel.ptr, kernel.size)) )

As already said on an earlier version, the use of !EFI_ERROR() here is a behavioral change from ...

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle, · line 1598

Quoted 9 lines
xen/common/efi/boot.c +0 -5
@@ -1591,12 +1638,8 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
      * device tree through the efi_check_dt_boot function, in this stage
      * verify it.
      */
-    if ( kernel.ptr &&
-         !kernel_verified &&
-         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
-                                           (void **)&shim_lock)) &&
-         (status = shim_lock->Verify(kernel.ptr, kernel.size)) != EFI_SUCCESS )

... checking for EFI_SUCCESS alone here. There's also nothing in the description justifying the change. (See the various EFI_WARN_* that exist.)

Jan

#13 lore raw

Re: [PATCH v4 2/2] efi: Support using Shim's LoadImage protocol

Quoted 4 lines

As already said on an earlier version, the use of !EFI_ERROR() here is a behavioral change from ...

Review void EFIAPI __init noreturn efi_start(EFI_HANDLE

Quoted 1 line
(quoted diff) +0 -0
@@ -1591,12 +1638,8 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE

ImageHandle,

Quoted 8 lines

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,

Quoted 8 lines
xen/common/efi/boot.c +0 -5
      * device tree through the efi_check_dt_boot function, in this stage
      * verify it.
      */
-    if ( kernel.ptr &&
-         !kernel_verified &&
-         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
-                                           (void **)&shim_lock)) &&
-         (status = shim_lock->Verify(kernel.ptr, kernel.size)) !=

EFI_SUCCESS )

Quoted 3 lines

... checking for EFI_SUCCESS alone here. There's also nothing in the description justifying the change. (See the various EFI_WARN_* that exist.)

You're correct! The EFI_WARN_* responses should all be treated as failures, I'll revert that particular change in the patch series

*Gerald Elder-Vass*
Senior Software Engineer

XenServer
Cambridge, UK

Quoted 57 lines · Jan Beulich

Review static UINTN __init

Quoted 1 line · Gerald Elder-Vass
(quoted diff) +0 -0
@@ -1047,6 +1056,46 @@ static UINTN __init

efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop,

Quoted 14 lines
xen/common/efi/boot.c +11 -0
     return gop_mode;
 }
 
+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_SHIM_LOCK_PROTOCOL *shim_lock;
+    EFI_STATUS status;
+    bool verified = false;
+
+    /* Look for LoadImage first */

xen/common/efi/boot.c static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop,

Quoted 10 lines
xen/common/efi/boot.c +10 -0
+    if ( !EFI_ERROR(efi_bs->LocateProtocol(&shim_image_guid, NULL,
+                                           (void **)&shim_loader)) )
+    {
+        status = shim_loader->LoadImage(false, ImageHandle, NULL,
+                                        (void *)kernel.ptr, kernel.size,
+                                        &loaded_kernel);
+        if ( !EFI_ERROR(status) )
+            verified = true;
+
+        /* LoadImage performed verification, now clean up with

UnloadImage */

xen/common/efi/boot.c static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, · line 1087

Quoted 8 lines
xen/common/efi/boot.c +8 -0
+        shim_loader->UnloadImage(loaded_kernel);
+    }
+
+    /* else fall back to Shim Lock */
+    if ( !verified &&
+         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
+                                           (void **)&shim_lock)) &&
+         !EFI_ERROR(shim_lock->Verify(kernel.ptr, kernel.size)) )

As already said on an earlier version, the use of !EFI_ERROR() here is a behavioral change from ...

Review void EFIAPI __init noreturn efi_start(EFI_HANDLE

Quoted 1 line
(quoted diff) +0 -0
@@ -1591,12 +1638,8 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE

ImageHandle,

xen/common/efi/boot.c void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,

Quoted 8 lines
xen/common/efi/boot.c +0 -5
      * device tree through the efi_check_dt_boot function, in this stage
      * verify it.
      */
-    if ( kernel.ptr &&
-         !kernel_verified &&
-         !EFI_ERROR(efi_bs->LocateProtocol(&shim_lock_guid, NULL,
-                                           (void **)&shim_lock)) &&
-         (status = shim_lock->Verify(kernel.ptr, kernel.size)) !=

EFI_SUCCESS )

... checking for EFI_SUCCESS alone here. There's also nothing in the description justifying the change. (See the various EFI_WARN_* that exist.)

Jan