Skip to contentSkip to conversation

GRUB: Supporting Secure Boot of xen

PATCH New Ross Lagerwall via Grub-devel 1 participant 4 messages Mar 28, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover GRUB: Supporting Secure Boot of xen

This patch series implements support for loading and verifying a signed xen binary. This would allow the same xen binary to be used for BIOS boot, UEFI boot, and UEFI boot with Secure Boot verification. There is an accompanying Xen patch series.

The first patch updates the multiboot2 specification to allow PE binaries.

Patch 2 implements the spec change.
Patch 3 allows loading multiboot modules without verification since the multiboot kernel is expected to verify them if needed.

Ross

Changed in v2:

* Use magic numbers to determine whether to load as ELF or PE rather than introducing a load type tag.
* Drop the 64 bit entry address extensions to the protocol - this is no longer necessary after some Xen build changes.
* Dropped the patch to allow verification after compression since it is not critical to make this work.

Diff stat for patch 1 (to be applied to the "multiboot2" branch):

Ross Lagerwall (1):
multiboot2: Add support for the PE binary type

 doc/multiboot.texi | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

Diff stat for patch 2 and 3 (to be applied to the "master" branch):

Ross Lagerwall (2):
multiboot2: Add PE load support
efi: Allow loading multiboot modules without verification

 grub-core/Makefile.core.def       |   1 +
 grub-core/kern/efi/sb.c           |   1 +
 grub-core/loader/multiboot.c      |   7 +
 grub-core/loader/multiboot_mbi2.c |  11 +-
 grub-core/loader/multiboot_pe.c   | 702 ++++++++++++++++++++++++++++++
 include/grub/efi/pe32.h           |  64 +++
 include/grub/multiboot.h          |   3 +
 include/grub/multiboot2.h         |   9 +
 8 files changed, 797 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/loader/multiboot_pe.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 multiboot2: Add support for the PE binary type

Currently, multiboot2-compatible bootloaders can load ELF binaries and a.out binaries. The presence of the address header tag determines how the bootloader tries to interpret the binary (a.out if the address tag is present else ELF).

In addition to the existing address and ELF load types, specify that boot loaders may optionally support PE binaries as well. This new type is a useful addition since PE binaries can be signed and verified (i.e. used with Secure Boot). Boot loaders can distinguish between ELF and PE binaries using magic numbers since it is not possible for a binary to be both an ELF and PE binary at the same time.

---
 doc/multiboot.texi | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
doc/multiboot.texi +11 -8
index df8a0d056e76..d26c35b54cd4 100644
@@ -510,12 +510,15 @@ assumes that no bss segment is present.
 @end table
 
 Note: This information does not need to be provided if the kernel image
-is in @sc{elf} format, but it must be provided if the image is in a.out
-format or in some other format. When the address tag is present it must
-be used in order to load the image, regardless of whether an @sc{elf}
-header is also present. Compliant boot loaders must be able to load
-images that are either in @sc{elf} format or contain the address tag
-embedded in the Multiboot2 header.
+is in @sc{elf} or @sc{PE} format, but it must be provided if the image
+is in a.out format or in some other format. When the address tag is
+present it must be used in order to load the image, regardless of
+whether an @sc{elf} or @sc{PE} header is also present. Compliant boot
+loaders must be able to load images that are either in @sc{elf} format
+or contain the address tag embedded in the Multiboot2 header. Compliant
+boot loaders may optionally support loading images in @sc{PE} format.
+When the address tag is not present, the boot loader should use magic
+numbers to identify the type of the image to determine how to load it.
 
 @subsection The entry address tag of Multiboot2 header
 
@@ -565,7 +568,7 @@ start running EFI i386 compatible operating system code.
 
 This tag is taken into account only on EFI i386 platforms
 when Multiboot2 image header contains EFI boot services tag.
-Then entry point specified in ELF header and the entry address
+Then entry point specified in ELF or PE header and the entry address
 tag of Multiboot2 header are ignored.
 
 @subsection EFI amd64 entry address tag of Multiboot2 header
@@ -597,7 +600,7 @@ start running EFI amd64 compatible operating system code.
 
 This tag is taken into account only on EFI amd64 platforms
 when Multiboot2 image header contains EFI boot services tag.
-Then entry point specified in ELF header and the entry address
+Then entry point specified in ELF or PE header and the entry address
 tag of Multiboot2 header are ignored.
 
 @node Console header tags
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/3 multiboot2: Add PE load support

In reply to Ross Lagerwall via Grub-devel

Add the ability to load multiboot binaries in PE format. This allows the binaries to be signed and verified.

