Skip to contentSkip to conversation

arm64/linux/loader: Use EFI CODE allocations for the linux kernel

RFC In review Jeffrey Hugo 5 participants 9 messages Apr 4, 2019 – Apr 9, 2019

lore
Share & tools
lore
#0 lore raw

RFC 1/1 arm64/linux/loader: Use EFI CODE allocations for the linux kernel

Some UEFI implementations for ARM64 devices apply strict permissions on the different allocation types. In these implementations, DATA allocations have XN (execute never) permissions, preventing code execution from those pages.

On these implementations, the Linux kernel is loaded to DATA pages, which causes a permission fault when GRUB attempts to kick off the kernel. This results in a device crash.

Fix this by allocating CODE pages for the Linux kernel.

---
 grub-core/kern/efi/mm.c        | 8 ++++++++
 grub-core/loader/arm64/linux.c | 2 +-
 include/grub/efi/efi.h         | 2 ++
 3 files changed, 11 insertions(+), 1 deletion(-)
grub-core/kern/efi/mm.c +8 -0
index b02fab1b1..725695e01 100644
@@ -156,6 +156,14 @@ grub_efi_allocate_any_pages (grub_efi_uintn_t pages)
 				       GRUB_EFI_LOADER_DATA);
 }
 
+void *
+grub_efi_allocate_code_pages (grub_efi_uintn_t pages)
+{
+  return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS,
+				       pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS,
+				       GRUB_EFI_LOADER_CODE);
+}
+
 void *
 grub_efi_allocate_fixed (grub_efi_physical_address_t address,
 			 grub_efi_uintn_t pages)
grub-core/loader/arm64/linux.c +1 -1
index ef3e9f944..877839072 100644
@@ -313,7 +313,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
   grub_loader_unset();
 
   grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
