Skip to contentSkip to conversation

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

PATCH Superseded Feng Wu 3 participants 6 messages Apr 28, 2014

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH v3 7/9 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                | 13 +++++++++
 xen/arch/x86/traps.c                | 58 +++++++++++++++++++++++++++----------
 xen/include/asm-x86/cpufeature.h    |  1 +
 xen/include/asm-x86/domain.h        |  6 ++--
 5 files changed, 68 insertions(+), 17 deletions(-)
docs/misc/xen-command-line.markdown +7 -0
index 689ffe6..b9b38ad 100644
@@ -860,6 +860,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 +13 -0
index e9c2c51..e7db18f 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.                   */
@@ -1394,6 +1398,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                         bootstrap_map, cmdline) != 0)
         panic("Could not set up DOM0 guest OS");
 
+    /*
+     * Enable SMAP after constructing domain0, since there are lots of accesses to
+     * user pages in construct_dom0(), which is safe at the current stage.
+     */
+    if ( disable_smap )
+        setup_clear_cpu_cap(X86_FEATURE_SMAP);
+    if ( cpu_has_smap )
+        set_in_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 3dfb875..9a5b18d 100644
@@ -189,6 +189,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 4ff89f0..3b515f2 100644
@@ -464,12 +464,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
#1 lore raw

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

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:17, <[email protected]> wrote:
xen/arch/x86/setup.c +6 -0
@@ -1394,6 +1398,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                         bootstrap_map, cmdline) != 0)
         panic("Could not set up DOM0 guest OS");
 
+    /*
+     * Enable SMAP after constructing domain0, since there are lots of accesses to
+     * user pages in construct_dom0(), which is safe at the current stage.
+     */
+    if ( disable_smap )
+        setup_clear_cpu_cap(X86_FEATURE_SMAP);

You should not have moved this part - this should happen before APs get brought up.

xen/arch/x86/traps.c void do_page_fault(struct cpu_user_regs *regs) · line 1406

Quoted 11 lines
xen/arch/x86/traps.c +5 -2
@@ -1379,8 +1399,14 @@ void do_page_fault(struct cpu_user_regs *regs)
 
     if ( unlikely(!guest_mode(regs)) )
     {
-        pf_type = spurious_page_fault(addr, error_code);
-        BUG_ON(pf_type == smep_fault);
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault) )
+        {
+            console_start_sync();
+            printk("Xen %s violation\n", (pf_type == smep_fault) ? "SMEP" : "SMAP");

I know it's largely a matter of taste, but could I talk you into doing

            printk("Xen SM%cP violation\n", (pf_type == smep_fault) ? 'E' : 'A');

instead (unless others object)?

xen/arch/x86/traps.c void do_page_fault(struct cpu_user_regs *regs) · line 1439

Quoted 12 lines
xen/arch/x86/traps.c +4 -3
@@ -1406,10 +1432,12 @@ void do_page_fault(struct cpu_user_regs *regs)
 
     if ( unlikely(current->domain->arch.suppress_spurious_page_faults) )
     {
-        pf_type = spurious_page_fault(addr, error_code);
-        if ( pf_type == smep_fault )
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault))
         {
-            gdprintk(XENLOG_ERR, "Fatal SMEP fault\n");
+            printk(XENLOG_G_ERR "%p fatal %s fault\n",
+                   current, (pf_type == smep_fault) ? "SMEP" : "SMAP");

And similarly here then?

Jan

#2 lore raw
Quoted 27 lines · Jan Beulich

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

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:17, <[email protected]> wrote:
xen/arch/x86/setup.c +6 -0
@@ -1394,6 +1398,15 @@ void __init noreturn __start_xen(unsigned long mbi_p)
                         bootstrap_map, cmdline) != 0)
         panic("Could not set up DOM0 guest OS");
 
