Skip to contentSkip to conversation

Wrap font files into PE files

RFC PATCH In review Zhang Boyang 5 participants 9 messages Dec 5, 2022 – Dec 7, 2022 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Wrap font files into PE files

Hi,

This patchset introduces a utility: grub-wrap. It can wrap a non-PE file as a PE file, which can be subsequently digitally signed (and verified by shim). The wrapped file will be unwrapped at runtime automatically and transparently by the unwrap filter.

With grub-wrap, a PF2 font file can be wrapped and digitally signed. The name of PE section must be .GRUBpf2, to clearly identifies such PE file is a wrapper of a PF2 font file.

Example usage:

# generate a MOK key for signing
pesign --certficate 'MyKey' --export-cert MyKey.crt
# import the new MOK key
mokutil --import MyKey.crt 
reboot
# wrap unicode.pf2 as unicode.pf2.dll
grub-wrap --input unicode.pf2 --output unicode.pf2.dll --name .GRUBpf2 -O x86_64-efi
# sign the wrapper with MOK key
pesign --in unicode.pf2.dll --out /boot/grub/fonts/unicode.pf2 --force --certficate 'MyKey' --sign

There is one problem: those wrappers mustn't be accepted by other PE loaders. To distinguish these wrappers from normal EFI applications, I decided to make these wrappers looks like plain Windows resource-only DLLs (no entry point, and subsystem tag is set to Windows GUI instead of EFI Application). Currently, shim, tianocore, and my patched GRUB will reject these wrappers while trying to use them as executables. However, I'm not sure whether in-market UEFI firmwares will reject them. So I think the best workaround is to forbid keys available in firmware from signing these wrappers, and only use keys in shim to sign wrappers (this seems like exactly what we are doing for GRUB itself).

Best Regards,
Zhang Boyang

#1 lore raw

RFC PATCH 1/4 util/grub-wrap: New tool to wrap a file as a PE image

This new tool can wrap a non-PE file as a PE image, which can be subsequently digitally signed (and verified by shim).

It's important that this type of PE images must be rejected when trying to execute them. This is done by making them look like plain Windows DLLs instead of UEFI images. Major differences between these wrappers and normal UEFI images are:

* DLL flag is set (nobody cares)
* Entry point is set to 0 (shim rejects this)
* Subsystem is set to Windows GUI instead of EFI Application (tianocore rejects this)

---
 .gitignore                  |   2 +
 Makefile.util.def           |  20 +++
 docs/man/grub-wrap.h2m      |   4 +
 include/grub/efi/pe32.h     |   4 +
 include/grub/util/install.h |   3 +-
 util/grub-install-common.c  |   2 +-
 util/grub-mkimage.c         |   3 +-
 util/grub-wrap.c            | 160 ++++++++++++++++++++++++
 util/mkimage.c              | 239 ++++++++++++++++++++++--------------
 9 files changed, 341 insertions(+), 96 deletions(-)
 create mode 100644 docs/man/grub-wrap.h2m
 create mode 100644 util/grub-wrap.c
.gitignore +2 -0
index f6a1bd051..e2802578e 100644
@@ -179,6 +179,8 @@ widthspec.bin
 /grub-sparc64-setup.exe
 /grub-syslinux2cfg
 /grub-syslinux2cfg.exe
+/grub-wrap
+/grub-wrap.exe
 /grub_cmd_date
 /grub_cmd_echo
 /grub_cmd_regexp
Makefile.util.def +20 -0
index d919c562c..c02632fbd 100644
@@ -1385,3 +1385,23 @@ program = {
   ldadd = grub-core/lib/gnulib/libgnu.a;
   ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
 };
+
+program = {
+  name = grub-wrap;
+  mansection = 1;
+
+  common = util/grub-wrap.c;
+  common = util/mkimage.c;
+  common = util/grub-mkimage32.c;
+  common = util/grub-mkimage64.c;
+  common = util/resolve.c;
+  common = grub-core/kern/emu/argp_common.c;
+  common = grub-core/osdep/init.c;
+
+  ldadd = '$(LIBLZMA)';
+  ldadd = libgrubmods.a;
+  ldadd = libgrubgcry.a;
+  ldadd = libgrubkern.a;
+  ldadd = grub-core/lib/gnulib/libgnu.a;
+  ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+};
docs/man/grub-wrap.h2m new file +4 -0
new file mode 100644
index 000000000..f9d44fcd7
@@ -0,0 +1,4 @@
+[NAME]
+grub-wrap \- Wrap a file as a PE image
+[DESCRIPTION]
+grub-wrap wraps a file as a PE image.
include/grub/efi/pe32.h +4 -0
index 98c4ff177..7609b5198 100644
@@ -227,8 +227,12 @@ struct grub_pe64_optional_header
 #define GRUB_PE32_PE32_MAGIC	0x10b
 #define GRUB_PE32_PE64_MAGIC	0x20b
 
