Skip to contentSkip to conversation

util/mkimage: Fix wrong PE32+ section sizes for some arches

PATCH In review Javier Martinez Canillas 4 participants 6 messages Apr 27, 2021 – May 10, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 util/mkimage: Fix wrong PE32+ section sizes for some arches

Commit f60ba9e5945 (util/mkimage: Refactor section setup to use a helper) added a helper function to setup PE sections. But it also changed how the raw data offsets were calculated since all the section sizes are aligned.

However, for some platforms, i.e ia64-efi and arm64-efi, the kernel image size is not aligned using the section alignment.

This leads to the situation in which the mods section offset in its PE section header does not match its real placement in the PE file. So, finally the GRUB is not able to locate and load built-in modules.

The problem surfaces on ia64-efi and arm64-efi because both platforms require additional relocation data which is added behind .bss section. So, we have to add some padding behind this extra data to make the beginning of mods section properly aligned in the PE file.

Fix it by aligning the kernel_size to the section alignment. That makes the sizes and offsets in the PE section headers to match relevant sections in the PE32+ binary file.

---

Changes in v3:
- Drop the RFC prefix since I'm confident now about the solution.
- Improve commit message (suggested by Daniel Kiper).
- Don't use virtual memory addresses to calculate the kernel size, but instead use the raw data sizes (suggested by Daniel Kiper).

Changes in v2:
- Align up for any arch in the is_relocatable (image_target) patch and not only for MKIMAGE_ELF64 or EM_AARCH64 (suggested by Daniel Kiper).

 util/grub-mkimagexx.c | 4 ++++
 1 file changed, 4 insertions(+)
util/grub-mkimagexx.c +4 -0
index 00f49ccaaaf..d78fa3e5330 100644
@@ -2388,6 +2388,10 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path,
 	  layout->kernel_size += ALIGN_UP (layout->got_size, 16);
 	}
 #endif
