nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0507-loader-efi-chainloader-simplify-the-loader-state.patch

0ccc47
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
0ccc47
From: Chris Coulson <chris.coulson@canonical.com>
0ccc47
Date: Fri, 29 Apr 2022 21:13:08 +0100
0ccc47
Subject: [PATCH] loader/efi/chainloader: simplify the loader state
0ccc47
0ccc47
When not using the shim lock protocol, the chainloader command retains
0ccc47
the source buffer and device path passed to LoadImage, requiring the
0ccc47
unload hook passed to grub_loader_set to free them. It isn't required
0ccc47
to retain this state though - they aren't required by StartImage or
0ccc47
anything else in the boot hook, so clean them up before
0ccc47
grub_cmd_chainloader finishes.
0ccc47
0ccc47
This also wraps the loader state when using the shim lock protocol
0ccc47
inside a struct.
0ccc47
0ccc47
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
0ccc47
(cherry picked from commit fa39862933b3be1553a580a3a5c28073257d8046)
0ccc47
(cherry picked from commit 0333343ee99c4e88f062789263c94291c057251b)
0ccc47
[rharwood: verifying twice]
0ccc47
(cherry picked from commit 6080ad5d91d6a80d5f67c592dd33b6dd413e9453)
0ccc47
[rharwood: double frees and unintialized, context fuzz - orig_dp]
0ccc47
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
0ccc47
---
0ccc47
 grub-core/loader/efi/chainloader.c | 160 +++++++++++++++++++++++--------------
0ccc47
 1 file changed, 102 insertions(+), 58 deletions(-)
0ccc47
0ccc47
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
0ccc47
index d75d345003..afeb1fc97e 100644
0ccc47
--- a/grub-core/loader/efi/chainloader.c
0ccc47
+++ b/grub-core/loader/efi/chainloader.c
0ccc47
@@ -47,38 +47,21 @@ GRUB_MOD_LICENSE ("GPLv3+");
0ccc47
 
0ccc47
 static grub_dl_t my_mod;
0ccc47
 
0ccc47
-static grub_efi_physical_address_t address;
0ccc47
-static grub_efi_uintn_t pages;
0ccc47
-static grub_ssize_t fsize;
0ccc47
-static grub_efi_device_path_t *file_path;
0ccc47
 static grub_efi_handle_t image_handle;
0ccc47
-static grub_efi_char16_t *cmdline;
0ccc47
-static grub_ssize_t cmdline_len;
0ccc47
-static grub_efi_handle_t dev_handle;
0ccc47
 
0ccc47
-static grub_efi_status_t (*entry_point) (grub_efi_handle_t image_handle, grub_efi_system_table_t *system_table);
0ccc47
+struct grub_secureboot_chainloader_context {
0ccc47
+  grub_efi_physical_address_t address;
0ccc47
+  grub_efi_uintn_t pages;
0ccc47
+  grub_ssize_t fsize;
0ccc47
+  grub_efi_device_path_t *file_path;
0ccc47
+  grub_efi_char16_t *cmdline;
0ccc47
+  grub_ssize_t cmdline_len;
0ccc47
+  grub_efi_handle_t dev_handle;
0ccc47
+};
0ccc47
+static struct grub_secureboot_chainloader_context *sb_context;
0ccc47
 
0ccc47
 static grub_err_t