+    /*
+     * Enable SMAP after constructing domain0, since there are lots of accesses to
+     * user pages in construct_dom0(), which is safe at the current stage.
+     */
+    if ( disable_smap )
+        setup_clear_cpu_cap(X86_FEATURE_SMAP);

You should not have moved this part - this should happen before APs get brought up.

xen/arch/x86/traps.c void do_page_fault(struct cpu_user_regs *regs) · line 1406

Quoted 11 lines
xen/arch/x86/traps.c +5 -2
@@ -1379,8 +1399,14 @@ void do_page_fault(struct cpu_user_regs *regs)
 
     if ( unlikely(!guest_mode(regs)) )
     {
-        pf_type = spurious_page_fault(addr, error_code);
-        BUG_ON(pf_type == smep_fault);
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault) )
+        {
+            console_start_sync();
+            printk("Xen %s violation\n", (pf_type == smep_fault) ? "SMEP" : "SMAP");

I know it's largely a matter of taste, but could I talk you into doing

Quoted 3 lines
            printk("Xen SM%cP violation\n", (pf_type == smep_fault) ? 'E' : 'A');

instead (unless others object)?

I concur. Neither string is grepable.

Quoted 13 lines

xen/arch/x86/traps.c void do_page_fault(struct cpu_user_regs *regs) · line 1439

Quoted 13 lines
xen/arch/x86/traps.c +4 -3
@@ -1406,10 +1432,12 @@ void do_page_fault(struct cpu_user_regs *regs)
 
     if ( unlikely(current->domain->arch.suppress_spurious_page_faults) )
     {
-        pf_type = spurious_page_fault(addr, error_code);
-        if ( pf_type == smep_fault )
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault))
         {
-            gdprintk(XENLOG_ERR, "Fatal SMEP fault\n");
+            printk(XENLOG_G_ERR "%p fatal %s fault\n",
+                   current, (pf_type == smep_fault) ? "SMEP" : "SMAP");

That should be %pv rather than %p, which is an automagic format parameter which will get you "d<domid>v<vcpuid>" formatted.

Quoted 4 lines

And similarly here then?

Jan

~Andrew

author
#3 lore raw
Quoted 63 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: Monday, April 28, 2014 5:48 PM
To: Jan Beulich
Cc: Wu, Feng; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: Re: [PATCH v3 7/9] x86: Enable Supervisor Mode Access Prevention (SMAP) for Xen

Quoted 2 lines · Jan Beulich

Review void __init noreturn __start_xen(unsigned long

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:17, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -1394,6 +1398,15 @@ void __init noreturn __start_xen(unsigned long

mbi_p)

Quoted 5 lines

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

Quoted 5 lines
xen/arch/x86/setup.c +2 -0
                         bootstrap_map, cmdline) != 0)
         panic("Could not set up DOM0 guest OS");
 
+    /*
+     * Enable SMAP after constructing domain0, since there are lots of

accesses to

Quoted 9 lines

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

Quoted 4 lines
xen/arch/x86/setup.c +4 -0
+     * user pages in construct_dom0(), which is safe at the current stage.
+     */
+    if ( disable_smap )
+        setup_clear_cpu_cap(X86_FEATURE_SMAP);

You should not have moved this part - this should happen before APs get brought up.

Quoted 2 lines
(quoted diff) +0 -0
@@ -1379,8 +1399,14 @@ void do_page_fault(struct cpu_user_regs *regs)
 
Quoted 9 lines

xen/arch/x86/traps.c void do_page_fault(struct cpu_user_regs *regs)

