All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches
@ 2021-04-27 10:25 Javier Martinez Canillas
  2021-04-27 10:48 ` John Paul Adrian Glaubitz
  0 siblings, 1 reply; 6+ messages in thread
From: Javier Martinez Canillas @ 2021-04-27 10:25 UTC (permalink / raw)
  To: grub-devel
  Cc: John Paul Adrian Glaubitz, Daniel Kiper, 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.

Reported-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---

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(+)

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index 00f49ccaaaf..d78fa3e5330 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -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
     {
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches
  2021-04-27 10:25 [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches Javier Martinez Canillas
@ 2021-04-27 10:48 ` John Paul Adrian Glaubitz
  2021-04-27 11:05   ` Javier Martinez Canillas
  2021-04-27 15:35   ` Daniel Kiper
  0 siblings, 2 replies; 6+ messages in thread
From: John Paul Adrian Glaubitz @ 2021-04-27 10:48 UTC (permalink / raw)
  To: The development of GNU GRUB, Javier Martinez Canillas; +Cc: Daniel Kiper

On 4/27/21 12:25 PM, Javier Martinez Canillas wrote:
> 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.
> 
> Reported-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
> ---
> 
> 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(+)
> 
> diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
> index 00f49ccaaaf..d78fa3e5330 100644
> --- a/util/grub-mkimagexx.c
> +++ b/util/grub-mkimagexx.c
> @@ -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.

Tested-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>

-- 
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - glaubitz@debian.org
`. `'   Freie Universitaet Berlin - glaubitz@physik.fu-berlin.de
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches
  2021-04-27 10:48 ` John Paul Adrian Glaubitz
@ 2021-04-27 11:05   ` Javier Martinez Canillas
  2021-04-27 15:35   ` Daniel Kiper
  1 sibling, 0 replies; 6+ messages in thread
From: Javier Martinez Canillas @ 2021-04-27 11:05 UTC (permalink / raw)
  To: John Paul Adrian Glaubitz, The development of GNU GRUB; +Cc: Daniel Kiper

On 4/27/21 12:48 PM, John Paul Adrian Glaubitz wrote:
> On 4/27/21 12:25 PM, Javier Martinez Canillas wrote:

[snip]

>>  	}
>>  #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.
> 
> Tested-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
> 

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

Best regards,
-- 
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches
  2021-04-27 10:48 ` John Paul Adrian Glaubitz
  2021-04-27 11:05   ` Javier Martinez Canillas
@ 2021-04-27 15:35   ` Daniel Kiper
  2021-05-09 13:28     ` John Paul Adrian Glaubitz
  1 sibling, 1 reply; 6+ messages in thread
From: Daniel Kiper @ 2021-04-27 15:35 UTC (permalink / raw)
  To: John Paul Adrian Glaubitz
  Cc: The development of GNU GRUB, Javier Martinez Canillas

On Tue, Apr 27, 2021 at 12:48:29PM +0200, John Paul Adrian Glaubitz wrote:
> On 4/27/21 12:25 PM, Javier Martinez Canillas wrote:
> > 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.
> >
> > Reported-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
> > Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
> > ---
> >
> > 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(+)
> >
> > diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
> > index 00f49ccaaaf..d78fa3e5330 100644
> > --- a/util/grub-mkimagexx.c
> > +++ b/util/grub-mkimagexx.c
> > @@ -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.
>
> Tested-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>

Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

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

Daniel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches
  2021-04-27 15:35   ` Daniel Kiper
@ 2021-05-09 13:28     ` John Paul Adrian Glaubitz
  2021-05-10 14:53       ` Daniel Kiper
  0 siblings, 1 reply; 6+ messages in thread
From: John Paul Adrian Glaubitz @ 2021-05-09 13:28 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: The development of GNU GRUB, Javier Martinez Canillas

Hi Daniel!

On 4/27/21 5:35 PM, Daniel Kiper wrote:
>> Works on my ia64 machine.
>>
>> Tested-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
> 
> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
> 
> 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

-- 
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - glaubitz@debian.org
`. `'   Freie Universitaet Berlin - glaubitz@physik.fu-berlin.de
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches
  2021-05-09 13:28     ` John Paul Adrian Glaubitz
@ 2021-05-10 14:53       ` Daniel Kiper
  0 siblings, 0 replies; 6+ messages in thread
From: Daniel Kiper @ 2021-05-10 14:53 UTC (permalink / raw)
  To: John Paul Adrian Glaubitz
  Cc: The development of GNU GRUB, Javier Martinez Canillas

On Sun, May 09, 2021 at 03:28:53PM +0200, John Paul Adrian Glaubitz wrote:
> Hi Daniel!
>
> On 4/27/21 5:35 PM, Daniel Kiper wrote:
> >> Works on my ia64 machine.
> >>
> >> Tested-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
> >
> > Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
> >
> > 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-05-10 14:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-27 10:25 [PATCH v3] util/mkimage: Fix wrong PE32+ section sizes for some arches Javier Martinez Canillas
2021-04-27 10:48 ` John Paul Adrian Glaubitz
2021-04-27 11:05   ` Javier Martinez Canillas
2021-04-27 15:35   ` Daniel Kiper
2021-05-09 13:28     ` John Paul Adrian Glaubitz
2021-05-10 14:53       ` Daniel Kiper

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.