0ccc47
-grub_chainloader_unload (void)
0ccc47
-{
0ccc47
-  grub_efi_boot_services_t *b;
0ccc47
-
0ccc47
-  b = grub_efi_system_table->boot_services;
0ccc47
-  efi_call_1 (b->unload_image, image_handle);
0ccc47
-  grub_efi_free_pages (address, pages);
0ccc47
-
0ccc47
-  grub_free (file_path);
0ccc47
-  grub_free (cmdline);
0ccc47
-  cmdline = 0;
0ccc47
-  file_path = 0;
0ccc47
-  dev_handle = 0;
0ccc47
-
0ccc47
-  grub_dl_unref (my_mod);
0ccc47
-  return GRUB_ERR_NONE;
0ccc47
-}
0ccc47
-
0ccc47
-static grub_err_t
0ccc47
-grub_chainloader_boot (void)
0ccc47
+grub_start_image (grub_efi_handle_t handle)
0ccc47
 {
0ccc47
   grub_efi_boot_services_t *b;
0ccc47
   grub_efi_status_t status;
0ccc47
@@ -86,7 +69,7 @@ grub_chainloader_boot (void)
0ccc47
   grub_efi_char16_t *exit_data = NULL;
0ccc47
 
0ccc47
   b = grub_efi_system_table->boot_services;
0ccc47
-  status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
0ccc47
+  status = efi_call_3 (b->start_image, handle, &exit_data_size, &exit_data);
0ccc47
   if (status != GRUB_EFI_SUCCESS)
0ccc47
     {
0ccc47
       if (exit_data)
0ccc47
@@ -110,11 +93,37 @@ grub_chainloader_boot (void)
0ccc47
   if (exit_data)
0ccc47
     grub_efi_free_pool (exit_data);
0ccc47
 
0ccc47
-  grub_loader_unset ();
0ccc47
-
0ccc47
   return grub_errno;
0ccc47
 }
0ccc47
 
0ccc47
+static grub_err_t
0ccc47
+grub_chainloader_unload (void)
0ccc47
+{
0ccc47
+  grub_efi_loaded_image_t *loaded_image;
0ccc47
+  grub_efi_boot_services_t *b;
0ccc47
+
0ccc47
+  loaded_image = grub_efi_get_loaded_image (image_handle);
0ccc47
+  if (loaded_image != NULL)
0ccc47
+    grub_free (loaded_image->load_options);
0ccc47
+
0ccc47
+  b = grub_efi_system_table->boot_services;
0ccc47
+  efi_call_1 (b->unload_image, image_handle);
0ccc47
+
0ccc47
+  grub_dl_unref (my_mod);
0ccc47
+  return GRUB_ERR_NONE;
0ccc47
+}
0ccc47
+
0ccc47
+static grub_err_t
0ccc47
+grub_chainloader_boot (void)
0ccc47
+{
0ccc47
+  grub_err_t err;
0ccc47
+
0ccc47
+  err = grub_start_image (image_handle);
0ccc47
+
0ccc47
+  grub_loader_unset ();
0ccc47
+  return err;
0ccc47
+}
0ccc47
+
0ccc47
 static grub_err_t
0ccc47
 copy_file_path (grub_efi_file_path_device_path_t *fp,
0ccc47
 		const char *str, grub_efi_uint16_t len)
0ccc47
@@ -149,7 +158,7 @@ make_file_path (grub_efi_device_path_t *dp, const char *filename)
0ccc47
   char *dir_start;
0ccc47
   char *dir_end;
0ccc47
   grub_size_t size;
0ccc47
-  grub_efi_device_path_t *d;
0ccc47
+  grub_efi_device_path_t *d, *file_path;
0ccc47
 
0ccc47
   dir_start = grub_strchr (filename, ')');
0ccc47
   if (! dir_start)
0ccc47
@@ -520,10 +529,12 @@ grub_efi_get_media_file_path (grub_efi_device_path_t *dp)
0ccc47
 }
0ccc47
 
0ccc47
 static grub_efi_boolean_t
