nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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