Skip to contentSkip to conversation

x86: Enable Supervisor Mode Access Prevention (SMAP) for Xen

PATCH Superseded Feng Wu 2 participants 2 messages May 6, 2014

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH v5 8/10 x86: Enable Supervisor Mode Access Prevention (SMAP) for Xen

Supervisor Mode Access Prevention (SMAP) is a new security feature disclosed by Intel, please refer to the following document:

http://software.intel.com/sites/default/files/319433-014.pdf

If CR4.SMAP = 1, supervisor-mode data accesses are not allowed to linear addresses that are accessible in user mode. If CPL < 3, SMAP protections are disabled if EFLAGS.AC = 1. If CPL = 3, SMAP applies to all supervisor-mode data accesses (these are implicit supervisor accesses) regardless of the value of EFLAGS.AC.

This patch enables SMAP in Xen to prevent Xen hypervisor from accessing pv guest data, whose translation paging-structure entries' U/S flags are all set.

---
 docs/misc/xen-command-line.markdown |  7 +++++
 xen/arch/x86/setup.c                | 29 +++++++++++++++++++
 xen/arch/x86/traps.c                | 58 +++++++++++++++++++++++++++----------
 xen/include/asm-x86/cpufeature.h    |  1 +
 xen/include/asm-x86/domain.h        |  6 ++--
 5 files changed, 84 insertions(+), 17 deletions(-)
docs/misc/xen-command-line.markdown +7 -0
index 7dc938b..a7ac53d 100644
@@ -912,6 +912,13 @@ Set the serial transmit buffer size.
 
 Flag to enable Supervisor Mode Execution Protection
 
+### smap
+> `= <boolean>`
+
+> Default: `true`
+
+Flag to enable Supervisor Mode Access Prevention
+
 ### snb\_igd\_quirk
 > `= <boolean>`
 
xen/arch/x86/setup.c +29 -0
index 2e30701..b5c88c3 100644
@@ -61,6 +61,10 @@ integer_param("maxcpus", max_cpus);
 static bool_t __initdata disable_smep;
 invbool_param("smep", disable_smep);
 
+/* smap: Enable/disable Supervisor Mode Access Prevention (default on). */
+static bool_t __initdata disable_smap;
+invbool_param("smap", disable_smap);
+
 /* **** Linux config option: propagated to domain0. */
 /* "acpi=off":    Sisables both ACPI table parsing and interpreter. */
 /* "acpi=force":  Override the disable blacklist.                   */
@@ -1280,6 +1284,11 @@ void __init noreturn __start_xen(unsigned long mbi_p)
     if ( cpu_has_smep )
         set_in_cr4(X86_CR4_SMEP);
 
+    if ( disable_smap )
+        setup_clear_cpu_cap(X86_FEATURE_SMAP);
+    if ( cpu_has_smap )
+        set_in_cr4(X86_CR4_SMAP);
+
     if ( cpu_has_fsgsbase )
         set_in_cr4(X86_CR4_FSGSBASE);
 
@@ -1386,6 +1395,23 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                initrdidx);
 
     /*
+     * Temporarily clear SMAP bit in CR4 to allow the user-page accesses in
+     * construct_dom0(). This looks a little hackish, but using stac()/clac()
+     * to do this is not so proper here, since in construct_dom0(),
+     * copy_from_user() is call, so we cannot make construct_dom0() wrapped
+     * by stac()/clac() as a whole, or the AC bit will remain cleared after
+     * copy_from_user() in construct_dom0(), this will induce problems.
+     *
+     * On the other hand, if we call stac()/clac() in construct_dom0(), we
+     * must carefully handle some concer cases, since there are many branches
+     * in it.
+     *
+     * So we clear SMAP before construct_dom0() and set it back after it.
+     */
+    if ( cpu_has_smap )
+        write_cr4(read_cr4() & ~X86_CR4_SMAP);
+
+    /*
      * We're going to setup domain0 using the module(s) that we stashed safely
      * above our heap. The second module, if present, is an initrd ramdisk.
      */
@@ -1395,6 +1421,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                         bootstrap_map, cmdline) != 0)
         panic("Could not set up DOM0 guest OS");
 
+    if ( cpu_has_smap )
+        write_cr4(read_cr4() | X86_CR4_SMAP);
+
     /* Scrub RAM that is still free and so may go to an unprivileged domain. */
     scrub_heap_pages();
 
