Skip to contentSkip to conversation

efi/libstub/arm64: handle randomized TEXT_OFFSET

PATCH New Mark Rutland 3 participants 5 messages Apr 24, 2018 EFI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi/libstub/arm64: handle randomized TEXT_OFFSET

When CONFIG_RANDOMIZE_TEXT_OFFSET is selected, TEXT_OFFSET is an arbitrary multiple of PAGE_SIZE in the interval [0, 2MB).

The EFI stub doesn't accuont for this, and only handles the case where TEXT_OFFSET is multiple of EFI_KIMG_ALIGN. This can result in the kernel being loaded to an erroneous physical alignment. This has been observed to result in spurious stack overflow reports and failure to make use of the IRQ stacks, and theoretically could result in a number of other issues.

We can OR in the low bits of TEXT_OFFSET to ensure that we have the necessary offset (and hence have the necessary alignment), so let's do that.

Fixes 6f26b3671184c36d ("arm64: kaslr: increase randomization granularity")
Cc Ard Biesheuvel <[email protected]>
Cc linux-efi at vger.kernel.org
---
 drivers/firmware/efi/libstub/arm64-stub.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/firmware/efi/libstub/arm64-stub.c +7 -0
index b9bd827caa22..541b82fdc8a2 100644
@@ -98,6 +98,13 @@ efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg,
 			     (phys_seed >> 32) & mask : TEXT_OFFSET;
 
 		/*
+		 * With CONFIG_RANDOMIZE_TEXT_OFFSET, TEXT_OFFSET may not be a
+		 * multiple of EFI_KIMG_ALIGN, and we must ensure that we apply
+		 * the offset below EFI_KIMG_ALIGN.
+		 */
+		offset |= (TEXT_OFFSET % EFI_KIMG_ALIGN);
+
+		/*
 		 * If KASLR is enabled, and we have some randomness available,
 		 * locate the kernel at a randomized offset in physical memory.
 		 */
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/1 efi/libstub/arm64: handle randomized TEXT_OFFSET

Hi Mark,

Quoted 4 lines · Mark Rutland

When CONFIG_RANDOMIZE_TEXT_OFFSET is selected, TEXT_OFFSET is an arbitrary multiple of PAGE_SIZE in the interval [0, 2MB).

The EFI stub doesn't accuont for this, and only handles the case where

'account'

If you agree, I will add something here to clarify that the newly chosen offset should retain the misalignment of TEXT_OFFSET relative to EFI_KIMG_ALIGN, because it took me a while to figure that out.

Other than that,

I will queue this as a fix

Thanks,
Ard.

Quoted 40 lines

TEXT_OFFSET is multiple of EFI_KIMG_ALIGN. This can result in the kernel being loaded to an erroneous physical alignment. This has been observed to result in spurious stack overflow reports and failure to make use of the IRQ stacks, and theoretically could result in a number of other issues.

We can OR in the low bits of TEXT_OFFSET to ensure that we have the necessary offset (and hence have the necessary alignment), so let's do that.

Fixes 6f26b3671184c36d ("arm64: kaslr: increase randomization granularity")
Cc Ard Biesheuvel <[email protected]>
Cc linux-efi at vger.kernel.org
---
 drivers/firmware/efi/libstub/arm64-stub.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/firmware/efi/libstub/arm64-stub.c +7 -1
index b9bd827caa22..541b82fdc8a2 100644
@@ -98,6 +98,13 @@ efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg,
                             (phys_seed >> 32) & mask : TEXT_OFFSET;
 
                /*
+                * With CONFIG_RANDOMIZE_TEXT_OFFSET, TEXT_OFFSET may not be a
+                * multiple of EFI_KIMG_ALIGN, and we must ensure that we apply
+                * the offset below EFI_KIMG_ALIGN.
+                */
+               offset |= (TEXT_OFFSET % EFI_KIMG_ALIGN);
+
+               /*
                 * If KASLR is enabled, and we have some randomness available,
                 * locate the kernel at a randomized offset in physical memory.
                 */
--

2.11.0

#2 lore raw

PATCH 1/1 efi/libstub/arm64: handle randomized TEXT_OFFSET

Quoted 6 lines · Ard Biesheuvel

Hi Mark,

Quoted 3 lines · Mark Rutland

When CONFIG_RANDOMIZE_TEXT_OFFSET is selected, TEXT_OFFSET is an arbitrary multiple of PAGE_SIZE in the interval [0, 2MB).

Quoted 7 lines
Quoted 1 line

The EFI stub doesn't accuont for this, and only handles the case where

'account'

If you agree, I will add something here to clarify that the newly chosen offset should retain the misalignment of TEXT_OFFSET relative to EFI_KIMG_ALIGN, because it took me a while to figure that out.

That makes sense to me. Sorry for the clumsy wording!

Quoted 6 lines

Other than that,

I will queue this as a fix

Great; thanks!

Mark.

#3 lore raw

PATCH 1/1 efi/libstub/arm64: handle randomized TEXT_OFFSET

In reply to Mark Rutland
Quoted 14 lines · On Tue, 24 Apr 2018 12:00:58 +0100 Mark Rutland <[email protected]> wrote:

When CONFIG_RANDOMIZE_TEXT_OFFSET is selected, TEXT_OFFSET is an arbitrary multiple of PAGE_SIZE in the interval [0, 2MB).

The EFI stub doesn't accuont for this, and only handles the case where TEXT_OFFSET is multiple of EFI_KIMG_ALIGN. This can result in the kernel being loaded to an erroneous physical alignment. This has been observed to result in spurious stack overflow reports and failure to make use of the IRQ stacks, and theoretically could result in a number of other issues.

We can OR in the low bits of TEXT_OFFSET to ensure that we have the necessary offset (and hence have the necessary alignment), so let's do that.

Quoted 6 lines
Fixes 6f26b3671184c36d ("arm64: kaslr: increase randomization granularity")
Cc Ard Biesheuvel <[email protected]>
Cc linux-efi at vger.kernel.org

---

Thanks,

Kim

#4 lore raw

PATCH 1/1 efi/libstub/arm64: handle randomized TEXT_OFFSET

Quoted 17 lines · Kim Phillips
Quoted 14 lines · On Tue, 24 Apr 2018 12:00:58 +0100 Mark Rutland <[email protected]> wrote:

When CONFIG_RANDOMIZE_TEXT_OFFSET is selected, TEXT_OFFSET is an arbitrary multiple of PAGE_SIZE in the interval [0, 2MB).

The EFI stub doesn't accuont for this, and only handles the case where TEXT_OFFSET is multiple of EFI_KIMG_ALIGN. This can result in the kernel being loaded to an erroneous physical alignment. This has been observed to result in spurious stack overflow reports and failure to make use of the IRQ stacks, and theoretically could result in a number of other issues.

We can OR in the low bits of TEXT_OFFSET to ensure that we have the necessary offset (and hence have the necessary alignment), so let's do that.

Quoted 9 lines
Quoted 6 lines
Fixes 6f26b3671184c36d ("arm64: kaslr: increase randomization granularity")
Cc Ard Biesheuvel <[email protected]>
Cc linux-efi at vger.kernel.org

---

Thanks all

Queued in efi/urgent.