Quoted 9 lines
xen/arch/x86/traps.c +5 -2
     if ( unlikely(!guest_mode(regs)) )
     {
-        pf_type = spurious_page_fault(addr, error_code);
-        BUG_ON(pf_type == smep_fault);
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault) )
+        {
+            console_start_sync();
+            printk("Xen %s violation\n", (pf_type == smep_fault) ?

"SMEP" : "SMAP");

Quoted 3 lines

I know it's largely a matter of taste, but could I talk you into doing

            printk("Xen SM%cP violation\n", (pf_type == smep_fault) ? 'E' :

'A');

Quoted 2 lines

instead (unless others object)?

I concur. Neither string is grepable.

Quoted 2 lines

Review void do_page_fault(struct cpu_user_regs

Quoted 2 lines
(quoted diff) +0 -0
@@ -1406,10 +1432,12 @@ void do_page_fault(struct cpu_user_regs

*regs)

Quoted 11 lines

xen/arch/x86/traps.c void do_page_fault(struct cpu_user_regs *regs)

Quoted 11 lines
xen/arch/x86/traps.c +4 -3
     if ( unlikely(current->domain->arch.suppress_spurious_page_faults) )
     {
-        pf_type = spurious_page_fault(addr, error_code);
-        if ( pf_type == smep_fault )
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault))
         {
-            gdprintk(XENLOG_ERR, "Fatal SMEP fault\n");
+            printk(XENLOG_G_ERR "%p fatal %s fault\n",
+                   current, (pf_type == smep_fault) ? "SMEP" :
Quoted 4 lines

"SMAP");

That should be %pv rather than %p, which is an automagic format parameter which will get you "d<domid>v<vcpuid>" formatted.

Thanks Andrew, I thought it was a typo in your last review:)

Quoted 7 lines
Quoted 5 lines

And similarly here then?

Jan

~Andrew

Thanks,
Feng

#4 lore raw
Quoted 10 lines · Feng
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
     if ( unlikely(current->domain->arch.suppress_spurious_page_faults) )
     {
xen/arch/x86/traps.c +4 -3
-        pf_type = spurious_page_fault(addr, error_code);
-        if ( pf_type == smep_fault )
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault))
         {
-            gdprintk(XENLOG_ERR, "Fatal SMEP fault\n");
+            printk(XENLOG_G_ERR "%p fatal %s fault\n",
+                   current, (pf_type == smep_fault) ? "SMEP" :
Quoted 5 lines
Quoted 4 lines

"SMAP");

That should be %pv rather than %p, which is an automagic format parameter which will get you "d<domid>v<vcpuid>" formatted.

Thanks Andrew, I thought it was a typo in your last review:)

We are slowly accumulating useful custom %p formatting, similar to Linux.

docs/misc/printk-formats.txt shows the current extensions supported.

~Andrew

author
#5 lore raw
Quoted 25 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: Monday, April 28, 2014 5:53 PM
To: Wu, Feng
Cc: Jan Beulich; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: Re: [PATCH v3 7/9] x86: Enable Supervisor Mode Access Prevention (SMAP) for Xen

Quoted 1 line · Feng
Quoted 1 line
Quoted 1 line
Quoted 1 line

if

( unlikely(current->domain->arch.suppress_spurious_page_faults) )

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines

xen/arch/x86/traps.c void do_page_fault(struct cpu_user_regs *regs)

Quoted 9 lines
     {
xen/arch/x86/traps.c +4 -3
-        pf_type = spurious_page_fault(addr, error_code);
-        if ( pf_type == smep_fault )
+        pf_type = spurious_page_fault(addr, regs);
+        if ( (pf_type == smep_fault) || (pf_type == smap_fault))
         {
-            gdprintk(XENLOG_ERR, "Fatal SMEP fault\n");
+            printk(XENLOG_G_ERR "%p fatal %s fault\n",
+                   current, (pf_type == smep_fault) ? "SMEP" :

"SMAP");

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

That should be %pv rather than %p, which is an automagic format parameter which will get you "d<domid>v<vcpuid>" formatted.

Thanks Andrew, I thought it was a typo in your last review:)

We are slowly accumulating useful custom %p formatting, similar to Linux.

docs/misc/printk-formats.txt shows the current extensions supported.

Thanks for the useful information!

Quoted 2 lines

~Andrew

Thanks,
Feng