Skip to content

Commit c7d2361

Browse files
thgarnieIngo Molnar
authored andcommitted
x86/mm/KASLR: Fix physical memory calculation on KASLR memory randomization
Initialize KASLR memory randomization after max_pfn is initialized. Also ensure the size is rounded up. It could create problems on machines with more than 1Tb of memory on certain random addresses. Signed-off-by: Thomas Garnier <thgarnie@google.com> Cc: Aleksey Makarov <aleksey.makarov@linaro.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Baoquan He <bhe@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Borislav Petkov <bp@suse.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dave Young <dyoung@redhat.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Fabian Frederick <fabf@skynet.be> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Joerg Roedel <jroedel@suse.de> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Lv Zheng <lv.zheng@intel.com> Cc: Mark Salter <msalter@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rafael J . Wysocki <rafael.j.wysocki@intel.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Toshi Kani <toshi.kani@hp.com> Cc: kernel-hardening@lists.openwall.com Fixes: 021182e ("Enable KASLR for physical mapping memory regions") Link: http://lkml.kernel.org/r/1470762665-88032-1-git-send-email-thgarnie@google.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 469f002 commit c7d2361

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

arch/x86/kernel/setup.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,6 @@ void __init setup_arch(char **cmdline_p)
936936

937937
x86_init.oem.arch_setup();
938938

939-
kernel_randomize_memory();
940-
941939
iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
942940
setup_memory_map();
943941
parse_setup_data();
@@ -1055,6 +1053,12 @@ void __init setup_arch(char **cmdline_p)
10551053

10561054
max_possible_pfn = max_pfn;
10571055

1056+
/*
1057+
* Define random base addresses for memory sections after max_pfn is
1058+
* defined and before each memory section base is used.
1059+
*/
1060+
kernel_randomize_memory();
1061+
10581062
#ifdef CONFIG_X86_32
10591063
/* max_low_pfn get updated here */
10601064
find_low_pfn_range();

arch/x86/mm/kaslr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void __init kernel_randomize_memory(void)
9797
* add padding if needed (especially for memory hotplug support).
9898
*/
9999
BUG_ON(kaslr_regions[0].base != &page_offset_base);
100-
memory_tb = ((max_pfn << PAGE_SHIFT) >> TB_SHIFT) +
100+
memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
101101
CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
102102

103103
/* Adapt phyiscal memory region size based on available memory */

0 commit comments

Comments
 (0)