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

[RFC PATCH 4/5] x86/xen: Support memory KASLR for PV guests


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx, x86@xxxxxxxxxx
  • From: Teddy Astie <teddy.astie@xxxxxxxxxx>
  • Date: Mon, 30 Nov 2026 15:36:49 +0100
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=vates.tech header.i="@vates.tech" header.h="From:Subject:Date:Message-ID:To:Cc:MIME-Version:Content-Type:In-Reply-To:References:Feedback-ID"
  • Cc: Teddy Astie <teddy.astie@xxxxxxxxxx>, Juergen Gross <jgross@xxxxxxxx>, Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx>
  • Delivery-date: Mon, 30 Nov 2026 14:37:37 +0000
  • Feedback-id: default:8631fc262581453bbf619ec5b2062170:Sweego
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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

 


Rackspace

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