util/mkimage: Fix wrong PE32+ section sizes for some arches
RFC PATCH v2 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.
But for some platforms (i.e: ia64 and aa64) the kernel image size wasn't aligned using the section alignment, which causes the PE section headers to not match the actual section sizes in the PE32+ binary file.
This caused problems on ia64 EFI machines, since the .data section size is bigger than the actual section in the PE32+ binary, overlapping with part of the mods section. That leads to GRUB not being able to load any built-in module.
Fix it by aligning the kernel_size to the section alignment, that makes the sizes and offsets in the PE section headers to match the sections in the PE32+ binary file.
---
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 | 5 +++++ 1 file changed, 5 insertions(+)
util/grub-mkimagexx.c +5 -0
@@ -2388,6 +2388,11 @@ 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 + image_target->vaddr_offset, + image_target->section_align) + - image_target->vaddr_offset; }else {
Signature · 1 line
2.31.1
Quoted 35 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.But for some platforms (i.e: ia64 and aa64) the kernel image size wasn't aligned using the section alignment, which causes the PE section headers to not match the actual section sizes in the PE32+ binary file.
This caused problems on ia64 EFI machines, since the .data section size is bigger than the actual section in the PE32+ binary, overlapping with part of the mods section. That leads to GRUB not being able to load any built-in module.
Fix it by aligning the
kernel_sizeto the section alignment, that makes the sizes and offsets in the PE section headers to match the sections in the PE32+ binary file.Reported-by John Paul Adrian Glaubitz <[email protected]>Signed-off-by Javier Martinez Canillas <[email protected]>---
Changes in v2:
- Align up for any arch in theis_relocatable(image_target) patch and not only forMKIMAGE_ELF64orEM_AARCH64(suggested by Daniel Kiper).util/grub-mkimagexx.c | 5 +++++ 1 file changed, 5 insertions(+)util/grub-mkimagexx.c +0
-0@@ -2388,6 +2388,11 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path, layout->kernel_size +=ALIGN_UP (layout->got_size,16 );
util/grub-mkimagexx.c SUFFIX (grub_mkimage_load_image) (const char *kernel_path, · line 2398
Quoted 10 lines
util/grub-mkimagexx.c +5
-0}#endif + +if (image_target->id ==IMAGE_EFI ) + layout->kernel_size =ALIGN_UP (layout->kernel_size + image_target->vaddr_offset, + image_target->section_align) + - image_target->vaddr_offset; }else {
Verified to fix the issue on ia64.
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
Hello Adrian,
On 4/26/21 9:52 AM, John Paul Adrian Glaubitz wrote:
[snip]
Quoted 10 lines
util/grub-mkimagexx.c SUFFIX (grub_mkimage_load_image) (const char *kernel_path, · line 2398
Quoted 9 lines
util/grub-mkimagexx.c +5
-0+ +if (image_target->id ==IMAGE_EFI ) + layout->kernel_size =ALIGN_UP (layout->kernel_size + image_target->vaddr_offset, + image_target->section_align) + - image_target->vaddr_offset; }else {
Quoted 4 lines
Verified to fix the issue on ia64.
Thanks again for testing this so quickly! I really appreciate it since don't have access to an Itanium system to verify the patch.
Best regards,
Signature · 3 lines
Javier Martinez Canillas Software Engineer - Desktop Hardware Enablement Red Hat
Hi Javier!
Quoted 14 lines · Javier Martinez Canillas
On 4/26/21 9:52 AM, John Paul Adrian Glaubitz wrote:
[snip]
Quoted 10 lines
Quoted 9 lines
util/grub-mkimagexx.c +5
-0+ +if (image_target->id ==IMAGE_EFI ) + layout->kernel_size =ALIGN_UP (layout->kernel_size + image_target->vaddr_offset, + image_target->section_align) + - image_target->vaddr_offset; }else {
Quoted 7 lines
Quoted 4 lines
Verified to fix the issue on ia64.
Thanks again for testing this so quickly! I really appreciate it since don't have access to an Itanium system to verify the patch.
You're absolutely welcome. And I also appreciate the great work so many people have already delivered to get the next release of GRUB ready. I'm really looking forward to version 2.06 and the swath of patches which distributions will be able to drop because they have all been merged upstream :-).
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
Quoted 5 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.But for some platforms (i.e: ia64 and aa64) the kernel image size wasn't
s/But/However, /
s/(i.e: ia64 and aa64)/, i.e ia64-efi and arm64-efi,/ s/wasn't/is not/
Quoted 1 line
aligned using the section alignment, which causes the PE section headers
s/, which causes the PE section headers/./
Quoted 1 line
to not match the actual section sizes in the PE32+ binary file.
s/to not match the actual section sizes in the PE32+ binary file./
/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.
Quoted 4 lines
This caused problems on ia64 EFI machines, since the .data section size is bigger than the actual section in the PE32+ binary, overlapping with part of the mods section. That leads to GRUB not being able to load any built-in module.
Please drop this paragraph...
Quoted 1 line
Fix it by aligning the
kernel_sizeto the section alignment, that makes
s/, t/. T/
Quoted 1 line
the sizes and offsets in the PE section headers to match the sections
s/the sections/relevant sections/
Quoted 18 lines
in the PE32+ binary file.
Reported-by John Paul Adrian Glaubitz <[email protected]>Signed-off-by Javier Martinez Canillas <[email protected]>---
Changes in v2:
- Align up for any arch in theis_relocatable(image_target) patch and not only forMKIMAGE_ELF64orEM_AARCH64(suggested by Daniel Kiper).util/grub-mkimagexx.c | 5 +++++ 1 file changed, 5 insertions(+)util/grub-mkimagexx.c +0
-0
util/grub-mkimagexx.c SUFFIX (grub_mkimage_load_image) (const char *kernel_path, · line 2395
Quoted 8 lines
util/grub-mkimagexx.c +5
-0@@ -2388,6 +2388,11 @@ 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 + image_target->vaddr_offset, + image_target->section_align) + - image_target->vaddr_offset;
I think this should be:
layout->kernel_size = ALIGN_UP (layout->kernel_size, GRUB_PE32_FILE_ALIGNMENT);
Here we care about alignment in the PE file not in the memory. So, I think we have to use GRUB_PE32_FILE_ALIGNMENT instead. Though the result is the same...
Daniel
Hi Javier!
Quoted 7 lines · Daniel Kiper
(...)
I think this should be:
layout->kernel_size=ALIGN_UP(layout->kernel_size,GRUB_PE32_FILE_ALIGNMENT);Here we care about alignment in the PE file not in the memory. So, I think we have to use
GRUB_PE32_FILE_ALIGNMENTinstead. Though the result is the same...
Let me know once you have a new version of the patch ready, I'll test it as quickly as possible.
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
Hello Adrian,
Quoted 7 lines · John Paul Adrian Glaubitz
Hi Javier!
Quoted 4 lines · Daniel Kiper
(...)
I think this should be:
layout->kernel_size=ALIGN_UP(layout->kernel_size,GRUB_PE32_FILE_ALIGNMENT);
Quoted 7 lines
Quoted 3 lines
Here we care about alignment in the PE file not in the memory. So, I think we have to use
GRUB_PE32_FILE_ALIGNMENTinstead. Though the result is the same...Let me know once you have a new version of the patch ready, I'll test it as quickly as possible.
Thanks! I was busy this morning with other things but I'll post a v3 now.
I'll keep your Tested-by tag since the size calculated should be the same, but would be good to test it again just to make sure that's the case.
Quoted 2 lines
Adrian
Best regards,
Signature · 3 lines
Javier Martinez Canillas Software Engineer - Desktop Hardware Enablement Red Hat