nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0474-util-mkimage-Add-an-option-to-import-SBAT-metadata-i.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Peter Jones <pjones@redhat.com>
80913e
Date: Mon, 15 Feb 2021 17:07:00 +0100
80913e
Subject: [PATCH] util/mkimage: Add an option to import SBAT metadata into a
80913e
 .sbat section
80913e
80913e
Add a --sbat option to the grub-mkimage tool which allows us to import
80913e
an SBAT metadata formatted as a CSV file into a .sbat section of the
80913e
EFI binary.
80913e
80913e
Signed-off-by: Peter Jones <pjones@redhat.com>
80913e
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
---
80913e
 util/grub-install-common.c  |  2 +-
80913e
 util/grub-mkimage.c         | 15 ++++++++++++++-
80913e
 util/mkimage.c              | 43 ++++++++++++++++++++++++++++++++++++-------
80913e
 include/grub/util/install.h |  3 ++-
80913e
 include/grub/util/mkimage.h |  1 +
80913e
 docs/grub.texi              | 19 +++++++++++++++++++
80913e
 6 files changed, 73 insertions(+), 10 deletions(-)
80913e
80913e
diff --git a/util/grub-install-common.c b/util/grub-install-common.c
b32e65
index fa6b65347..fde4ca7fc 100644
80913e
--- a/util/grub-install-common.c
80913e
+++ b/util/grub-install-common.c
80913e
@@ -537,7 +537,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,
80913e
 			       pubkeys, npubkeys,
80913e
 			       x509keys, nx509keys,
80913e
 			       config_path, tgt,
80913e
-			       note, appsig_size, compression, dtb);
80913e
+			       note, appsig_size, compression, dtb, NULL);
80913e
   while (dc--)
80913e
     grub_install_pop_module ();
80913e
 }
80913e
diff --git a/util/grub-mkimage.c b/util/grub-mkimage.c
b32e65
index 394d2dc5f..17a86261f 100644
80913e
--- a/util/grub-mkimage.c
80913e
+++ b/util/grub-mkimage.c
80913e
@@ -82,6 +82,7 @@ static struct argp_option options[] = {
80913e
   {"output",  'o', N_("FILE"), 0, N_("output a generated image to FILE [default=stdout]"), 0},
80913e
   {"format",  'O', N_("FORMAT"), 0, 0, 0},
80913e
   {"compression",  'C', "(xz|none|auto)", 0, N_("choose the compression to use for core image"), 0},
80913e
+  {"sbat", 's', N_("FILE"), 0, N_("SBAT metadata"), 0},
80913e
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
80913e
   {"appended-signature-size", 'S', N_("SIZE"), 0, N_("Add a note segment reserving SIZE bytes for an appended signature"), 0},
80913e
   { 0, 0, 0, 0, 0, 0 }
80913e
@@ -127,6 +128,7 @@ struct arguments
80913e
   size_t nx509keys;
80913e
   char *font;
80913e
   char *config;
80913e
+  char *sbat;
80913e
   int note;
80913e
   size_t appsig_size;
80913e
   const struct grub_install_image_target_desc *image_target;
80913e
@@ -244,6 +246,13 @@ argp_parser (int key, char *arg, struct argp_state *state)
80913e
       arguments->prefix = xstrdup (arg);
80913e
       break;
80913e
 
80913e
+    case 's':
80913e
+      if (arguments->sbat)
80913e
+	free (arguments->sbat);
80913e
+
80913e
+      arguments->sbat = xstrdup (arg);
80913e
+      break;
80913e
+
80913e
     case 'v':
80913e
       verbosity++;
80913e
       break;
80913e
@@ -331,7 +340,8 @@ main (int argc, char *argv[])
80913e
 			       arguments.nx509keys, arguments.config,
80913e
 			       arguments.image_target, arguments.note,
80913e
 			       arguments.appsig_size,
80913e
-			       arguments.comp, arguments.dtb);
80913e
+			       arguments.comp, arguments.dtb,
80913e
+			       arguments.sbat);
80913e
 
80913e
   grub_util_file_sync  (fp);
80913e
   fclose (fp);
80913e
@@ -346,5 +356,8 @@ main (int argc, char *argv[])
80913e
   if (arguments.output)
80913e
     free (arguments.output);
80913e
 
80913e
+  if (arguments.sbat)
80913e
+    free (arguments.sbat);
80913e
+
80913e
   return 0;
80913e
 }
