|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [RFC PATCH 4/5] x86/xen: Support memory KASLR for PV guests
Memory KASLR (CONFIG_RANDOMIZE_MEMORY) is never enabled for Xen PV
guests as KASLR_FLAG is normally set by the decompressor, which isn't
used for PV. Just setting it isn't enough though: kernel_randomize_memory()
then moves PAGE_OFFSET in setup_arch(), while the direct map already
built by xen_setup_kernel_pagetable() is at the default location, and
the guest crashes on the next __va() access.
Randomize the memory regions in xen_start_kernel() instead, right before
the initial page tables are built. Handle "nokaslr" there, as nothing
else does it for PV.
As the final memory map is only known later (xen_memory_setup()), size
the direct map region for the largest possible one (MAXMEM). This costs
some entropy, similarly to what CONFIG_ZONE_DEVICE does.
Tested as PV domU (1G and 16G, SMP) and PV dom0 with upstream Xen, with
and without "nokaslr": direct map, vmalloc and vmemmap bases change
across boots.
Signed-off-by: Teddy Astie <teddy.astie@xxxxxxxxxx>
Assisted-by: LLM
---
CC: Juergen Gross <jgross@xxxxxxxx>
CC: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>
arch/x86/xen/enlighten_pv.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
index 4a37d493f57c..4231eb7d4ad7 100644
--- a/arch/x86/xen/enlighten_pv.c
+++ b/arch/x86/xen/enlighten_pv.c
@@ -64,6 +64,8 @@
#include <asm/msr.h>
#include <asm/traps.h>
#include <asm/setup.h>
+#include <asm/cmdline.h>
+#include <asm/kaslr.h>
#include <asm/desc.h>
#include <asm/pgalloc.h>
#include <asm/tlbflush.h>
@@ -1324,6 +1326,31 @@ static void __init xen_domu_set_legacy_features(void)
extern void early_xen_iret_patch(void);
+/*
+ * Unlike on bare metal, where the direct map is populated on demand until
+ * init_mem_mapping(), Xen PV builds it in xen_setup_kernel_pagetable() and
+ * uses it right away. So the memory regions have to be randomized before
+ * that, rather than in setup_arch().
+ */
+static void __init xen_randomize_memory(void)
+{
+ unsigned long saved_max_pfn = max_pfn;
+
+ if (!IS_ENABLED(CONFIG_RANDOMIZE_MEMORY) ||
+ cmdline_find_option_bool(xen_start_info->cmd_line, "nokaslr"))
+ return;
+
+ boot_params.hdr.loadflags |= KASLR_FLAG;
+
+ /*
+ * The final memory map isn't known yet (see xen_memory_setup()), so
+ * size the direct map region for the largest one possible.
+ */
+ max_pfn = PFN_DOWN(MAXMEM);
+ kernel_randomize_memory();
+ max_pfn = saved_max_pfn;
+}
+
/* First C function to be called on Xen boot */
asmlinkage __visible void __init xen_start_kernel(struct start_info *si)
{
@@ -1472,6 +1499,8 @@ asmlinkage __visible void __init xen_start_kernel(struct
start_info *si)
local_irq_disable();
+ xen_randomize_memory();
+
xen_raw_console_write("mapping kernel into physical memory\n");
xen_setup_kernel_pagetable((pgd_t *)xen_start_info->pt_base,
xen_start_info->nr_pages);
--
2.56.1
--
Teddy Astie | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |