blob: fa755e78d8531f57eca49ac4422e8aaf3ecbd4e8 [file] [log] [blame]
From 708e4a8c1e4ac3070396d9ba2d2f412cba29a455 Mon Sep 17 00:00:00 2001
From: Oleksandr Tymoshenko <ovt@google.com>
Date: Thu, 13 May 2021 01:55:34 +0000
Subject: [PATCH] grub-lakitu: fix invalid offsets in PE sections
Commit f60ba9e5 changed logic for calculating section's raw offset
for PE header. New logic generates invalid "mods" section offset
if ".data" section size is not page-aligned. In this case memory
layout of core image looks like [.text] [.data] [mods] but the
generated offset table makes it [.text] [.data] [align] [mods]
so the mods raw offset in the header points at [align] bytes
in the "mods" section.
To fix the issue by copying "mods" section seperately into the newly
allocated image area so the header offset and actual one matches.
---
util/mkimage.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/util/mkimage.c b/util/mkimage.c
index 8319e8dfb..8d99cbc7d 100644
--- a/util/mkimage.c
+++ b/util/mkimage.c
@@ -1365,11 +1365,13 @@ grub_install_generate_image (const char *dir, const char *prefix,
sbat_size = ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT);
}
- pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
+ pe_size = ALIGN_UP (header_size + layout.exec_size, GRUB_PE32_FILE_ALIGNMENT) +
+ ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT) +
+ ALIGN_UP (core_size - layout.kernel_size, GRUB_PE32_FILE_ALIGNMENT) +
ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT) + sbat_size;
header = pe_img = xcalloc (1, pe_size);
- memcpy (pe_img + raw_data, core_img, core_size);
+ memcpy (pe_img + raw_data, core_img, layout.kernel_size);
/* The magic. */
memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
@@ -1458,7 +1460,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
GRUB_PE32_SCN_MEM_READ |
GRUB_PE32_SCN_MEM_WRITE);
- scn_size = pe_size - layout.reloc_size - sbat_size - raw_data;
+ scn_size = core_size - layout.kernel_size;
+ memcpy (pe_img + raw_data, core_img + layout.kernel_size, scn_size);
section = init_pe_section (image_target, section, "mods",
&vma, scn_size, image_target->section_align,
&raw_data, scn_size,
--
2.31.1.751.gd2f1c929bd-goog