+#define GRUB_PE32_SUBSYSTEM_WINDOWS_GUI		2
 #define GRUB_PE32_SUBSYSTEM_EFI_APPLICATION	10
 
+#define GRUB_PE32_DLLCHARACTERISTICS_DYNAMIC_BASE	0x0040
+#define GRUB_PE32_DLLCHARACTERISTICS_NX_COMPAT		0x0100
+
 #define GRUB_PE32_NUM_DATA_DIRECTORIES	16
 
 struct grub_pe32_section_table
include/grub/util/install.h +2 -1
index 7d7445af9..eac34abaf 100644
@@ -190,7 +190,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
 			     const struct grub_install_image_target_desc *image_target,
 			     int note,
 			     grub_compression_t comp, const char *dtb_file,
-			     const char *sbat_path, const int disable_shim_lock);
+			     const char *sbat_path, const int disable_shim_lock,
+			     const char *wrap_path, const char *wrap_section);
 
 const struct grub_install_image_target_desc *
 grub_install_get_image_target (const char *arg);
util/grub-install-common.c +1 -1
index e45fb2903..c64a061b7 100644
@@ -681,7 +681,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,
 			       modules.entries, memdisk_path,
 			       pubkeys, npubkeys, config_path, tgt,
 			       note, compression, dtb, sbat,
-			       disable_shim_lock);
+			       disable_shim_lock, NULL, NULL);
   while (dc--)
     grub_install_pop_module ();
 }
util/grub-mkimage.c +2 -1
index c0d559937..51aebb40e 100644
@@ -325,7 +325,8 @@ main (int argc, char *argv[])
 			       arguments.npubkeys, arguments.config,
 			       arguments.image_target, arguments.note,
 			       arguments.comp, arguments.dtb,
-			       arguments.sbat, arguments.disable_shim_lock);
+			       arguments.sbat, arguments.disable_shim_lock,
+			       NULL, NULL);
 
   if (grub_util_file_sync (fp) < 0)
     grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",
util/grub-wrap.c new file +160 -0

160 lines not on this page · raw message

util/mkimage.c +146 -93

376 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

RFC PATCH 2/4 kern/unwrap: File filter to unwrap files wrapped by grub-wrap

In reply to Zhang Boyang

This file filter can unwrap a file wrapped by grub-wrap. This process is done automatically and transparently, thus no additional code is required at user's side.

First, it checks for file type flags and ignore irrelevant files. Then, it find target PE section and unwraps the file. Subsequent read operations will operate on unwrapped data. If error occurred during unwrapping, e.g. file is not wrapped, it passthroughs original file.

Currently, this filter only operate on font files (GRUB_FILE_TYPE_FONT), and they must be wrapped with section name set to ".GRUBpf2".

---
 grub-core/Makefile.core.def |   1 +
 grub-core/kern/main.c       |   3 +
 grub-core/kern/unwrap.c     | 188 ++++++++++++++++++++++++++++++++++++
 include/grub/efi/pe32.h     |  75 ++++++++++++++
 include/grub/file.h         |   1 +
 include/grub/unwrap.h       |  25 +++++
 6 files changed, 293 insertions(+)
 create mode 100644 grub-core/kern/unwrap.c
 create mode 100644 include/grub/unwrap.h
