[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC PATCH 2/5] x86/mm/kaslr: Allow memory regions to be randomized before setup_arch()



kernel_randomize_memory() is called from setup_arch(), which works
because on bare metal, the direct map is only populated on demand by
the early page fault handler until init_mem_mapping().

This isn't the case for Xen PV, where the direct map is built early
in xen_start_kernel() and used right away, so the regions need to be
randomized before that.

Make kernel_randomize_memory() a no-op once the regions have been
randomized, so that an early caller isn't overridden by setup_arch().
Warn if the direct map region turns out to be too small for the final
max_pfn.

Signed-off-by: Teddy Astie <teddy.astie@xxxxxxxxxx>
Assisted-by: LLM
---
Perhaps the Xen PV specific comments can be stripped out ?

CC: Thomas Gleixner <tglx@xxxxxxxxxx>
CC: Ingo Molnar <mingo@xxxxxxxxxx>
CC: Borislav Petkov <bp@xxxxxxxxx>
CC: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
CC: "H. Peter Anvin" <hpa@xxxxxxxxx>
CC: Juergen Gross <jgross@xxxxxxxx>
CC: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>
---
 arch/x86/mm/kaslr.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 834641c6049a..1144bba63eef 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -69,6 +69,8 @@ static __initdata struct kaslr_memory_region {
  */
 unsigned long direct_map_physmem_end __ro_after_init;
 
+static bool memory_randomized __initdata;
+
 /* Get size in bytes used by the memory region */
 static inline unsigned long get_padding(struct kaslr_memory_region *region)
 {
@@ -85,6 +87,16 @@ void __init kernel_randomize_memory(void)
        unsigned long remain_entropy;
        unsigned long vmemmap_size;
 
+       /*
+        * Xen PV has to randomize the layout before setup_arch(), as its
+        * direct map is built (and used) right away, at the final location.
+        * Don't move the regions behind its back.
+        */
+       if (memory_randomized) {
+               WARN_ON(PFN_PHYS(max_pfn) - 1 > direct_map_physmem_end);
+               return;
+       }
+
        vaddr_start = pgtable_l5_enabled() ? __PAGE_OFFSET_BASE_L5 : 
__PAGE_OFFSET_BASE_L4;
        vaddr = vaddr_start;
 
@@ -166,6 +178,8 @@ void __init kernel_randomize_memory(void)
                vaddr = round_up(vaddr + 1, PUD_SIZE);
                remain_entropy -= entropy;
        }
+
+       memory_randomized = true;
 }
 
 void __meminit init_trampoline_kaslr(void)
-- 
2.56.1



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.