Skip to contentSkip to conversation

x86/xpti: Hide almost all of .text and all .data/.rodata/.bss mappings

PATCH In review Andrew Cooper 2 participants 2 messages Feb 14, 2018 – Feb 15, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/xpti: Hide almost all of .text and all .data/.rodata/.bss mappings

The current XPTI implementation isolates the directmap (and therefore a lot of guest data), but a large quantity of CPU0's state (including its stack) remains visible.

Furthermore, an attacker able to read .text is in a vastly superior position to normal when it comes to fingerprinting Xen for known vulnerabilities, or scanning for ROP/Spectre gadgets.

Collect together the entrypoints in .text.entry (currently 3x4k frames, but can almost certainly be slimmed down), and create a common mapping which is inserted into each per-cpu shadow. The stubs are also inserted into this mapping by pointing at the in-use L2. This allows stubs allocated later (SMP boot, or CPU hotplug) to work without further changes to the common mappings.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>

v2:
* Drop "incomplete" warning from the docs. This is about as complete as it can reasonably get.
* Correct BUILD_BUG_ON() calculation, duplicate in the common_pgt code
* scope/const improvements
* Use .push/.popsection in preference to .previous
* Exclude {compat_}create_bounce_frame from .text.entry
* Extend the sanity checking of linear in clone_mapping(). There is a latent bug where a bad linear parameter would cause Xen to fall over a NULL pointer.

---
 docs/misc/xen-command-line.markdown |  3 --
 xen/arch/x86/smpboot.c              | 56 +++++++++++++++++++++++++++++++++----
 xen/arch/x86/x86_64/compat/entry.S  |  5 ++++
 xen/arch/x86/x86_64/entry.S         | 11 ++++++--
 xen/arch/x86/xen.lds.S              |  7 +++++
 5 files changed, 71 insertions(+), 11 deletions(-)
docs/misc/xen-command-line.markdown +0 -3
index 79feba6..8317639 100644
@@ -1935,9 +1935,6 @@ mode.
 Override default selection of whether to isolate 64-bit PV guest page
 tables.
 
-** WARNING: Not yet a complete isolation implementation, but better than
-nothing. **
-
 ### xsave
 > `= <boolean>`
 
xen/arch/x86/smpboot.c +50 -6

85 lines not on this page · raw message

xen/arch/x86/x86_64/compat/entry.S +5 -0
index 707c746..458d810 100644
@@ -13,6 +13,8 @@
 #include <public/xen.h>
 #include <irq_vectors.h>
 
+        .section .text.entry, "ax", @progbits
+
 ENTRY(entry_int82)
         ASM_CLAC
         pushq $0
@@ -270,6 +272,9 @@ ENTRY(compat_int80_direct_trap)
         call  compat_create_bounce_frame
         jmp   compat_test_all_events
 
+        /* compat_create_bounce_frame & helpers don't need to be in .text.entry */
+        .text
+
 /* CREATE A BASIC EXCEPTION FRAME ON GUEST OS (RING-1) STACK:            */
 /*   {[ERRCODE,] EIP, CS, EFLAGS, [ESP, SS]}                             */
 /* %rdx: trap_bounce, %rbx: struct vcpu                                  */
xen/arch/x86/x86_64/entry.S +9 -2
index 58f652d..941f06f 100644
@@ -14,6 +14,8 @@
 #include <public/xen.h>
 #include <irq_vectors.h>
 
+        .section .text.entry, "ax", @progbits
+
 /* %rbx: struct vcpu */
 ENTRY(switch_to_kernel)
         leaq  VCPU_trap_bounce(%rbx),%rdx
@@ -357,6 +359,9 @@ int80_slow_path:
         subq  $2,UREGS_rip(%rsp)
         jmp   handle_exception_saved
 
+        /* create_bounce_frame & helpers don't need to be in .text.entry */
+        .text
+
 /* CREATE A BASIC EXCEPTION FRAME ON GUEST OS STACK:                     */
 /*   { RCX, R11, [ERRCODE,] RIP, CS, RFLAGS, RSP, SS }                   */
 /* %rdx: trap_bounce, %rbx: struct vcpu                                  */
@@ -487,6 +492,8 @@ ENTRY(dom_crash_sync_extable)
         jmp   asm_domain_crash_synchronous /* Does not return */
         .popsection
 
+        .section .text.entry, "ax", @progbits
+
 ENTRY(common_interrupt)
         SAVE_ALL CLAC
 
@@ -845,7 +852,7 @@ GLOBAL(trap_nop)
         iretq
 
 /* Table of automatically generated entry points.  One per vector. */
-        .section .init.rodata, "a", @progbits
+        .pushsection .init.rodata, "a", @progbits
 GLOBAL(autogen_entrypoints)
         /* pop into the .init.rodata section and record an entry point. */
         .macro entrypoint ent
@@ -854,7 +861,7 @@ GLOBAL(autogen_entrypoints)
         .popsection
         .endm
 
-        .text
+        .popsection
 autogen_stubs: /* Automatically generated stubs. */
 
         vec = 0
xen/arch/x86/xen.lds.S +7 -0
index 0952980..25c6cbc 100644
@@ -67,6 +67,13 @@ SECTIONS
        *(.text)
        *(.text.__x86_indirect_thunk_*)
        *(.text.page_aligned)
+
+       . = ALIGN(PAGE_SIZE);
+       _stextentry = .;
+       *(.text.entry)
+       . = ALIGN(PAGE_SIZE);
+       _etextentry = .;
+
        *(.text.cold)
        *(.text.unlikely)
        *(.fixup)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 62 lines
Quoted 1 line
Quoted 1 line · On 14.02.18 at 17:23, <[email protected]> wrote:

The current XPTI implementation isolates the directmap (and therefore a lot of
guest data), but a large quantity of CPU0's state (including its stack) remains visible.

Furthermore, an attacker able to read .text is in a vastly superior position to normal when it comes to fingerprinting Xen for known vulnerabilities, or scanning for ROP/Spectre gadgets.

Collect together the entrypoints in .text.entry (currently 3x4k frames, but can almost certainly be slimmed down), and create a common mapping which is inserted into each per-cpu shadow. The stubs are also inserted into this mapping by pointing at the in-use L2. This allows stubs allocated later (SMP boot, or CPU hotplug) to work without further changes to the common mappings.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>

v2:
* Drop "incomplete" warning from the docs. This is about as complete as it can reasonably get.
* Correct BUILD_BUG_ON() calculation, duplicate in the common_pgt code
* scope/const improvements
* Use .push/.popsection in preference to .previous
* Exclude {compat_}create_bounce_frame from .text.entry
* Extend the sanity checking of linear in clone_mapping(). There is a
latent
bug where a bad linear parameter would cause Xen to fall over a NULL
pointer.

---
 docs/misc/xen-command-line.markdown |  3 --
 xen/arch/x86/smpboot.c              | 56 +++++++++++++++++++++++++++++++++----
 xen/arch/x86/x86_64/compat/entry.S  |  5 ++++
 xen/arch/x86/x86_64/entry.S         | 11 ++++++--
 xen/arch/x86/xen.lds.S              |  7 +++++
 5 files changed, 71 insertions(+), 11 deletions(-)
docs/misc/xen-command-line.markdown +0 -3
index 79feba6..8317639 100644
@@ -1935,9 +1935,6 @@ mode.
 Override default selection of whether to isolate 64-bit PV guest page
 tables.
 
-** WARNING: Not yet a complete isolation implementation, but better than
-nothing. **
-
 ### xsave
 > `= <boolean>`
 
xen/arch/x86/smpboot.c +0 -0
index 2ebef03..10bf2f3 100644

xen/arch/x86/smpboot.c unsigned long alloc_stub_page(unsigned int cpu, unsigned · line 627

Quoted 6 lines
xen/arch/x86/smpboot.c +2 -0
@@ -622,6 +622,9 @@ unsigned long alloc_stub_page(unsigned int cpu, unsigned 
 long *mfn)
         unmap_domain_page(memset(__map_domain_page(pg), 0xcc, PAGE_SIZE));
     }
 
+    /* Confirm that all stubs fit in a single L2 pagetable. */
+    BUILD_BUG_ON(NR_CPUS * PAGE_SIZE > (1u << L3_PAGETABLE_SHIFT));

It's a little confusing that the comment talks about L2 yet the check is on L3. May I suggest you either use L2_PAGETABLE_ENTRIES << L2_PAGETABLE_SHIFT or say "are covered by a single L3 entry" in the comment?

Quoted 11 lines
xen/arch/x86/smpboot.c +1 -3
@@ -646,13 +649,23 @@ static int clone_mapping(const void *ptr, root_pgentry_t *rpt)
 {
     unsigned long linear = (unsigned long)ptr, pfn;
     unsigned int flags;
-    l3_pgentry_t *pl3e = l4e_to_l3e(idle_pg_table[root_table_offset(linear)]) +
-                         l3_table_offset(linear);
+    l3_pgentry_t *pl3e;
     l2_pgentry_t *pl2e;
     l1_pgentry_t *pl1e;
 
-    if ( linear < DIRECTMAP_VIRT_START )

xen/arch/x86/smpboot.c static int clone_mapping(const void *ptr, root_pgentry_t *rpt) · line 664

Quoted 10 lines
xen/arch/x86/smpboot.c +9 -1
-        return 0;
+    /*
+     * Sanity check 'linear'.  We only allow cloning from the Xen virtual
+     * range, and in particular, only from the directmap and .text ranges.
+     */
+    if ( root_table_offset(linear) > ROOT_PAGETABLE_LAST_XEN_SLOT ||
+         root_table_offset(linear) < ROOT_PAGETABLE_FIRST_XEN_SLOT )
+        return -EINVAL;
+    if ( !(linear >= DIRECTMAP_VIRT_START ||
+           (linear >= XEN_VIRT_START && linear < XEN_VIRT_END)) )

Putting it this way is certainly fine, but generally I find using !() on logical || or && expressions less easy to understand than pushing the negation through the entire expression. Once having done so, it might even be debatable whether

    if ( linear < XEN_VIRT_START ||
         (linear >= XEN_VIRT_END && linear < DIRECTMAP_VIRT_START)) )
        return -EINVAL;

wouldn't be better.

With at least the first (comment vs code) aspect taken care of in both instances

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel