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