---
 grub-core/Makefile.core.def       |   1 +
 grub-core/loader/multiboot.c      |   7 +
 grub-core/loader/multiboot_mbi2.c |  11 +-
 grub-core/loader/multiboot_pe.c   | 702 ++++++++++++++++++++++++++++++
 include/grub/efi/pe32.h           |  64 +++
 include/grub/multiboot.h          |   3 +
 include/grub/multiboot2.h         |   9 +
 7 files changed, 796 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/loader/multiboot_pe.c
grub-core/Makefile.core.def +1 -0
index 1571421d7e84..34697ba58171 100644
@@ -1815,6 +1815,7 @@ module = {
 
   common = loader/multiboot.c;
   common = loader/multiboot_mbi2.c;
+  common = loader/multiboot_pe.c;
   enable = x86;
   enable = i386_xen_pvh;
   enable = mips;
grub-core/loader/multiboot.c +7 -0
index 94be512c4d0c..8220a74e6f19 100644
@@ -220,6 +220,13 @@ static grub_uint64_t highest_load;
 #include "multiboot_elfxx.c"
 #undef MULTIBOOT_LOAD_ELF32
 
+bool
+GRUB_MULTIBOOT (is_elf) (mbi_load_data_t *mld)
+{
+  return grub_multiboot_is_elf32 (mld->buffer) ||
+         grub_multiboot_is_elf64 (mld->buffer);
+}
+
 /* Load ELF32 or ELF64.  */
 grub_err_t
 GRUB_MULTIBOOT (load_elf) (mbi_load_data_t *mld)
grub-core/loader/multiboot_mbi2.c +10 -1
index 00a48413c013..601f38161e11 100644
@@ -349,7 +349,16 @@ grub_multiboot2_load (grub_file_t file, const char *filename)
       mld.file = file;
       mld.filename = filename;
       mld.avoid_efi_boot_services = keep_bs;
-      err = grub_multiboot2_load_elf (&mld);
+      if (grub_multiboot2_is_pe (&mld))
+          err = grub_multiboot2_load_pe (&mld);
+      else if (grub_multiboot2_is_elf (&mld))
+          err = grub_multiboot2_load_elf (&mld);
+      else
+        {
+          grub_free (mld.buffer);
+          return grub_error (GRUB_ERR_UNKNOWN_OS,
+                             "Unknown image type and address tag not specified");
+        }
       if (err)
 	{
 	  grub_free (mld.buffer);
grub-core/loader/multiboot_pe.c new file +702 -0

702 lines not on this page · raw message

include/grub/efi/pe32.h +64 -0

86 lines not on this page · raw message

include/grub/multiboot.h +3 -0
index d8847f7531d3..c6af5c71b4e8 100644
@@ -100,6 +100,9 @@ struct mbi_load_data
 };
 typedef struct mbi_load_data mbi_load_data_t;
 
+bool
+grub_multiboot_is_elf (mbi_load_data_t *mld);
+
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (mbi_load_data_t *mld);
include/grub/multiboot2.h +9 -0
index b90aa6989674..6169cb4472a7 100644
@@ -92,10 +92,19 @@ struct mbi_load_data
 };
 typedef struct mbi_load_data mbi_load_data_t;
 
+bool
+grub_multiboot2_is_elf (mbi_load_data_t *mld);
+
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot2_load_elf (mbi_load_data_t *mld);
 
+grub_err_t
+grub_multiboot2_load_pe (mbi_load_data_t *mld);
+
+bool
+grub_multiboot2_is_pe (mbi_load_data_t *mld);
+
 extern grub_size_t grub_multiboot2_pure_size;
 extern grub_size_t grub_multiboot2_alloc_mbi;
 extern grub_uint32_t grub_multiboot2_payload_eip;
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/3 efi: Allow loading multiboot modules without verification

In reply to Ross Lagerwall via Grub-devel

GRUB doesn't do anything with multiboot modules except loading them and passing a pointer to the multiboot kernel. Therefore GRUB itself doesn't need to verify the module. Multiboot modules may contain code that needs to be verified. If this is the case, the expectation is that the multiboot kernel verifies the modules. For example, with Xen, the first multiboot module contains the dom0 kernel binary and Xen verifies it before starting it.

---
 grub-core/kern/efi/sb.c | 1 +
 1 file changed, 1 insertion(+)
grub-core/kern/efi/sb.c +1 -0
index 8d3e413608bb..f76290d65e9f 100644
@@ -171,6 +171,7 @@ shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
     case GRUB_FILE_TYPE_LOADENV:
     case GRUB_FILE_TYPE_SAVEENV:
     case GRUB_FILE_TYPE_VERIFY_SIGNATURE:
+    case GRUB_FILE_TYPE_MULTIBOOT_MODULE:
       *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
       return GRUB_ERR_NONE;
 
Signature · 1 line
2.43.0