Blame SOURCES/0509-loader-efi-chainloader-Use-grub_loader_set_ex.patch

bf0270
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
bf0270
From: Chris Coulson <chris.coulson@canonical.com>
bf0270
Date: Fri, 29 Apr 2022 21:30:56 +0100
bf0270
Subject: [PATCH] loader/efi/chainloader: Use grub_loader_set_ex
bf0270
bf0270
This ports the EFI chainloader to use grub_loader_set_ex in order to fix
bf0270
a use-after-free bug that occurs when grub_cmd_chainloader is executed
bf0270
more than once before a boot attempt is performed.
bf0270
bf0270
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
bf0270
(cherry picked from commit 4b7f0402b7cb0f67a93be736f2b75b818d7f44c9)
bf0270
(cherry picked from commit fc1a79bf0e0bc019362ace46d908a92b48dcd55b)
bf0270
(cherry picked from commit f5b653dfe00271384ff7fbd82db926ab95dbd80e)
bf0270
[rharwood: context sludge from previous commit]
bf0270
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
bf0270
---
bf0270
 grub-core/loader/efi/chainloader.c | 38 ++++++++++++++++++++++----------------
bf0270
 1 file changed, 22 insertions(+), 16 deletions(-)
bf0270
bf0270
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
bf0270
index afeb1fc97e..720f6181e5 100644
bf0270
--- a/grub-core/loader/efi/chainloader.c
bf0270
+++ b/grub-core/loader/efi/chainloader.c
bf0270
@@ -47,8 +47,6 @@ GRUB_MOD_LICENSE ("GPLv3+");
bf0270
 
bf0270
 static grub_dl_t my_mod;
bf0270
 
bf0270
-static grub_efi_handle_t image_handle;
bf0270
-
bf0270
 struct grub_secureboot_chainloader_context {
bf0270
   grub_efi_physical_address_t address;
bf0270
   grub_efi_uintn_t pages;
bf0270
@@ -58,7 +56,6 @@ struct grub_secureboot_chainloader_context {
bf0270
   grub_ssize_t cmdline_len;
bf0270
   grub_efi_handle_t dev_handle;
bf0270
 };
bf0270
-static struct grub_secureboot_chainloader_context *sb_context;
bf0270
 
bf0270
 static grub_err_t
bf0270
 grub_start_image (grub_efi_handle_t handle)
bf0270
@@ -97,11 +94,14 @@ grub_start_image (grub_efi_handle_t handle)
bf0270
 }
bf0270
 
bf0270
 static grub_err_t
bf0270
-grub_chainloader_unload (void)
bf0270
+grub_chainloader_unload (void *context)
bf0270
 {
bf0270
+  grub_efi_handle_t image_handle;
bf0270
   grub_efi_loaded_image_t *loaded_image;
bf0270
   grub_efi_boot_services_t *b;
bf0270
 
bf0270
+  image_handle = (grub_efi_handle_t) context;
bf0270
+
bf0270
   loaded_image = grub_efi_get_loaded_image (image_handle);
bf0270
   if (loaded_image != NULL)
bf0270
     grub_free (loaded_image->load_options);
bf0270
@@ -114,10 +114,12 @@ grub_chainloader_unload (void)
bf0270
 }
bf0270
 
bf0270
 static grub_err_t
bf0270
-grub_chainloader_boot (void)
bf0270
+grub_chainloader_boot (void *context)
bf0270
 {
bf0270
+  grub_efi_handle_t image_handle;
bf0270
   grub_err_t err;
bf0270
 
bf0270
+  image_handle = (grub_efi_handle_t) context;
bf0270
   err = grub_start_image (image_handle);
bf0270
 
bf0270
   grub_loader_unset ();
bf0270
@@ -833,15 +835,17 @@ error_exit:
bf0270
 }
bf0270
 
bf0270
 static grub_err_t
bf0270
-grub_secureboot_chainloader_unload (void)
bf0270
+grub_secureboot_chainloader_unload (void *context)
bf0270
 {
bf0270
+  struct grub_secureboot_chainloader_context *sb_context;
bf0270
+
bf0270
+  sb_context = (struct grub_secureboot_chainloader_context *) context;
bf0270
+
bf0270
   grub_efi_free_pages (sb_context->address, sb_context->pages);
bf0270
   grub_free (sb_context->file_path);
bf0270
   grub_free (sb_context->cmdline);
bf0270
   grub_free (sb_context);
bf0270
 
bf0270
-  sb_context = 0;
bf0270
-
bf0270
   grub_dl_unref (my_mod);
bf0270
   return GRUB_ERR_NONE;
bf0270
 }
bf0270
@@ -890,12 +894,15 @@ grub_load_image(grub_efi_device_path_t *file_path, void *boot_image,
bf0270
 }
bf0270
 
bf0270
 static grub_err_t
bf0270
-grub_secureboot_chainloader_boot (void)
bf0270
+grub_secureboot_chainloader_boot (void *context)
bf0270
 {
bf0270
+  struct grub_secureboot_chainloader_context *sb_context;
bf0270
   grub_efi_boot_services_t *b;
bf0270
   int rc;
bf0270
   grub_efi_handle_t handle = 0;
bf0270
 
bf0270
+  sb_context = (struct grub_secureboot_chainloader_context *) context;
bf0270
+
bf0270
   rc = handle_image (sb_context);
bf0270
   if (rc == 0)
bf0270
     {
bf0270
@@ -936,6 +943,8 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
bf0270
   grub_efi_char16_t *cmdline = 0;
bf0270
   grub_ssize_t cmdline_len = 0;
bf0270
   grub_efi_handle_t dev_handle = 0;
bf0270
+  grub_efi_handle_t image_handle = 0;
bf0270
+  struct grub_secureboot_chainloader_context *sb_context = 0;
bf0270
 
bf0270
   if (argc == 0)
bf0270
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
bf0270
@@ -1108,8 +1117,8 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
bf0270
       grub_file_close (file);
bf0270
       grub_device_close (dev);
bf0270
 
bf0270
-      grub_loader_set (grub_secureboot_chainloader_boot,
bf0270
-		       grub_secureboot_chainloader_unload, 0);
bf0270
+      grub_loader_set_ex (grub_secureboot_chainloader_boot,
bf0270
+			  grub_secureboot_chainloader_unload, sb_context, 0);
bf0270
       return 0;
bf0270
     }
bf0270
   else if (rc == 0)
bf0270
@@ -1123,7 +1132,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
bf0270
       efi_call_2 (b->free_pages, address, pages);
bf0270
       grub_free (file_path);
bf0270
 
bf0270
-      grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
bf0270
+      grub_loader_set_ex (grub_chainloader_boot, grub_chainloader_unload, image_handle, 0);
bf0270
 
bf0270
       return 0;
bf0270
     }
bf0270
@@ -1145,10 +1154,7 @@ fail:
bf0270
     grub_free (cmdline);
bf0270
 
bf0270
   if (image_handle != 0)
bf0270
-    {
bf0270
-      efi_call_1 (b->unload_image, image_handle);
bf0270
-      image_handle = 0;
bf0270
-    }
bf0270
+    efi_call_1 (b->unload_image, image_handle);
bf0270
 
bf0270
   grub_dl_unref (my_mod);
bf0270