0ccc47
-handle_image (void *data, grub_efi_uint32_t datasize)
0ccc47
+handle_image (struct grub_secureboot_chainloader_context *load_context)
0ccc47
 {
0ccc47
   grub_efi_loaded_image_t *li, li_bak;
0ccc47
   grub_efi_status_t efi_status;
0ccc47
+  void *data = (void *)(unsigned long)load_context->address;
0ccc47
+  grub_efi_uint32_t datasize = load_context->fsize;
0ccc47
   void *buffer = NULL;
0ccc47
   char *buffer_aligned = NULL;
0ccc47
   grub_efi_uint32_t i;
0ccc47
@@ -534,6 +545,7 @@ handle_image (void *data, grub_efi_uint32_t datasize)
0ccc47
   grub_uint32_t buffer_size;
0ccc47
   int found_entry_point = 0;
0ccc47
   int rc;
0ccc47
+  grub_efi_status_t (*entry_point) (grub_efi_handle_t image_handle, grub_efi_system_table_t *system_table);
0ccc47
 
0ccc47
   rc = read_header (data, datasize, &context);
0ccc47
   if (rc < 0)
0ccc47
@@ -791,10 +803,10 @@ handle_image (void *data, grub_efi_uint32_t datasize)
0ccc47
   grub_memcpy (&li_bak, li, sizeof (grub_efi_loaded_image_t));
0ccc47
   li->image_base = buffer_aligned;
0ccc47
   li->image_size = context.image_size;
0ccc47
-  li->load_options = cmdline;
0ccc47
-  li->load_options_size = cmdline_len;
0ccc47
-  li->file_path = grub_efi_get_media_file_path (file_path);
0ccc47
-  li->device_handle = dev_handle;
0ccc47
+  li->load_options = load_context->cmdline;
0ccc47
+  li->load_options_size = load_context->cmdline_len;
0ccc47
+  li->file_path = grub_efi_get_media_file_path (load_context->file_path);
0ccc47
+  li->device_handle = load_context->dev_handle;
0ccc47
   if (!li->file_path)
0ccc47
     {
0ccc47
       grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching file path found");
0ccc47
@@ -823,19 +835,22 @@ error_exit:
0ccc47
 static grub_err_t
0ccc47
 grub_secureboot_chainloader_unload (void)
0ccc47
 {
0ccc47
-  grub_efi_free_pages (address, pages);
0ccc47
-  grub_free (file_path);
0ccc47
-  grub_free (cmdline);
0ccc47
-  cmdline = 0;
0ccc47
-  file_path = 0;
0ccc47
-  dev_handle = 0;
0ccc47
+  grub_efi_free_pages (sb_context->address, sb_context->pages);
0ccc47
+  grub_free (sb_context->file_path);
0ccc47
+  grub_free (sb_context->cmdline);
0ccc47
+  grub_free (sb_context);
0ccc47
+
0ccc47
+  sb_context = 0;
0ccc47
 
0ccc47
   grub_dl_unref (my_mod);
0ccc47
   return GRUB_ERR_NONE;
0ccc47
 }
0ccc47
 
0ccc47
 static grub_err_t
0ccc47
-grub_load_image(void *boot_image)
0ccc47
+grub_load_image(grub_efi_device_path_t *file_path, void *boot_image,
0ccc47
+		grub_efi_uintn_t image_size, grub_efi_handle_t dev_handle,
0ccc47
+		grub_efi_char16_t *cmdline, grub_ssize_t cmdline_len,
0ccc47
+		grub_efi_handle_t *image_handle_out)
0ccc47
 {
0ccc47
   grub_efi_boot_services_t *b;
0ccc47
   grub_efi_status_t status;
0ccc47
@@ -844,7 +859,7 @@ grub_load_image(void *boot_image)
0ccc47
   b = grub_efi_system_table->boot_services;
0ccc47
 
0ccc47
   status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
0ccc47
-		       boot_image, fsize, &image_handle);
0ccc47
+		       boot_image, image_size, image_handle_out);
0ccc47
   if (status != GRUB_EFI_SUCCESS)
0ccc47
     {
0ccc47
       if (status == GRUB_EFI_OUT_OF_RESOURCES)
0ccc47
@@ -857,7 +872,7 @@ grub_load_image(void *boot_image)
0ccc47
   /* LoadImage does not set a device handler when the image is
0ccc47
      loaded from memory, so it is necessary to set it explicitly here.
0ccc47
      This is a mess.  */
0ccc47
-  loaded_image = grub_efi_get_loaded_image (image_handle);
0ccc47
+  loaded_image = grub_efi_get_loaded_image (*image_handle_out);
0ccc47
   if (! loaded_image)
0ccc47
     {
0ccc47
       grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
0ccc47
@@ -879,20 +894,25 @@ grub_secureboot_chainloader_boot (void)
0ccc47
 {
0ccc47
   grub_efi_boot_services_t *b;
0ccc47
   int rc;
0ccc47
+  grub_efi_handle_t handle = 0;
0ccc47
 
0ccc47
-  rc = handle_image ((void *)(unsigned long)address, fsize);
0ccc47
+  rc = handle_image (sb_context);
0ccc47
   if (rc == 0)
0ccc47
     {
0ccc47
       /* We weren't able to attempt to execute the image, so fall back
0ccc47
        * to LoadImage / StartImage.
0ccc47
        */
0ccc47
-      rc = grub_load_image((void *)(unsigned long)address);
0ccc47
+      rc = grub_load_image(sb_context->file_path,
0ccc47
+			   (void *)(unsigned long)sb_context->address,
0ccc47
+			   sb_context->fsize, sb_context->dev_handle,
0ccc47
+			   sb_context->cmdline, sb_context->cmdline_len,
0ccc47
+			   &handle);
0ccc47
       if (rc == 0)
0ccc47
-        grub_chainloader_boot ();
0ccc47
+	grub_start_image (handle);
0ccc47
     }
0ccc47
 
0ccc47
   b = grub_efi_system_table->boot_services;
0ccc47
-  efi_call_1 (b->unload_image, image_handle);
0ccc47
+  efi_call_1 (b->unload_image, handle);
0ccc47
 
0ccc47
   grub_loader_unset ();
0ccc47
   return grub_errno;
0ccc47
@@ -906,10 +926,16 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
0ccc47
   grub_efi_status_t status;
0ccc47
   grub_efi_boot_services_t *b;
0ccc47
   grub_device_t dev = 0;
0ccc47
-  grub_efi_device_path_t *dp = 0;
0ccc47
+  grub_efi_device_path_t *dp = 0, *file_path = 0;
0ccc47
   char *filename;
0ccc47
   void *boot_image = 0;
0ccc47
   int rc;
0ccc47
+  grub_efi_physical_address_t address = 0;
0ccc47
+  grub_ssize_t fsize;
0ccc47
+  grub_efi_uintn_t pages = 0;
0ccc47
+  grub_efi_char16_t *cmdline = 0;
0ccc47
+  grub_ssize_t cmdline_len = 0;
0ccc47
+  grub_efi_handle_t dev_handle = 0;
0ccc47
 
0ccc47
   if (argc == 0)
0ccc47
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
0ccc47
@@ -917,12 +943,6 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
0ccc47
 
0ccc47
   grub_dl_ref (my_mod);
0ccc47
 
0ccc47
-  /* Initialize some global variables.  */
0ccc47
-  address = 0;
0ccc47
-  image_handle = 0;
0ccc47
-  file_path = 0;
0ccc47
-  dev_handle = 0;
0ccc47
-
0ccc47
   b = grub_efi_system_table->boot_services;
0ccc47
 
0ccc47
   if (argc > 1)
0ccc47
@@ -1074,17 +1094,35 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
0ccc47
   grub_dprintf ("chain", "linuxefi_secure_validate: %d\n", rc);
0ccc47
   if (rc > 0)
0ccc47
     {
0ccc47
+      sb_context = grub_malloc (sizeof (*sb_context));
0ccc47
+      if (sb_context == NULL)
0ccc47
+        goto fail;
0ccc47
+      sb_context->address = address;
0ccc47
+      sb_context->fsize = fsize;
0ccc47
+      sb_context->pages = pages;
0ccc47
+      sb_context->file_path = file_path;
0ccc47
+      sb_context->cmdline = cmdline;
0ccc47
+      sb_context->cmdline_len = cmdline_len;
0ccc47
+      sb_context->dev_handle = dev_handle;
0ccc47
+
0ccc47
       grub_file_close (file);
0ccc47
       grub_device_close (dev);
0ccc47
+
0ccc47
       grub_loader_set (grub_secureboot_chainloader_boot,
0ccc47
 		       grub_secureboot_chainloader_unload, 0);
0ccc47
       return 0;
0ccc47
     }
0ccc47
   else if (rc == 0)
0ccc47
     {
0ccc47
-      grub_load_image(boot_image);
0ccc47
+      grub_load_image(file_path, boot_image, fsize, dev_handle, cmdline,
0ccc47
+		      cmdline_len, &image_handle);
0ccc47
       grub_file_close (file);
0ccc47
       grub_device_close (dev);
0ccc47
+
0ccc47
+      /* We're finished with the source image buffer and file path now */
0ccc47
+      efi_call_2 (b->free_pages, address, pages);
0ccc47
+      grub_free (file_path);
0ccc47
+
0ccc47
       grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
0ccc47
 
0ccc47
       return 0;
0ccc47
@@ -1106,6 +1144,12 @@ fail:
0ccc47
   if (cmdline)
0ccc47
     grub_free (cmdline);
0ccc47
 
0ccc47
+  if (image_handle != 0)
0ccc47
+    {
0ccc47
+      efi_call_1 (b->unload_image, image_handle);
0ccc47
+      image_handle = 0;
0ccc47
+    }
0ccc47
+
0ccc47
   grub_dl_unref (my_mod);
0ccc47
 
0ccc47
   return grub_errno;