80913e
diff --git a/util/mkimage.c b/util/mkimage.c
b32e65
index 0f5ae2a76..16418e245 100644
80913e
--- a/util/mkimage.c
80913e
+++ b/util/mkimage.c
80913e
@@ -826,12 +826,13 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 			     char **x509key_paths, size_t nx509keys,
80913e
 			     char *config_path,
80913e
 			     const struct grub_install_image_target_desc *image_target,
80913e
-			     int note, size_t appsig_size, grub_compression_t comp, const char *dtb_path)
80913e
+			     int note, size_t appsig_size, grub_compression_t comp,
80913e
+			     const char *dtb_path, const char *sbat_path)
80913e
 {
80913e
   char *kernel_img, *core_img;
80913e
   size_t total_module_size, core_size;
80913e
   size_t memdisk_size = 0, config_size = 0;
80913e
-  size_t prefix_size = 0, dtb_size = 0;
80913e
+  size_t prefix_size = 0, dtb_size = 0, sbat_size = 0;
80913e
   char *kernel_path;
80913e
   size_t offset;
80913e
   struct grub_util_path_list *path_list, *p;
80913e
@@ -895,6 +896,9 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
       total_module_size += dtb_size + sizeof (struct grub_module_header);
80913e
     }
80913e
 
80913e
+  if (sbat_path != NULL && image_target->id != IMAGE_EFI)
80913e
+    grub_util_error (_(".sbat section can be embedded into EFI images only"));
80913e
+
80913e
   if (config_path)
80913e
     {
80913e
       config_size = ALIGN_ADDR (grub_util_get_image_size (config_path) + 1);
80913e
@@ -1277,8 +1281,9 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
       break;
80913e
     case IMAGE_EFI:
80913e
       {
80913e
-	char *pe_img, *header;
80913e
+	char *pe_img, *pe_sbat, *header;
80913e
 	struct grub_pe32_section_table *section;
80913e
+	size_t n_sections = 4;
80913e
 	size_t scn_size;
80913e
 	grub_uint32_t vma, raw_data;
80913e
 	size_t pe_size, header_size;
80913e
@@ -1293,8 +1298,15 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 	  header_size = EFI64_HEADER_SIZE;
80913e
 
80913e
 	vma = raw_data = header_size;
80913e
+
80913e
+	if (sbat_path != NULL)
80913e
+	  {
80913e
+	    sbat_size = ALIGN_ADDR (grub_util_get_image_size (sbat_path));
80913e
+	    sbat_size = ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT);
80913e
+	  }
80913e
+
80913e
 	pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
80913e
-          ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT);
80913e
+          ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT) + sbat_size;
80913e
 	header = pe_img = xcalloc (1, pe_size);
80913e
 
80913e
 	memcpy (pe_img + raw_data, core_img, core_size);
80913e
@@ -1309,7 +1321,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 					      + GRUB_PE32_SIGNATURE_SIZE);
80913e
 	c->machine = grub_host_to_target16 (image_target->pe_target);
80913e
 
80913e
-	c->num_sections = grub_host_to_target16 (4);
80913e
+	if (sbat_path != NULL)
80913e
+	  n_sections++;
80913e
+
80913e
+	c->num_sections = grub_host_to_target16 (n_sections);
80913e
 	c->time = grub_host_to_target32 (STABLE_EMBEDDING_TIMESTAMP);
80913e
 	c->characteristics = grub_host_to_target16 (GRUB_PE32_EXECUTABLE_IMAGE
80913e
 						    | GRUB_PE32_LINE_NUMS_STRIPPED
80913e
@@ -1371,7 +1386,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 				   GRUB_PE32_SCN_MEM_READ);
80913e
 
80913e
 	scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
80913e
-	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
80913e
+	/* ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT) is done earlier. */
80913e
+	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size + sbat_size +
80913e
 							       ALIGN_UP (total_module_size,
80913e
 									 GRUB_PE32_FILE_ALIGNMENT));
80913e
 
80913e
@@ -1382,7 +1398,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 				   GRUB_PE32_SCN_MEM_READ |
80913e
 				   GRUB_PE32_SCN_MEM_WRITE);
80913e
 
80913e
-	scn_size = pe_size - layout.reloc_size - raw_data;
80913e
+	scn_size = pe_size - layout.reloc_size - sbat_size - raw_data;
80913e
 	section = init_pe_section (image_target, section, "mods",
80913e
 				   &vma, scn_size, image_target->section_align,
80913e
 				   &raw_data, scn_size,
80913e
@@ -1390,6 +1406,19 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 				   GRUB_PE32_SCN_MEM_READ |
80913e
 				   GRUB_PE32_SCN_MEM_WRITE);
