Skip to content

Commit 690eaa5

Browse files
Vidossuryasaimadhu
authored andcommitted
x86/boot/KASLR: Limit KASLR to extract the kernel in immovable memory only
KASLR may randomly choose a range which is located in movable memory regions. As a result, this will break memory hotplug and make the movable memory chosen by KASLR immovable. Therefore, limit KASLR to choose memory regions in the immovable range after consulting the SRAT table. [ bp: - Rewrite commit message. - Trim comments. ] Signed-off-by: Chao Fan <fanc.fnst@cn.fujitsu.com> Signed-off-by: Borislav Petkov <bp@suse.de> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Baoquan He <bhe@redhat.com> Cc: caoj.fnst@cn.fujitsu.com Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: indou.takao@jp.fujitsu.com Cc: Ingo Molnar <mingo@redhat.com> Cc: Juergen Gross <jgross@suse.com> Cc: kasong@redhat.com Cc: Kees Cook <keescook@chromium.org> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: msys.mizuma@gmail.com Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: x86-ml <x86@kernel.org> Link: https://lkml.kernel.org/r/20190123110850.12433-8-fanc.fnst@cn.fujitsu.com
1 parent 02a3e3c commit 690eaa5

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

arch/x86/boot/compressed/kaslr.c

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ static bool memmap_too_large;
9797
/* Store memory limit specified by "mem=nn[KMG]" or "memmap=nn[KMG]" */
9898
static unsigned long long mem_limit = ULLONG_MAX;
9999

100+
/* Number of immovable memory regions */
101+
static int num_immovable_mem;
100102

101103
enum mem_avoid_index {
102104
MEM_AVOID_ZO_RANGE = 0,
@@ -413,6 +415,9 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
413415
/* Mark the memmap regions we need to avoid */
414416
handle_mem_options();
415417

418+
/* Enumerate the immovable memory regions */
419+
num_immovable_mem = count_immovable_mem_regions();
420+
416421
#ifdef CONFIG_X86_VERBOSE_BOOTUP
417422
/* Make sure video RAM can be used. */
418423
add_identity_map(0, PMD_SIZE);
@@ -568,9 +573,9 @@ static unsigned long slots_fetch_random(void)
568573
return 0;
569574
}
570575

571-
static void process_mem_region(struct mem_vector *entry,
572-
unsigned long minimum,
573-
unsigned long image_size)
576+
static void __process_mem_region(struct mem_vector *entry,
577+
unsigned long minimum,
578+
unsigned long image_size)
574579
{
575580
struct mem_vector region, overlap;
576581
unsigned long start_orig, end;
@@ -646,6 +651,56 @@ static void process_mem_region(struct mem_vector *entry,
646651
}
647652
}
648653

654+
static bool process_mem_region(struct mem_vector *region,
655+
unsigned long long minimum,
656+
unsigned long long image_size)
657+
{
658+
int i;
659+
/*
660+
* If no immovable memory found, or MEMORY_HOTREMOVE disabled,
661+
* use @region directly.
662+
*/
663+
if (!num_immovable_mem) {
664+
__process_mem_region(region, minimum, image_size);
665+
666+
if (slot_area_index == MAX_SLOT_AREA) {
667+
debug_putstr("Aborted e820/efi memmap scan (slot_areas full)!\n");
668+
return 1;
669+
}
670+
return 0;
671+
}
672+
673+
#ifdef CONFIG_MEMORY_HOTREMOVE
674+
/*
675+
* If immovable memory found, filter the intersection between
676+
* immovable memory and @region.
677+
*/
678+
for (i = 0; i < num_immovable_mem; i++) {
679+
unsigned long long start, end, entry_end, region_end;
680+
struct mem_vector entry;
681+
682+
if (!mem_overlaps(region, &immovable_mem[i]))
683+
continue;
684+
685+
start = immovable_mem[i].start;
686+
end = start + immovable_mem[i].size;
687+
region_end = region->start + region->size;
688+
689+
entry.start = clamp(region->start, start, end);
690+
entry_end = clamp(region_end, start, end);
691+
entry.size = entry_end - entry.start;
692+
693+
__process_mem_region(&entry, minimum, image_size);
694+
695+
if (slot_area_index == MAX_SLOT_AREA) {
696+
debug_putstr("Aborted e820/efi memmap scan when walking immovable regions(slot_areas full)!\n");
697+
return 1;
698+
}
699+
}
700+
return 0;
701+
#endif
702+
}
703+
649704
#ifdef CONFIG_EFI
650705
/*
651706
* Returns true if mirror region found (and must have been processed
@@ -711,11 +766,8 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
711766

712767
region.start = md->phys_addr;
713768
region.size = md->num_pages << EFI_PAGE_SHIFT;
714-
process_mem_region(&region, minimum, image_size);
715-
if (slot_area_index == MAX_SLOT_AREA) {
716-
debug_putstr("Aborted EFI scan (slot_areas full)!\n");
769+
if (process_mem_region(&region, minimum, image_size))
717770
break;
718-
}
719771
}
720772
return true;
721773
}
@@ -742,11 +794,8 @@ static void process_e820_entries(unsigned long minimum,
742794
continue;
743795
region.start = entry->addr;
744796
region.size = entry->size;
745-
process_mem_region(&region, minimum, image_size);
746-
if (slot_area_index == MAX_SLOT_AREA) {
747-
debug_putstr("Aborted e820 scan (slot_areas full)!\n");
797+
if (process_mem_region(&region, minimum, image_size))
748798
break;
749-
}
750799
}
751800
}
752801

arch/x86/boot/compressed/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static inline acpi_physical_address get_rsdp_addr(void) { return 0; }
132132
#endif
133133

134134
#if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_MEMORY_HOTREMOVE)
135+
extern struct mem_vector immovable_mem[MAX_NUMNODES*2];
135136
int count_immovable_mem_regions(void);
136137
#else
137138
static inline int count_immovable_mem_regions(void) { return 0; }

0 commit comments

Comments
 (0)