From a2af951d4f61ac7a5ea819eba49ce979c544a5ff Mon Sep 17 00:00:00 2001 From: Sergii Dmytruk Date: Wed, 21 Feb 2024 01:10:53 +0200 Subject: [PATCH] i386/slaunch: improve error recovery of "slaunch_module" command Update `slaunch_module` global only if new module passed all checks. Closes #11. Signed-off-by: Sergii Dmytruk --- grub-core/loader/i386/slaunch.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/grub-core/loader/i386/slaunch.c b/grub-core/loader/i386/slaunch.c index f2f091197..b6b76993c 100644 --- a/grub-core/loader/i386/slaunch.c +++ b/grub-core/loader/i386/slaunch.c @@ -205,6 +205,7 @@ grub_cmd_slaunch_module (grub_command_t cmd __attribute__ ((unused)), { grub_file_t file; grub_ssize_t size; + void *new_module = NULL; if (!argc) return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); @@ -231,12 +232,12 @@ grub_cmd_slaunch_module (grub_command_t cmd __attribute__ ((unused)), goto fail; } - slaunch_module = grub_malloc (size); + new_module = grub_malloc (size); - if (slaunch_module == NULL) + if (new_module == NULL) goto fail; - if (grub_file_read (file, slaunch_module, size) != size) + if (grub_file_read (file, new_module, size) != size) { if (grub_errno == GRUB_ERR_NONE) grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file: %s"), @@ -246,13 +247,13 @@ grub_cmd_slaunch_module (grub_command_t cmd __attribute__ ((unused)), if (slp == SLP_INTEL_TXT) { - if (!grub_txt_is_sinit_acmod (slaunch_module, size)) + if (!grub_txt_is_sinit_acmod (new_module, size)) { grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("it does not look like SINIT ACM")); goto fail; } - if (!grub_txt_acmod_match_platform (slaunch_module)) + if (!grub_txt_acmod_match_platform (new_module)) { grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("SINIT ACM does not match platform")); goto fail; @@ -269,16 +270,17 @@ grub_cmd_slaunch_module (grub_command_t cmd __attribute__ ((unused)), grub_file_close (file); + grub_free (slaunch_module); + slaunch_module = new_module; + return GRUB_ERR_NONE; fail: grub_error_push (); - grub_free (slaunch_module); + grub_free (new_module); grub_file_close (file); - slaunch_module = NULL; - grub_error_pop (); return grub_errno;