0003-loader-efi-chainloader-Simplify-the-loader-state.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. From dfdc742bdb22be468035f96cce0be5fee23b6df5 Mon Sep 17 00:00:00 2001
  2. From: Chris Coulson <chris.coulson@canonical.com>
  3. Date: Tue, 5 Apr 2022 10:02:04 +0100
  4. Subject: [PATCH] loader/efi/chainloader: Simplify the loader state
  5. The chainloader command retains the source buffer and device path passed
  6. to LoadImage(), requiring the unload hook passed to grub_loader_set() to
  7. free them. It isn't required to retain this state though - they aren't
  8. required by StartImage() or anything else in the boot hook, so clean them
  9. up before grub_cmd_chainloader() finishes.
  10. Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
  11. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  12. Upstream: 1469983ebb9674753ad333d37087fb8cb20e1dce
  13. [Thomas: needed to cherry-pick
  14. 04c86e0bb7b58fc2f913f798cdb18934933e532d which fixes CVE-2022-28736]
  15. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  16. ---
  17. grub-core/loader/efi/chainloader.c | 38 +++++++++++++++++-------------
  18. 1 file changed, 21 insertions(+), 17 deletions(-)
  19. diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
  20. index 2bd80f4db..d1602c89b 100644
  21. --- a/grub-core/loader/efi/chainloader.c
  22. +++ b/grub-core/loader/efi/chainloader.c
  23. @@ -44,25 +44,20 @@ GRUB_MOD_LICENSE ("GPLv3+");
  24. static grub_dl_t my_mod;
  25. -static grub_efi_physical_address_t address;
  26. -static grub_efi_uintn_t pages;
  27. -static grub_efi_device_path_t *file_path;
  28. static grub_efi_handle_t image_handle;
  29. -static grub_efi_char16_t *cmdline;
  30. static grub_err_t
  31. grub_chainloader_unload (void)
  32. {
  33. + grub_efi_loaded_image_t *loaded_image;
  34. grub_efi_boot_services_t *b;
  35. + loaded_image = grub_efi_get_loaded_image (image_handle);
  36. + if (loaded_image != NULL)
  37. + grub_free (loaded_image->load_options);
  38. +
  39. b = grub_efi_system_table->boot_services;
  40. efi_call_1 (b->unload_image, image_handle);
  41. - efi_call_2 (b->free_pages, address, pages);
  42. -
  43. - grub_free (file_path);
  44. - grub_free (cmdline);
  45. - cmdline = 0;
  46. - file_path = 0;
  47. grub_dl_unref (my_mod);
  48. return GRUB_ERR_NONE;
  49. @@ -140,7 +135,7 @@ make_file_path (grub_efi_device_path_t *dp, const char *filename)
  50. char *dir_start;
  51. char *dir_end;
  52. grub_size_t size;
  53. - grub_efi_device_path_t *d;
  54. + grub_efi_device_path_t *d, *file_path;
  55. dir_start = grub_strchr (filename, ')');
  56. if (! dir_start)
  57. @@ -222,11 +217,14 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
  58. grub_efi_status_t status;
  59. grub_efi_boot_services_t *b;
  60. grub_device_t dev = 0;
  61. - grub_efi_device_path_t *dp = 0;
  62. + grub_efi_device_path_t *dp = NULL, *file_path = NULL;
  63. grub_efi_loaded_image_t *loaded_image;
  64. char *filename;
  65. void *boot_image = 0;
  66. grub_efi_handle_t dev_handle = 0;
  67. + grub_efi_physical_address_t address = 0;
  68. + grub_efi_uintn_t pages = 0;
  69. + grub_efi_char16_t *cmdline = NULL;
  70. if (argc == 0)
  71. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  72. @@ -234,11 +232,6 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
  73. grub_dl_ref (my_mod);
  74. - /* Initialize some global variables. */
  75. - address = 0;
  76. - image_handle = 0;
  77. - file_path = 0;
  78. -
  79. b = grub_efi_system_table->boot_services;
  80. file = grub_file_open (filename, GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE);
  81. @@ -408,6 +401,10 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
  82. grub_file_close (file);
  83. grub_device_close (dev);
  84. + /* We're finished with the source image buffer and file path now. */
  85. + efi_call_2 (b->free_pages, address, pages);
  86. + grub_free (file_path);
  87. +
  88. grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
  89. return 0;
  90. @@ -419,11 +416,18 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
  91. if (file)
  92. grub_file_close (file);
  93. + grub_free (cmdline);
  94. grub_free (file_path);
  95. if (address)
  96. efi_call_2 (b->free_pages, address, pages);
  97. + if (image_handle != NULL)
  98. + {
  99. + efi_call_1 (b->unload_image, image_handle);
  100. + image_handle = NULL;
  101. + }
  102. +
  103. grub_dl_unref (my_mod);
  104. return grub_errno;
  105. --
  106. 2.41.0