Skip to contentSkip to conversation

efi: Secure Boot fixes

PATCH Superseded Daniel Kiper 3 participants 6 messages Jun 29, 2023 – Jun 30, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover efi: Secure Boot fixes

Hi,

Here are two fixes for Secure Boot issues reported recently. This version of patch set is more verbose in case of fallback to legacy Linux kernel loader as it was suggested by Ard. Please review and test.

Daniel

Daniel Kiper (2):
efi: Drop __grub_efi_api attribute from shim_lock->verify() call
efi: Fallback to legacy mode if shim is loaded on x86 archs

 grub-core/kern/efi/sb.c      | 10 ++++++++++
 grub-core/loader/efi/linux.c | 17 +++++++++++++++++
 include/grub/efi/api.h       |  2 +-
 include/grub/efi/sb.h        |  5 ++++-
 4 files changed, 32 insertions(+), 2 deletions(-)
#1 lore raw

PATCH v2 1/2 efi: Drop __grub_efi_api attribute from shim_lock->verify() call

... because (surprisingly) it uses System V AMD64 ABI on x86-64 architecture...

Fixes 6a080b9cd (efi: Add calling convention annotation to all prototypes)
---
 include/grub/efi/api.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/grub/efi/api.h +1 -1
index 16161e1f0..f3c50ed5d 100644
@@ -1777,7 +1777,7 @@ typedef struct grub_efi_block_io grub_efi_block_io_t;
 
 struct grub_efi_shim_lock_protocol
 {
-  grub_efi_status_t (__grub_efi_api *verify) (void *buffer, grub_uint32_t size);
+  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);
 };
 typedef struct grub_efi_shim_lock_protocol grub_efi_shim_lock_protocol_t;
 
Signature · 1 line
2.11.0
#2 lore raw

Re: [PATCH v2 1/2] efi: Drop __grub_efi_api attribute from shim_lock->verify() call

Quoted 16 lines · On Thu, 29 Jun 2023 20:02:31 +0200 Daniel Kiper <[email protected]> wrote:

... because (surprisingly) it uses System V AMD64 ABI on x86-64 architecture...

Fixes 6a080b9cd (efi: Add calling convention annotation to all prototypes)
---
 include/grub/efi/api.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/grub/efi/api.h +0 -0
index 16161e1f0..f3c50ed5d 100644

include/grub/efi/api.h typedef struct grub_efi_block_io grub_efi_block_io_t; · line 1780

Quoted 5 lines
include/grub/efi/api.h +1 -1
@@ -1777,7 +1777,7 @@ typedef struct grub_efi_block_io grub_efi_block_io_t;
 
 struct grub_efi_shim_lock_protocol
 {
-  grub_efi_status_t (__grub_efi_api *verify) (void *buffer, grub_uint32_t size);
+  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);

I would suggest adding a comment above this line, something like:

The shim_lock_protcol uses System V AMD64 ABI on x86-64 as specified in __insert specification reference here__, unlike most UEFI protocol methods, which use the MSABI calling convention.

I may have some details wrong in the above, so change as needed. Also, I'm confused by your commit message. If this is only on x86-64, what about the other EFI architectures? This change's contents suggest it applies to all of them. Or is the shim lock protocol itself limited to x86-64 architectures?

Glenn

Quoted 3 lines
include/grub/efi/api.h +0 -0
 };
 typedef struct grub_efi_shim_lock_protocol grub_efi_shim_lock_protocol_t;
 
#3 lore raw

Re: [PATCH v2 1/2] efi: Drop __grub_efi_api attribute from shim_lock->verify() call

Quoted 32 lines · Glenn Washburn
Quoted 16 lines · On Thu, 29 Jun 2023 20:02:31 +0200 Daniel Kiper <[email protected]> wrote:

... because (surprisingly) it uses System V AMD64 ABI on x86-64 architecture...

Fixes 6a080b9cd (efi: Add calling convention annotation to all prototypes)
---
 include/grub/efi/api.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/grub/efi/api.h +0 -0
index 16161e1f0..f3c50ed5d 100644

include/grub/efi/api.h typedef struct grub_efi_block_io grub_efi_block_io_t; · line 1780

Quoted 5 lines
include/grub/efi/api.h +1 -1
@@ -1777,7 +1777,7 @@ typedef struct grub_efi_block_io grub_efi_block_io_t;
 
 struct grub_efi_shim_lock_protocol
 {
-  grub_efi_status_t (__grub_efi_api *verify) (void *buffer, grub_uint32_t size);
+  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);

I would suggest adding a comment above this line, something like:

The shim_lock_protcol uses System V AMD64 ABI on x86-64 as specified in __insert specification reference here__, unlike most UEFI protocol methods, which use the MSABI calling convention.

Quoted 6 lines

I may have some details wrong in the above, so change as needed. Also, I'm confused by your commit message. If this is only on x86-64, what about the other EFI architectures? This change's contents suggest it applies to all of them. Or is the shim lock protocol itself limited to x86-64 architectures?

Conceptually, it applies to all architectures. However, x86_64 is the only architecture where the EFI calling convention deviates from the Linux/SysV calling convention, which is what GRUB uses internally.

For this reason, __grub_efi_api expands to an empty string on non-x86 architectures (on 32-bit x86, it expands to regparm(0) but this should be the default in any case)

#4 lore raw

Re: [PATCH v2 1/2] efi: Drop __grub_efi_api attribute from shim_lock->verify() call

