boot: restore parent loaded image when initrd registration fails

linux_exec() patches the stub's own EFI_LOADED_IMAGE_PROTOCOL to point
at the loaded inner kernel, and restores the saved original only after
the entry point returns. The initrd_register() failure path returns
without restoring, leaving the firmware's protocol pointing to freed
data.

Follow-up for f405165065

(cherry picked from commit baad1744bd)
This commit is contained in:
Luca Boccassi
2026-06-26 22:17:28 +01:00
parent b2644bfe8f
commit b16364d0df

View File

@@ -355,8 +355,12 @@ EFI_STATUS linux_exec(
_cleanup_(cleanup_initrd) EFI_HANDLE initrd_handle = NULL;
err = initrd_register(initrd, &initrd_handle);
if (err != EFI_SUCCESS)
if (err != EFI_SUCCESS) {
/* Restore the patched fields before kernel_file_path and loaded_kernel_pages are freed,
* otherwise the stub's own EFI_LOADED_IMAGE_PROTOCOL is left pointing at freed memory. */
*parent_loaded_image = original_parent_loaded_image;
return log_error_status(err, "Error registering initrd: %m");
}
log_wait();