xen/arch/x86/traps.c +43 -15

104 lines not on this page · raw message

xen/include/asm-x86/cpufeature.h +1 -0
index 20881c0..8014241 100644
@@ -190,6 +190,7 @@
 #define cpu_has_fsgsbase	boot_cpu_has(X86_FEATURE_FSGSBASE)
 
 #define cpu_has_smep            boot_cpu_has(X86_FEATURE_SMEP)
+#define cpu_has_smap            boot_cpu_has(X86_FEATURE_SMAP)
 #define cpu_has_fpu_sel         (!boot_cpu_has(X86_FEATURE_NO_FPU_SEL))
 
 #define cpu_has_ffxsr           ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) \
xen/include/asm-x86/domain.h +4 -2
index c5c266f..abf55fb 100644
@@ -467,12 +467,14 @@ unsigned long pv_guest_cr4_fixup(const struct vcpu *, unsigned long guest_cr4);
     (((v)->arch.pv_vcpu.ctrlreg[4]                          \
       | (mmu_cr4_features                                   \
          & (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_SMEP |      \
-            X86_CR4_OSXSAVE | X86_CR4_FSGSBASE))            \
+            X86_CR4_SMAP | X86_CR4_OSXSAVE |                \
+            X86_CR4_FSGSBASE))                              \
       | ((v)->domain->arch.vtsc ? X86_CR4_TSD : 0))         \
      & ~X86_CR4_DE)
 #define real_cr4_to_pv_guest_cr4(c)                         \
     ((c) & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_TSD |      \
-             X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE))
+             X86_CR4_OSXSAVE | X86_CR4_SMEP |               \
+             X86_CR4_FSGSBASE | X86_CR4_SMAP))
 
 void domain_cpuid(struct domain *d,
                   unsigned int  input,
Signature · 1 line
1.8.3.1
Reviewed-by on v5 8/10
#1 lore raw
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 06.05.14 at 12:10, <[email protected]> wrote:

Supervisor Mode Access Prevention (SMAP) is a new security feature disclosed by Intel, please refer to the following document:

http://software.intel.com/sites/default/files/319433-014.pdf

If CR4.SMAP = 1, supervisor-mode data accesses are not allowed to linear addresses that are accessible in user mode. If CPL < 3, SMAP protections are disabled if EFLAGS.AC = 1. If CPL = 3, SMAP applies to all supervisor-mode data accesses (these are implicit supervisor accesses) regardless of the value of EFLAGS.AC.

Quoted 5 lines

This patch enables SMAP in Xen to prevent Xen hypervisor from accessing pv guest data, whose translation paging-structure entries' U/S flags are all set.

however with the request to work on the comment below.

Quoted 7 lines
xen/arch/x86/setup.c +3 -0
@@ -1386,6 +1395,23 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                initrdidx);
 
     /*
+     * Temporarily clear SMAP bit in CR4 to allow the user-page accesses in
+     * construct_dom0(). This looks a little hackish, but using stac()/clac()
+     * to do this is not so proper here, since in construct_dom0(),

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1410

Quoted 10 lines
xen/arch/x86/setup.c +10 -0
+     * copy_from_user() is call, so we cannot make construct_dom0() wrapped
+     * by stac()/clac() as a whole, or the AC bit will remain cleared after
+     * copy_from_user() in construct_dom0(), this will induce problems.
+     *
+     * On the other hand, if we call stac()/clac() in construct_dom0(), we
+     * must carefully handle some concer cases, since there are many branches
+     * in it.
+     *
+     * So we clear SMAP before construct_dom0() and set it back after it.
+     */

Proposal:

+     * Temporarily clear SMAP bit in CR4 to allow user-page accesses in
+     * construct_dom0(). This looks a little hackish, but using stac()/clac()
+     * to do this is not appropriate here: Since in construct_dom0() calls
+     * copy_from_user(), so we cannot wrap construct_dom0() as a whole in
+     * stac()/clac() - the AC bit remaining cleared after copy_from_user()
+     * would induce problems in construct_dom0().
+     *
+     * On the other hand, if we used stac()/clac() in construct_dom0(), we
+     * would need to carefully handle some corner cases.
+     *
+     * So we clear SMAP before construct_dom0() and enable it back afterwards.

Jan