dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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