+
+      if (image_target->id == IMAGE_EFI)
+        layout->kernel_size = ALIGN_UP (layout->kernel_size,
+                                        GRUB_PE32_FILE_ALIGNMENT);
     }
   else
     {
Signature · 1 line
2.31.1
Tested-by on v3 1/1
#1 lore raw
Quoted 43 lines · Javier Martinez Canillas

Commit f60ba9e5945 (util/mkimage: Refactor section setup to use a helper) added a helper function to setup PE sections. But it also changed how the raw data offsets were calculated since all the section sizes are aligned.

However, for some platforms, i.e ia64-efi and arm64-efi, the kernel image size is not aligned using the section alignment.

This leads to the situation in which the mods section offset in its PE section header does not match its real placement in the PE file. So, finally the GRUB is not able to locate and load built-in modules.

The problem surfaces on ia64-efi and arm64-efi because both platforms require additional relocation data which is added behind .bss section. So, we have to add some padding behind this extra data to make the beginning of mods section properly aligned in the PE file.

Fix it by aligning the kernel_size to the section alignment. That makes the sizes and offsets in the PE section headers to match relevant sections in the PE32+ binary file.

---

Changes in v3:
- Drop the RFC prefix since I'm confident now about the solution.
- Improve commit message (suggested by Daniel Kiper).
- Don't use virtual memory addresses to calculate the kernel size, but instead use the raw data sizes (suggested by Daniel Kiper).

Changes in v2:
- Align up for any arch in the is_relocatable (image_target) patch and not only for MKIMAGE_ELF64 or EM_AARCH64 (suggested by Daniel Kiper).

 util/grub-mkimagexx.c | 4 ++++
 1 file changed, 4 insertions(+)
util/grub-mkimagexx.c +0 -0
index 00f49ccaaaf..d78fa3e5330 100644

util/grub-mkimagexx.c SUFFIX (grub_mkimage_load_image) (const char *kernel_path, · line 2397

Quoted 10 lines
util/grub-mkimagexx.c +4 -0
@@ -2388,6 +2388,10 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path,
 	  layout->kernel_size += ALIGN_UP (layout->got_size, 16);
 	}
 #endif
+
+      if (image_target->id == IMAGE_EFI)
+        layout->kernel_size = ALIGN_UP (layout->kernel_size,
+                                        GRUB_PE32_FILE_ALIGNMENT);
     }
   else
     {

Works on my ia64 machine.

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#2 lore raw
Quoted 1 line · John Paul Adrian Glaubitz

On 4/27/21 12:25 PM, Javier Martinez Canillas wrote:

[snip]

Quoted 11 lines

util/grub-mkimagexx.c SUFFIX (grub_mkimage_load_image) (const char *kernel_path, · line 2397

Quoted 10 lines
util/grub-mkimagexx.c +4 -0
 	}
 #endif
+
+      if (image_target->id == IMAGE_EFI)
+        layout->kernel_size = ALIGN_UP (layout->kernel_size,
+                                        GRUB_PE32_FILE_ALIGNMENT);
     }
   else
     {
Quoted 4 lines

Works on my ia64 machine.

Thanks for testing it again! Hopefully this could be the last version of this patch.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
Reviewed-by
#3 lore raw
In reply to John Paul Adrian Glaubitz
Quoted 57 lines · John Paul Adrian Glaubitz
Quoted 43 lines · Javier Martinez Canillas

Commit f60ba9e5945 (util/mkimage: Refactor section setup to use a helper) added a helper function to setup PE sections. But it also changed how the raw data offsets were calculated since all the section sizes are aligned.

However, for some platforms, i.e ia64-efi and arm64-efi, the kernel image size is not aligned using the section alignment.

This leads to the situation in which the mods section offset in its PE section header does not match its real placement in the PE file. So, finally the GRUB is not able to locate and load built-in modules.

The problem surfaces on ia64-efi and arm64-efi because both platforms require additional relocation data which is added behind .bss section. So, we have to add some padding behind this extra data to make the beginning of mods section properly aligned in the PE file.

Fix it by aligning the kernel_size to the section alignment. That makes the sizes and offsets in the PE section headers to match relevant sections in the PE32+ binary file.

---

Changes in v3:
- Drop the RFC prefix since I'm confident now about the solution.
- Improve commit message (suggested by Daniel Kiper).
- Don't use virtual memory addresses to calculate the kernel size, but instead use the raw data sizes (suggested by Daniel Kiper).

Changes in v2:
- Align up for any arch in the is_relocatable (image_target) patch and not only for MKIMAGE_ELF64 or EM_AARCH64 (suggested by Daniel Kiper).

 util/grub-mkimagexx.c | 4 ++++
 1 file changed, 4 insertions(+)
util/grub-mkimagexx.c +0 -0
index 00f49ccaaaf..d78fa3e5330 100644

util/grub-mkimagexx.c SUFFIX (grub_mkimage_load_image) (const char *kernel_path, · line 2397

Quoted 10 lines
util/grub-mkimagexx.c +4 -0
@@ -2388,6 +2388,10 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path,
 	  layout->kernel_size += ALIGN_UP (layout->got_size, 16);
 	}
 #endif
+
+      if (image_target->id == IMAGE_EFI)
+        layout->kernel_size = ALIGN_UP (layout->kernel_size,
+                                        GRUB_PE32_FILE_ALIGNMENT);
     }
   else
     {

Works on my ia64 machine.

Quoted 1 line

Javier, Adrian, thank you for working patiently on this fix.

Daniel

#4 lore raw

Hi Daniel!

Quoted 7 lines · Daniel Kiper
Quoted 3 lines

Works on my ia64 machine.

Javier, Adrian, thank you for working patiently on this fix.

Sure, no problem :-). Since this fix unbreaks grub on potentially many systems, could you merge the patch with higher priority?

Thanks,
Adrian

Signature · 4 lines
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - [email protected]
`. `'   Freie Universitaet Berlin - [email protected]
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
#5 lore raw
Quoted 5 lines · John Paul Adrian Glaubitz

Hi Daniel!

Quoted 2 lines · Daniel Kiper
Quoted 2 lines

Works on my ia64 machine.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Javier, Adrian, thank you for working patiently on this fix.

Sure, no problem :-). Since this fix unbreaks grub on potentially many systems, could you merge the patch with higher priority?

I have just pushed this fix together with others...

Daniel