In reply to Glenn Washburn
Quoted 31 lines · Glenn Washburn
Quoted 16 lines · On Thu, 29 Jun 2023 20:02:31 +0200 Daniel Kiper <[email protected]> wrote:

... because (surprisingly) it uses System V AMD64 ABI on x86-64 architecture...

Fixes 6a080b9cd (efi: Add calling convention annotation to all prototypes)
---
 include/grub/efi/api.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/grub/efi/api.h +0 -0
index 16161e1f0..f3c50ed5d 100644

include/grub/efi/api.h typedef struct grub_efi_block_io grub_efi_block_io_t; · line 1780

Quoted 5 lines
include/grub/efi/api.h +1 -1
@@ -1777,7 +1777,7 @@ typedef struct grub_efi_block_io grub_efi_block_io_t;
 
 struct grub_efi_shim_lock_protocol
 {
-  grub_efi_status_t (__grub_efi_api *verify) (void *buffer, grub_uint32_t size);
+  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);

I would suggest adding a comment above this line, something like:

The shim_lock_protcol uses System V AMD64 ABI on x86-64 as specified in __insert specification reference here__, unlike most UEFI protocol methods, which use the MSABI calling convention.

Quoted 5 lines

I may have some details wrong in the above, so change as needed. Also, I'm confused by your commit message. If this is only on x86-64, what about the other EFI architectures? This change's contents suggest it applies to all of them. Or is the shim lock protocol itself limited to x86-64 architectures?

I have added a comment and updated commit message as you suggested. Sadly I do not know a link to doc, except source code :-), which describes shim_lock->verify() calling convention. If we find one we can add it later...

Daniel

#5 lore raw

PATCH v2 2/2 efi: Fallback to legacy mode if shim is loaded on x86 archs

In reply to Daniel Kiper

The LoadImage() provided by the shim does not consult MOK when loading an image. So, simply signature verification fails when it should not. This means we cannot use Linux EFI stub to start the kernel when the shim is loaded. We have to fallback to legacy mode on x86 architectures. This is not possible on other architectures due to lack of legacy mode.

This is workaround which should disappear when the shim provides LoadImage() which looks up MOK during signature verification.

On the occasion align constants in include/grub/efi/sb.h.

---
 grub-core/kern/efi/sb.c      | 10 ++++++++++
 grub-core/loader/efi/linux.c | 17 +++++++++++++++++
 include/grub/efi/sb.h        |  5 ++++-
 3 files changed, 31 insertions(+), 1 deletion(-)
grub-core/kern/efi/sb.c +10 -0
index 80cfa0888..60550a6da 100644
@@ -32,6 +32,8 @@
 
 static grub_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID;
 
+static bool shim_lock_enabled = false;
+
 /*
  * Determine whether we're in secure boot mode.
  *
@@ -215,6 +217,14 @@ grub_shim_lock_verifier_setup (void)
   /* Enforce shim_lock_verifier. */
   grub_verifier_register (&shim_lock_verifier);
 
+  shim_lock_enabled = true;
+
   grub_env_set ("shim_lock", "y");
   grub_env_export ("shim_lock");
 }
+
+bool
+grub_is_shim_lock_enabled (void)
+{
+  return shim_lock_enabled;
+}
grub-core/loader/efi/linux.c +17 -0
index c1eef7c98..fb4d12e3f 100644
@@ -29,6 +29,7 @@
 #include <grub/efi/fdtload.h>
 #include <grub/efi/memory.h>
 #include <grub/efi/pe32.h>
+#include <grub/efi/sb.h>
 #include <grub/i18n.h>
 #include <grub/lib/cmdline.h>
 #include <grub/verify.h>
@@ -458,6 +459,22 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
 
   grub_dl_ref (my_mod);
 
+  if (grub_is_shim_lock_enabled () == true)
+    {
+#if defined(__i386__) || defined(__x86_64__)
+      grub_dprintf ("linux", "shim_lock enabled, falling back to legacy Linux kernel loader\n");
+
+      err = grub_cmd_linux_x86_legacy (cmd, argc, argv);
+
+      if (err == GRUB_ERR_NONE)
+	return GRUB_ERR_NONE;
+      else
+	goto fail;
+#else
+      grub_dprintf ("linux", "shim_lock enabled, trying Linux kernel EFI stub loader\n");
+#endif
+    }
+
   if (argc == 0)
     {
       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
include/grub/efi/sb.h +4 -1
index 30c4335bb..49a9ad01c 100644
@@ -22,7 +22,7 @@
 #include <grub/types.h>
 #include <grub/dl.h>
 
-#define GRUB_EFI_SECUREBOOT_MODE_UNSET	0
+#define GRUB_EFI_SECUREBOOT_MODE_UNSET		0
 #define GRUB_EFI_SECUREBOOT_MODE_UNKNOWN	1
 #define GRUB_EFI_SECUREBOOT_MODE_DISABLED	2
 #define GRUB_EFI_SECUREBOOT_MODE_ENABLED	3
@@ -31,6 +31,9 @@
 extern grub_uint8_t
 EXPORT_FUNC (grub_efi_get_secureboot) (void);
 
+extern bool
+EXPORT_FUNC (grub_is_shim_lock_enabled) (void);
+
 extern void
 grub_shim_lock_verifier_setup (void);
 #else
Signature · 1 line
2.11.0