grub-core/Makefile.core.def +1 -0
index 95942fc8c..fb6a25ed1 100644
@@ -148,6 +148,7 @@ kernel = {
   common = kern/rescue_reader.c;
   common = kern/term.c;
   common = kern/verifiers.c;
+  common = kern/unwrap.c;
 
   noemu = kern/compiler-rt.c;
   noemu = kern/mm.c;
grub-core/kern/main.c +3 -0
index 731c07c29..fa08c0086 100644
@@ -30,6 +30,7 @@
 #include <grub/reader.h>
 #include <grub/parser.h>
 #include <grub/verify.h>
+#include <grub/unwrap.h>
 
 #ifdef GRUB_MACHINE_PCBIOS
 #include <grub/machine/memory.h>
@@ -281,6 +282,8 @@ grub_main (void)
   /* Init verifiers API. */
   grub_verifiers_init ();
 
+  grub_unwrap_init ();
+
   grub_load_config ();
 
   grub_boot_time ("Before loading embedded modules.");
grub-core/kern/unwrap.c new file +188 -0

188 lines not on this page · raw message

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

94 lines not on this page · raw message

include/grub/file.h +1 -0
index a5bf3a792..5fae2a8fc 100644
@@ -183,6 +183,7 @@ extern grub_disk_read_hook_t EXPORT_VAR(grub_file_progress_hook);
 typedef enum grub_file_filter_id
   {
     GRUB_FILE_FILTER_VERIFY,
+    GRUB_FILE_FILTER_UNWRAP,
     GRUB_FILE_FILTER_GZIO,
     GRUB_FILE_FILTER_XZIO,
     GRUB_FILE_FILTER_LZOPIO,
include/grub/unwrap.h new file +25 -0
new file mode 100644
index 000000000..c5155fc6d
@@ -0,0 +1,25 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_UNWRAP_HEADER
+#define GRUB_UNWRAP_HEADER 1
+
+extern void
+grub_unwrap_init (void);
+
+#endif /* ! GRUB_UNWRAP_HEADER */
Signature · 1 line
2.30.2
#3 lore raw

RFC PATCH 3/4 kern/efi/sb: Set requirements for PE images

In reply to Zhang Boyang

There is a problem in shim lock verifier after PE wrappers created by grub-wrap are introduced. The shim lock verifier always says a file is ok as long as shim says that file is ok, so there is a possibility that wrappers may be used as kernel images, leading to crashes or vulnerabilities.

This patch fixes the problem by introducing PE requirements checks to shim lock verifier. For kernel images, it now only says ok to files marked as EFI Application (wrappers are marked as Windows GUI).

---
 grub-core/kern/efi/sb.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
grub-core/kern/efi/sb.c +35 -2
index db42c2539..4fb751d8a 100644
@@ -113,12 +113,41 @@ grub_efi_get_secureboot (void)
   return secureboot;
 }
 
+struct pe_requirements {
+  grub_uint16_t subsystem;
+};
+
+static bool verify_pe_requirements (void *buf, grub_size_t size,
+				   struct pe_requirements *constraints)
+{
+  grub_uint32_t pe_image_header;
+  struct grub_pe_image_header *pe_hdr;
+  grub_uint16_t subsystem;
+
+  pe_image_header = grub_pe32_get_pe_image_header (buf, size);
+  if (pe_image_header == 0)
+    return false;
+  pe_hdr = (void *) ((char *) buf + pe_image_header);
+
+  grub_memcpy (&subsystem, &pe_hdr->optional_header.subsystem, sizeof (subsystem));
+  subsystem = grub_le_to_cpu16 (subsystem);
+  if (subsystem != constraints->subsystem)
+    return false;
+
+  return true;
+}
+
+static struct pe_requirements kernel_pe_requirements = {
+  .subsystem = GRUB_PE32_SUBSYSTEM_EFI_APPLICATION,
+};
+
 static grub_err_t
 shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
 			 enum grub_file_type type,
-			 void **context __attribute__ ((unused)),
+			 void **context,
 			 enum grub_verify_flags *flags)
 {
+  *context = NULL;
   *flags = GRUB_VERIFY_FLAGS_NONE;
 
   switch (type & GRUB_FILE_TYPE_MASK)
@@ -130,6 +159,7 @@ shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
     case GRUB_FILE_TYPE_XNU_KERNEL:
     case GRUB_FILE_TYPE_PLAN9_KERNEL:
     case GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE:
+      *context = &kernel_pe_requirements;
       *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
       return GRUB_ERR_NONE;
 
@@ -171,7 +201,7 @@ shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
 }
 
 static grub_err_t
-shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, grub_size_t size)
+shim_lock_verifier_write (void *context, void *buf, grub_size_t size)
 {
   grub_efi_shim_lock_protocol_t *sl = grub_efi_locate_protocol (&shim_lock_guid, 0);
 
@@ -181,6 +211,9 @@ shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, gru
   if (sl->verify (buf, size) != GRUB_EFI_SUCCESS)
     return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad shim signature"));
 
+  if (!verify_pe_requirements (buf, size, context))
+    return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("bad file type"));
+
   return GRUB_ERR_NONE;
 }
 
Signature · 1 line
2.30.2
#4 lore raw

RFC PATCH 4/4 kern/efi/sb: Use shim to verify font files

In reply to Zhang Boyang

Since font files can be wrapped as PE images by grub-wrap, use shim to verify font files if Secure Boot is enabled. To prevent other PE files (e.g. kernel images) used as wrappers, it only allows files marked as Windows GUI used as wrappers.

---
 grub-core/kern/efi/sb.c | 9 +++++++++
 1 file changed, 9 insertions(+)
grub-core/kern/efi/sb.c +9 -0
index 4fb751d8a..a86763232 100644
@@ -141,6 +141,10 @@ static struct pe_requirements kernel_pe_requirements = {
   .subsystem = GRUB_PE32_SUBSYSTEM_EFI_APPLICATION,
 };
 
+static struct pe_requirements wrapper_pe_requirements = {
+  .subsystem = GRUB_PE32_SUBSYSTEM_WINDOWS_GUI,
+};
+
 static grub_err_t
 shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
 			 enum grub_file_type type,
