nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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