80913e
 
80913e
+	if (sbat_path != NULL)
80913e
+	  {
80913e
+	    pe_sbat = pe_img + raw_data;
80913e
+	    grub_util_load_image (sbat_path, pe_sbat);
80913e
+
80913e
+	    section = init_pe_section (image_target, section, ".sbat",
80913e
+				       &vma, sbat_size,
80913e
+				       image_target->section_align,
80913e
+				       &raw_data, sbat_size,
80913e
+				       GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
80913e
+				       GRUB_PE32_SCN_MEM_READ);
80913e
+	  }
80913e
+
80913e
 	scn_size = layout.reloc_size;
80913e
 	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
80913e
 	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
80913e
diff --git a/include/grub/util/install.h b/include/grub/util/install.h
b32e65
index 95059285b..dad17561c 100644
80913e
--- a/include/grub/util/install.h
80913e
+++ b/include/grub/util/install.h
80913e
@@ -187,7 +187,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 			     char *config_path,
80913e
 			     const struct grub_install_image_target_desc *image_target,
80913e
 			     int note, size_t appsig_size,
80913e
-			     grub_compression_t comp, const char *dtb_file);
80913e
+			     grub_compression_t comp, const char *dtb_file,
80913e
+			     const char *sbat_path);
80913e
 
80913e
 const struct grub_install_image_target_desc *
80913e
 grub_install_get_image_target (const char *arg);
80913e
diff --git a/include/grub/util/mkimage.h b/include/grub/util/mkimage.h
b32e65
index cef7fffa7..f48d544c2 100644
80913e
--- a/include/grub/util/mkimage.h
80913e
+++ b/include/grub/util/mkimage.h
80913e
@@ -24,6 +24,7 @@ struct grub_mkimage_layout
80913e
   size_t exec_size;
80913e
   size_t kernel_size;
80913e
   size_t bss_size;
80913e
+  size_t sbat_size;
80913e
   grub_uint64_t start_address;
80913e
   void *reloc_section;
80913e
   size_t reloc_size;
80913e
diff --git a/docs/grub.texi b/docs/grub.texi
b32e65
index 314bbeb84..52e6e5763 100644
80913e
--- a/docs/grub.texi
80913e
+++ b/docs/grub.texi
80913e
@@ -5719,6 +5719,7 @@ environment variables and commands are listed in the same order.
80913e
 * Using GPG-style digital signatures:: Booting digitally signed code
80913e
 * Using appended signatures::          An alternative approach to booting digitally signed code
80913e
 * Signing GRUB itself::                Ensuring the integrity of the GRUB core image
80913e
+* Secure Boot Advanced Targeting::   Embedded information for generation number based revocation
80913e
 * Lockdown::                           Lockdown when booting on a secure setup
80913e
 @end menu
80913e
 
80913e
@@ -6010,6 +6011,24 @@ As with UEFI secure boot, it is necessary to build in the required modules,
80913e
 or sign them separately.
80913e
 
80913e
 
80913e
+@node Secure Boot Advanced Targeting
80913e
+@section Embedded information for generation number based revocation
80913e
+
80913e
+The Secure Boot Advanced Targeting (SBAT) is a mechanism to allow the revocation
80913e
+of components in the boot path by using generation numbers embedded into the EFI
80913e
+binaries. The SBAT metadata is located in an .sbat data section that has set of
80913e
+UTF-8 strings as comma-separated values (CSV). See
80913e
+@uref{https://github.com/rhboot/shim/blob/main/SBAT.md} for more details.
80913e
+
80913e
+To add a data section containing the SBAT information into the binary, the
80913e
+@option{--sbat} option of @command{grub-mkimage} command should be used. The content
80913e
+of a CSV file, encoded with UTF-8, is copied as is to the .sbat data section into
80913e
+the generated EFI binary. The CSV file can be stored anywhere on the file system.
80913e
+
80913e
+@example
80913e
+grub-mkimage -O x86_64-efi -o grubx64.efi -p '(tftp)/grub' --sbat sbat.csv efinet tftp
80913e
+@end example
80913e
+
80913e
 @node Lockdown
80913e
 @section Lockdown when booting on a secure setup
80913e