@@ -163,6 +167,11 @@ shim_lock_verifier_init (grub_file_t io __attribute__ ((unused)),
       *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
       return GRUB_ERR_NONE;
 
+    case GRUB_FILE_TYPE_FONT:
+      *context = &wrapper_pe_requirements;
+      *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
+      return GRUB_ERR_NONE;
+
     /* Files that do not affect secureboot state. */
     case GRUB_FILE_TYPE_NONE:
     case GRUB_FILE_TYPE_LOOPBACK:
Signature · 1 line
2.30.2
#5 lore raw

Re: [RFC PATCH 4/4] kern/efi/sb: Use shim to verify font files

Quoted 4 lines · Zhang Boyang

Since font files can be wrapped as PE images by grub-wrap, use shim to verify font files if Secure Boot is enabled. To prevent other PE files (e.g. kernel images) used as wrappers, it only allows files marked as Windows GUI used as wrappers.

Thanks for writing this; it's helpful to have something concrete to look at.

This approach is very font-focused, and while I understand that given the discussion, I do still wonder if it wouldn't be better to make fonts an instance of modules. If fonts become instances of modules, and modules are wrapped into PE files, that not only seems cleaner but also gives us signed module support without baking those into the image.

What do you think?

Be well,
--Robbie

#6 lore raw

Re: [RFC PATCH 4/4] kern/efi/sb: Use shim to verify font files

Quoted 9 lines · Robbie Harwood
Quoted 4 lines · Zhang Boyang

Since font files can be wrapped as PE images by grub-wrap, use shim to verify font files if Secure Boot is enabled. To prevent other PE files (e.g. kernel images) used as wrappers, it only allows files marked as Windows GUI used as wrappers.

Thanks for writing this; it's helpful to have something concrete to look at.

Definitely!

Quoted 7 lines

This approach is very font-focused, and while I understand that given the discussion, I do still wonder if it wouldn't be better to make fonts an instance of modules. If fonts become instances of modules, and modules are wrapped into PE files, that not only seems cleaner but also gives us signed module support without baking those into the image.

What do you think?

Nod, that probably makes more sense if we want to go this way. I'm not sure we do personally, but...

Signature · 3 lines
Steve McIntyre, Cambridge, UK.                                [email protected]
Dance like no one's watching. Encrypt like everyone is.
 - @torproject
#7 lore raw

Re: [RFC PATCH 4/4] kern/efi/sb: Use shim to verify font files

In reply to Robbie Harwood
Quoted 7 lines · Robbie Harwood
Quoted 4 lines · Zhang Boyang

Since font files can be wrapped as PE images by grub-wrap, use shim to verify font files if Secure Boot is enabled. To prevent other PE files (e.g. kernel images) used as wrappers, it only allows files marked as Windows GUI used as wrappers.

Quoted 8 lines

Thanks for writing this; it's helpful to have something concrete to look at.

This approach is very font-focused, and while I understand that given the discussion, I do still wonder if it wouldn't be better to make fonts an instance of modules. If fonts become instances of modules, and modules are wrapped into PE files, that not only seems cleaner but also gives us signed module support without baking those into the image.

Why not just making the PE wrap applicable to all file types, be it font files, grub modules or even (static) initrd. Providing a solution to sign arbitrary data or binary with this PE envelope sounds to me a very attractive feature and worthwhile the extra miles. :)

Thanks,
Michael

Quoted 5 lines

What do you think?

Be well,
--Robbie

#8 lore raw

Re: [RFC PATCH 4/4] kern/efi/sb: Use shim to verify font files

Yes yes yes yes. Signed dtb in grub at last.

Quoted 38 lines · On Wed, 7 Dec 2022, 03:16 Michael Chang via Grub-devel, <[email protected]>…
Quoted 7 lines · Robbie Harwood
Quoted 4 lines · Zhang Boyang

Since font files can be wrapped as PE images by grub-wrap, use shim to verify font files if Secure Boot is enabled. To prevent other PE files (e.g. kernel images) used as wrappers, it only allows files marked as Windows GUI used as wrappers.

Quoted 8 lines

Thanks for writing this; it's helpful to have something concrete to look at.

This approach is very font-focused, and while I understand that given the discussion, I do still wonder if it wouldn't be better to make fonts an instance of modules. If fonts become instances of modules, and modules are wrapped into PE files, that not only seems cleaner but also gives us signed module support without baking those into the image.

Why not just making the PE wrap applicable to all file types, be it font files, grub modules or even (static) initrd. Providing a solution to sign arbitrary data or binary with this PE envelope sounds to me a very attractive feature and worthwhile the extra miles. :)

Thanks,
Michael

Quoted 5 lines

What do you think?

Be well,
--Robbie

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel