linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Wupeng Ma <mawupeng1@huawei.com>
To: <akpm@linux-foundation.org>, <catalin.marinas@arm.com>,
	<will@kernel.org>, <corbet@lwn.net>
Cc: <ardb@kernel.org>, <tglx@linutronix.de>, <mingo@redhat.com>,
	<bp@alien8.de>, <dave.hansen@linux.intel.com>, <x86@kernel.org>,
	<dvhart@infradead.org>, <andy@infradead.org>, <rppt@kernel.org>,
	<paulmck@kernel.org>, <peterz@infradead.org>, <jroedel@suse.de>,
	<songmuchun@bytedance.com>, <macro@orcam.me.uk>,
	<frederic@kernel.org>, <W_Armin@gmx.de>, <john.garry@huawei.com>,
	<seanjc@google.com>, <tsbogend@alpha.franken.de>,
	<anshuman.khandual@arm.com>, <chenhuacai@kernel.org>,
	<david@redhat.com>, <gpiccoli@igalia.com>, <mark.rutland@arm.com>,
	<wangkefeng.wang@huawei.com>, <mawupeng1@huawei.com>,
	<linux-doc@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-efi@vger.kernel.org>, <linux-ia64@vger.kernel.org>,
	<platform-driver-x86@vger.kernel.org>, <linux-mm@kvack.org>
Subject: [PATCH 1/2] arm64/boot/KASLR: Add support to relocate kernel image to mirrored region
Date: Tue, 19 Apr 2022 15:01:49 +0800	[thread overview]
Message-ID: <20220419070150.254377-2-mawupeng1@huawei.com> (raw)
In-Reply-To: <20220419070150.254377-1-mawupeng1@huawei.com>

From: Ma Wupeng <mawupeng1@huawei.com>

Add support to relocate kernel image to mirrored regions if KASLR is
enabled.

If a suiable mirrored slot if found, iterate EFI memory map and pick the
mirrored region to process for adding candidate of randomization slot. If
no suitable mirrored region found, alloc memory from non-mirrored regions.

Signed-off-by: Ma Wupeng <mawupeng1@huawei.com>
---
 drivers/firmware/efi/libstub/arm64-stub.c  | 52 +++++++++++++++++++++-
 drivers/firmware/efi/libstub/efistub.h     |  7 ++-
 drivers/firmware/efi/libstub/randomalloc.c | 13 +++++-
 3 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c
index 9cc556013d08..39b774853b93 100644
--- a/drivers/firmware/efi/libstub/arm64-stub.c
+++ b/drivers/firmware/efi/libstub/arm64-stub.c
@@ -79,6 +79,51 @@ static bool check_image_region(u64 base, u64 size)
 	return ret;
 }
 
+/* check if system has suitable for kernel to relocate */
+static bool check_mirror_suitable(unsigned long size,
+				   unsigned long align)
+{
+	unsigned long map_size, desc_size;
+	unsigned long buff_size;
+	efi_status_t status;
+	efi_memory_desc_t *memory_map;
+	int map_offset;
+	struct efi_boot_memmap map;
+	bool found = false;
+
+	map.map = &memory_map;
+	map.map_size = &map_size;
+	map.desc_size = &desc_size;
+	map.desc_ver = NULL;
+	map.key_ptr = NULL;
+	map.buff_size = &buff_size;
+
+	status = efi_get_memory_map(&map);
+	if (status != EFI_SUCCESS)
+		return false;
+
+	if (align < EFI_ALLOC_ALIGN)
+		align = EFI_ALLOC_ALIGN;
+
+	size = round_up(size, EFI_ALLOC_ALIGN);
+
+	for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
+		efi_memory_desc_t *md = (void *)memory_map + map_offset;
+		unsigned long slots;
+
+		/* system has suiable mirrored area */
+		slots = get_entry_num_slots(md, size, ilog2(align));
+		if (slots > 0 && md->attribute & EFI_MEMORY_MORE_RELIABLE) {
+			found = true;
+			break;
+		}
+	}
+
+	efi_bs_call(free_pool, memory_map);
+
+	return found;
+}
+
 efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *image_size,
 				 unsigned long *reserve_addr,
@@ -88,6 +133,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	efi_status_t status;
 	unsigned long kernel_size, kernel_memsize = 0;
 	u32 phys_seed = 0;
+	bool efi_mirror_found;
 
 	/*
 	 * Although relocatable kernels can fix up the misalignment with
@@ -127,13 +173,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	kernel_memsize = kernel_size + (_end - _edata);
 	*reserve_size = kernel_memsize;
 
+	efi_mirror_found = check_mirror_suitable(*reserve_size, min_kimg_align);
+
 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
 		/*
 		 * If KASLR is enabled, and we have some randomness available,
 		 * locate the kernel at a randomized offset in physical memory.
 		 */
 		status = efi_random_alloc(*reserve_size, min_kimg_align,
-					  reserve_addr, phys_seed);
+					  reserve_addr, phys_seed,
+					  efi_mirror_found);
 		if (status != EFI_SUCCESS)
 			efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
 	} else {
@@ -163,6 +212,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 		}
 	}
 
+out:
 	*image_addr = *reserve_addr;
 	memcpy((void *)*image_addr, _text, kernel_size);
 
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index edb77b0621ea..0cf2e25cb7d0 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -790,7 +790,8 @@ void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
 
 efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr, unsigned long random_seed);
+			      unsigned long *addr, unsigned long random_seed,
+			      bool efi_has_mirror);
 
 efi_status_t check_platform_features(void);
 
@@ -875,6 +876,10 @@ void efi_handle_post_ebs_state(void);
 
 enum efi_secureboot_mode efi_get_secureboot(void);
 
+extern unsigned long get_entry_num_slots(efi_memory_desc_t *md,
+					 unsigned long size,
+					 unsigned long align_shift);
+
 #ifdef CONFIG_RESET_ATTACK_MITIGATION
 void efi_enable_reset_attack_mitigation(void);
 #else
diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c
index 724155b9e10d..dd81d6c3c406 100644
--- a/drivers/firmware/efi/libstub/randomalloc.c
+++ b/drivers/firmware/efi/libstub/randomalloc.c
@@ -14,7 +14,7 @@
  * addresses it covers that are suitably aligned and supply enough room
  * for the allocation.
  */
-static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
+unsigned long get_entry_num_slots(efi_memory_desc_t *md,
 					 unsigned long size,
 					 unsigned long align_shift)
 {
@@ -53,7 +53,8 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
 efi_status_t efi_random_alloc(unsigned long size,
 			      unsigned long align,
 			      unsigned long *addr,
-			      unsigned long random_seed)
+			      unsigned long random_seed,
+			      bool efi_mirror_found)
 {
 	unsigned long map_size, desc_size, total_slots = 0, target_slot;
 	unsigned long buff_size;
@@ -83,6 +84,10 @@ efi_status_t efi_random_alloc(unsigned long size,
 		efi_memory_desc_t *md = (void *)memory_map + map_offset;
 		unsigned long slots;
 
+		if (efi_mirror_found &&
+		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
+			continue;
+
 		slots = get_entry_num_slots(md, size, ilog2(align));
 		MD_NUM_SLOTS(md) = slots;
 		total_slots += slots;
@@ -112,6 +117,10 @@ efi_status_t efi_random_alloc(unsigned long size,
 			continue;
 		}
 
+		if (efi_mirror_found &&
+		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
+			continue;
+
 		target = round_up(md->phys_addr, align) + target_slot * align;
 		pages = size / EFI_PAGE_SIZE;
 
-- 
2.25.1



  reply	other threads:[~2022-04-19  6:43 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-14 10:13 [PATCH v2 0/9] introduce mirrored memory support for arm64 Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 1/9] efi: Make efi_print_memmap() public Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 2/9] arm64: efi: Add fake memory support Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 3/9] efi: Make efi_find_mirror() public Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 4/9] arm64/mirror: arm64 enabling - find mirrored memory ranges Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 5/9] mm: Ratelimited mirrored memory related warning messages Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 6/9] mm: Demote warning message in vmemmap_verify() to debug level Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 7/9] mm: Calc the right pfn if page size is not 4K Wupeng Ma
2022-04-19  9:59   ` David Hildenbrand
2022-04-19 11:14   ` Anshuman Khandual
2022-04-19 18:29     ` Ard Biesheuvel
2022-04-20 22:27       ` Andrew Morton
2022-04-20 22:39         ` Ard Biesheuvel
2022-04-14 10:13 ` [PATCH v2 8/9] efi: Disable mirror feature if kernelcore is not specified Wupeng Ma
2022-04-14 10:13 ` [PATCH v2 9/9] ia64/efi: Code simplification in efi_init Wupeng Ma
2022-04-14 10:22 ` [PATCH v2 0/9] introduce mirrored memory support for arm64 Ard Biesheuvel
2022-04-16  1:32   ` mawupeng
2022-04-19 18:32     ` Ard Biesheuvel
2022-04-20  2:13       ` mawupeng
2022-04-19  7:01   ` [PATCH 0/2] Add support to relocate kernel image to mirrored region Wupeng Ma
2022-04-19  7:01     ` Wupeng Ma [this message]
2022-04-19  7:01     ` [PATCH 2/2] arm64/boot: Add support to relocate kernel image to mirrored region without kaslr Wupeng Ma
2022-05-03  9:58     ` [PATCH 0/2] Add support to relocate kernel image to mirrored region Ard Biesheuvel
2022-05-07  9:28       ` mawupeng
2022-05-19 11:09         ` mawupeng
2022-05-20  6:52           ` Ard Biesheuvel
2022-05-20  6:52             ` Ard Biesheuvel
2022-05-23 11:48               ` mawupeng
2022-05-23  1:18             ` mawupeng
2022-05-23 14:41               ` Ard Biesheuvel
2022-05-24  1:11                 ` mawupeng

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=20220419070150.254377-2-mawupeng1@huawei.com \
    --to=mawupeng1@huawei.com \
    --cc=W_Armin@gmx.de \
    --cc=akpm@linux-foundation.org \
    --cc=andy@infradead.org \
    --cc=anshuman.khandual@arm.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=chenhuacai@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@redhat.com \
    --cc=dvhart@infradead.org \
    --cc=frederic@kernel.org \
    --cc=gpiccoli@igalia.com \
    --cc=john.garry@huawei.com \
    --cc=jroedel@suse.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-ia64@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=macro@orcam.me.uk \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=rppt@kernel.org \
    --cc=seanjc@google.com \
    --cc=songmuchun@bytedance.com \
    --cc=tglx@linutronix.de \
    --cc=tsbogend@alpha.franken.de \
    --cc=wangkefeng.wang@huawei.com \
    --cc=will@kernel.org \
    --cc=x86@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