-  kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
+  kernel_addr = grub_efi_allocate_code_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
   grub_dprintf ("linux", "kernel numpages: %lld\n",
 		(long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
   if (!kernel_addr)
include/grub/efi/efi.h +2 -0
index e90e00dc4..697cabcd6 100644
@@ -47,6 +47,8 @@ EXPORT_FUNC(grub_efi_allocate_fixed) (grub_efi_physical_address_t address,
 				      grub_efi_uintn_t pages);
 void *
 EXPORT_FUNC(grub_efi_allocate_any_pages) (grub_efi_uintn_t pages);
+void *
+EXPORT_FUNC(grub_efi_allocate_code_pages) (grub_efi_uintn_t pages);
 void EXPORT_FUNC(grub_efi_free_pages) (grub_efi_physical_address_t address,
 				       grub_efi_uintn_t pages);
 grub_efi_uintn_t EXPORT_FUNC(grub_efi_find_mmap_size) (void);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 5 lines · Jeffrey Hugo

Some UEFI implementations for ARM64 devices apply strict permissions on the different allocation types. In these implementations, DATA allocations have XN (execute never) permissions, preventing code execution from those pages.

Quoted 7 lines

On these implementations, the Linux kernel is loaded to DATA pages, which causes a permission fault when GRUB attempts to kick off the kernel. This results in a device crash.

Fix this by allocating CODE pages for the Linux kernel.

Make sense for me but I would like to hear Leif's opinion too. I treat this a fix and if he is OK with it I am happy to take it into release.

Daniel

Reviewed-by
#2 lore raw
Quoted 10 lines · Daniel Kiper
Quoted 9 lines · Jeffrey Hugo

Some UEFI implementations for ARM64 devices apply strict permissions on the different allocation types. In these implementations, DATA allocations have XN (execute never) permissions, preventing code execution from those pages.

On these implementations, the Linux kernel is loaded to DATA pages, which causes a permission fault when GRUB attempts to kick off the kernel. This results in a device crash.

Quoted 6 lines
Quoted 3 lines

Fix this by allocating CODE pages for the Linux kernel.

Make sense for me but I would like to hear Leif's opinion too. I treat this a fix and if he is OK with it I am happy to take it into release.

This complements f826330683675f0deb55b58fd229afd7d65fb053 ("efi: change heap allocation type to GRUB_EFI_LOADER_CODE"), so I'm all for it.

This does bring to mind the clunkiness of the above. Marking *everything* executable bypasses the improved security provided by the firmware. Should I register a bug on Savannah to address this? (blatantly not for the upcoming release)

/
Leif

#3 lore raw
Quoted 15 lines · Leif Lindholm
Quoted 14 lines · Daniel Kiper
Quoted 12 lines · Jeffrey Hugo

Some UEFI implementations for ARM64 devices apply strict permissions on the different allocation types. In these implementations, DATA allocations have XN (execute never) permissions, preventing code execution from those pages.

On these implementations, the Linux kernel is loaded to DATA pages, which causes a permission fault when GRUB attempts to kick off the kernel. This results in a device crash.

Fix this by allocating CODE pages for the Linux kernel.

Quoted 8 lines
Quoted 2 lines

Make sense for me but I would like to hear Leif's opinion too. I treat this a fix and if he is OK with it I am happy to take it into release.

This complements f826330683675f0deb55b58fd229afd7d65fb053 ("efi: change heap allocation type to GRUB_EFI_LOADER_CODE"), so I'm all for it.

Thanks!

Quoted 2 lines

This does bring to mind the clunkiness of the above. Marking *everything* executable bypasses the improved security provided by the

Yeah, I agree. However, AIUI this issue is not ARM specific and also applies to other UEFI platforms.

Quoted 2 lines

firmware. Should I register a bug on Savannah to address this? (blatantly not for the upcoming release)

Please do.

Daniel

#4 lore raw
In reply to Leif Lindholm
Quoted 20 lines · Leif Lindholm
Quoted 10 lines · Daniel Kiper
Quoted 9 lines · Jeffrey Hugo

Some UEFI implementations for ARM64 devices apply strict permissions on the different allocation types. In these implementations, DATA allocations have XN (execute never) permissions, preventing code execution from those pages.

On these implementations, the Linux kernel is loaded to DATA pages, which causes a permission fault when GRUB attempts to kick off the kernel. This results in a device crash.

Quoted 5 lines
Quoted 3 lines

Fix this by allocating CODE pages for the Linux kernel.

Make sense for me but I would like to hear Leif's opinion too. I treat this a fix and if he is OK with it I am happy to take it into release.

This complements f826330683675f0deb55b58fd229afd7d65fb053 ("efi: change heap allocation type to GRUB_EFI_LOADER_CODE"), so I'm all for it.

Quoted 6 lines

This does bring to mind the clunkiness of the above. Marking *everything* executable bypasses the improved security provided by the firmware. Should I register a bug on Savannah to address this? (blatantly not for the upcoming release)

I quite frankly don't understand why we need to mark the PE binary as CODE in the first place. I thought the whole point of invoking the UEFI loader protocol was to ensure that the placement of sections from that binary into CODE/DATA happens properly?

Or are we not calling LoadImage?

Alex

#5 lore raw
Quoted 10 lines · Alexander Graf
Quoted 4 lines · Leif Lindholm

This does bring to mind the clunkiness of the above. Marking *everything* executable bypasses the improved security provided by the firmware. Should I register a bug on Savannah to address this? (blatantly not for the upcoming release)

I quite frankly don't understand why we need to mark the PE binary as CODE in the first place. I thought the whole point of invoking the UEFI loader protocol was to ensure that the placement of sections from that binary into CODE/DATA happens properly?

You have a point, but I don't think Jeffrey found this through code review.

It is possible that my belt-and-braces approach of both adding a memory mapped device path and setting SourceBuffer breaks assumptions in the UEFI implementation.

Jeffrey - could you try changing
status = b->load_image (0, grub_efi_image_handle,

                          (grub_efi_device_path_t *) mempath,
                          (void *) addr,
                          size, &image_handle);
to
  status = b->load_image (0, grub_efi_image_handle,
                          NULL,
                          (void *) addr,
                          size, &image_handle);

and see if that makes the problem go away without changing the allocation type?

Quoted 1 line

Or are we not calling LoadImage?

We are.

/
Leif

#6 lore raw
Quoted 30 lines · Leif Lindholm
Quoted 10 lines · Alexander Graf
Quoted 4 lines · Leif Lindholm

This does bring to mind the clunkiness of the above. Marking *everything* executable bypasses the improved security provided by the firmware. Should I register a bug on Savannah to address this? (blatantly not for the upcoming release)

I quite frankly don't understand why we need to mark the PE binary as CODE in the first place. I thought the whole point of invoking the UEFI loader protocol was to ensure that the placement of sections from that binary into CODE/DATA happens properly?

You have a point, but I don't think Jeffrey found this through code review.

It is possible that my belt-and-braces approach of both adding a memory mapped device path and setting SourceBuffer breaks assumptions in the UEFI implementation.

Jeffrey - could you try changing
status = b->load_image (0, grub_efi_image_handle,

                          (grub_efi_device_path_t *) mempath,
                          (void *) addr,
                          size, &image_handle);
to
  status = b->load_image (0, grub_efi_image_handle,
                          NULL,
                          (void *) addr,
                          size, &image_handle);
Quoted 2 lines

and see if that makes the problem go away without changing the allocation type?

I'll give that a shot and report back.

Quoted 7 lines
Quoted 2 lines

Or are we not calling LoadImage?

We are.

/
Leif

#7 lore raw
Quoted 35 lines · Jeffrey Hugo
Quoted 30 lines · Leif Lindholm
Quoted 10 lines · Alexander Graf
Quoted 4 lines · Leif Lindholm

This does bring to mind the clunkiness of the above. Marking *everything* executable bypasses the improved security provided by the firmware. Should I register a bug on Savannah to address this? (blatantly not for the upcoming release)

I quite frankly don't understand why we need to mark the PE binary as CODE in the first place. I thought the whole point of invoking the UEFI loader protocol was to ensure that the placement of sections from that binary into CODE/DATA happens properly?

You have a point, but I don't think Jeffrey found this through code review.

It is possible that my belt-and-braces approach of both adding a memory mapped device path and setting SourceBuffer breaks assumptions in the UEFI implementation.

Jeffrey - could you try changing
status = b->load_image (0, grub_efi_image_handle,

                          (grub_efi_device_path_t *) mempath,
                          (void *) addr,
                          size, &image_handle);
to
  status = b->load_image (0, grub_efi_image_handle,
                          NULL,
                          (void *) addr,
                          size, &image_handle);
Quoted 2 lines

and see if that makes the problem go away without changing the allocation type?

Quoted 1 line

I'll give that a shot and report back.

Hmm. I apparently wasn't very methodical about my previous testing. Mainline GRUB doesn't appear to have the original issue, thus my fix would appear to be not needed.
However, downstream distro GRUB appears to be affected (I was originally attempting to use Ubuntu).

I suspect this change, which appears to remove the load_image call while adding Secure Boot support -
https://git.launchpad.net/~ubuntu-core-dev/grub/+git/ubuntu/commit/grub-core/loader/arm64/linux.c?h=ubuntu&id=98bc4f52034abb18e7bd028d424bde15375c14af

Sorry for the noise, although I would appreciate some pointers on how this should be addressed. Should I go file bugs against the distros?

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

Or are we not calling LoadImage?

We are.

/
Leif

#8 lore raw
In reply to Leif Lindholm
Quoted 8 lines · Leif Lindholm
Quoted 6 lines · Alexander Graf
Quoted 4 lines · Leif Lindholm

This does bring to mind the clunkiness of the above. Marking *everything* executable bypasses the improved security provided by the firmware. Should I register a bug on Savannah to address this? (blatantly not for the upcoming release)

Quoted 8 lines
Quoted 4 lines

I quite frankly don't understand why we need to mark the PE binary as CODE in the first place. I thought the whole point of invoking the UEFI loader protocol was to ensure that the placement of sections from that binary into CODE/DATA happens properly?

You have a point, but I don't think Jeffrey found this through code review.

Whether a region is R-X, RW- or RWX and whether a region is carved out of Efi*Code or Efi*Data memory are two different things. The sections of a PE/COFF image need to be contiguous in memory, and you cannot expect the heap allocator to return contiguous regions for separate Efi*Code and Efi*Data allocations, so the memory holding a PE/COFF image should be allocated in a single call.

So usually, we allocate Efi*Code regions for executable images (wbich are guaranteed to be RWX when created) and rely on the SetMemoryAttributes() DXE services to remove either the W or the X bit depending on the nature of the section. This obviously leaves a small window where RWX memory exists, but this is the best we can do without violating the UEFI spec.

Quoted 23 lines

It is possible that my belt-and-braces approach of both adding a memory mapped device path and setting SourceBuffer breaks assumptions in the UEFI implementation.

Jeffrey - could you try changing
status = b->load_image (0, grub_efi_image_handle,

                          (grub_efi_device_path_t *) mempath,
                          (void *) addr,
                          size, &image_handle);
to
  status = b->load_image (0, grub_efi_image_handle,
                          NULL,
                          (void *) addr,
                          size, &image_handle);

and see if that makes the problem go away without changing the allocation type?

Quoted 1 line

Or are we not calling LoadImage?

We are.

/
Leif