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

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