public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
From: Benjamin Herrenschmidt <benh@kernel.crashing.org>
To: linux-arm-kernel@lists.infradead.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
	linux-efi@vger.kernel.org,
	"linux-kernel@vger.kernel.org Will Deacon" <will@kernel.org>
Subject: [PATCH 2/2] arm64: efi: kaslr: Fix boot failure if efi_random_alloc() fails
Date: Tue, 20 Jul 2021 21:14:12 +1000	[thread overview]
Message-ID: <161920fc31ec4168290ca31b3e4ac7a75ac1df6b.camel@kernel.crashing.org> (raw)

If efi_random_alloc() fails, we still try to use EFI_KIMG_ALIGN
instead of MIN_KIMG_ALIGN to check the kernel image alignment,
which is incorrect, we need to fallback to MIN_KIMG_ALIGN (2M).

This removes the not-that-useful min_kimg_align helper and instead
uses the appropriate aligment in the respective call sites:

efi_random_alloc() always wants EFI_KIMG_ALIGN as this is only
used when kaslr is on, and all other cases go into alignment
check code which always need to check (and enforce) MIN_KIMG_ALIGN

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Fixes: 7c116db24d94 (efi/libstub/arm64: Retain 2MB kernel Image alignment if !KASLR)
---
 drivers/firmware/efi/libstub/arm64-stub.c | 27 ++++++++++-------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c
index 7bf0a7acae5e..e264ff90ba03 100644
--- a/drivers/firmware/efi/libstub/arm64-stub.c
+++ b/drivers/firmware/efi/libstub/arm64-stub.c
@@ -34,18 +34,6 @@ efi_status_t check_platform_features(void)
 	return EFI_SUCCESS;
 }
 
-/*
- * Although relocatable kernels can fix up the misalignment with respect to
- * MIN_KIMG_ALIGN, the resulting virtual text addresses are subtly out of
- * sync with those recorded in the vmlinux when kaslr is disabled but the
- * image required relocation anyway. Therefore retain 2M alignment unless
- * KASLR is in use.
- */
-static u64 min_kimg_align(void)
-{
-	return efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
-}
-
 efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *image_size,
 				 unsigned long *reserve_addr,
@@ -84,15 +72,24 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 		/*
 		 * If KASLR is enabled, and we have some randomness available,
 		 * locate the kernel at a randomized offset in physical memory.
+		 *
+		 * In that case, we don't need to preserve the 2M alignment
 		 */
-		status = efi_random_alloc(*reserve_size, min_kimg_align(),
+		status = efi_random_alloc(*reserve_size, EFI_KIMG_ALIGN,
 					  reserve_addr, phys_seed);
 	} else {
 		status = EFI_OUT_OF_RESOURCES;
 	}
 
 	if (status != EFI_SUCCESS) {
-		if (IS_ALIGNED((u64)_text, min_kimg_align())) {
+		/*
+		 * Although relocatable kernels can fix up the misalignment with respect to
+		 * MIN_KIMG_ALIGN, the resulting virtual text addresses are subtly out of
+		 * sync with those recorded in the vmlinux when kaslr is disabled but the
+		 * image required relocation anyway. Therefore retain 2M alignment unless
+		 * KASLR is in use.
+		 */
+		if (IS_ALIGNED((u64)_text, MIN_KIMG_ALIGN)) {
 			/*
 			 * Just execute from wherever we were loaded by the
 			 * UEFI PE/COFF loader if the alignment is suitable.
@@ -103,7 +100,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 		}
 
 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
-						    ULONG_MAX, min_kimg_align());
+						    ULONG_MAX, MIN_KIMG_ALIGN);
 
 		if (status != EFI_SUCCESS) {
 			efi_err("Failed to relocate kernel\n");




_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

             reply	other threads:[~2021-07-20 11:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-20 11:14 Benjamin Herrenschmidt [this message]
2021-07-20 12:57 ` [PATCH 2/2] arm64: efi: kaslr: Fix boot failure if efi_random_alloc() fails Ard Biesheuvel
2021-07-20 13:10   ` Benjamin Herrenschmidt
2021-07-20 13:48     ` Ard Biesheuvel
2021-07-20 14:03       ` Benjamin Herrenschmidt
2021-07-20 14:10         ` Ard Biesheuvel
2021-07-20 14:25           ` Benjamin Herrenschmidt
2021-07-20 14:40             ` Ard Biesheuvel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=161920fc31ec4168290ca31b3e4ac7a75ac1df6b.camel@kernel.crashing.org \
    --to=benh@kernel.crashing.org \
    --cc=ardb@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox