Skip to contentSkip to conversation

xen/sm{e, a}p: allow disabling sm{e, a}p for Xen itself

PATCH Superseded He Chen 2 participants 2 messages Aug 31, 2016 – Sep 1, 2016

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

He Chen

author
#0 lore raw

PATCH v4 1/1 xen/sm{e, a}p: allow disabling sm{e, a}p for Xen itself

SMEP/SMAP is a security feature to prevent kernel executing/accessing user address involuntarily, any such behavior will lead to a page fault.

SMEP/SMAP is open (in CR4) for both Xen and HVM guest in earlier code. SMEP/SMAP bit set in Xen CR4 would enforce security checking for 32-bit PV guest which will suffer unknown SMEP/SMAP page fault when guest kernel attempt to access user address although SMEP/SMAP is close for PV guests.

This patch introduces a new boot option value "hvm" for "sm{e,a}p", it is going to diable SMEP/SMAP for Xen hypervisor while enable them for HVM. In this way, 32-bit PV guest will not suffer SMEP/SMAP security issue. Users can choose whether open SMEP/SMAP for Xen itself, especially when they are going to run 32-bit PV guests.

---
Changes in v4:
* introduce 2 new synthetic features X86_FEATURE_XEN_SMEP and X86_FEATURE_XEN_SMAP for Xen itself.
* adjust SM{E,A}P related instruction patching code.
* Commit message refinement.

Changes in v3:
* Fix boot options.
* Fix CR4 & mmu_cr4_features operations.
* Disable SMEP/SMAP for Dom0.
* Commit message refinement.

Changes in v2:
* Allow "hvm" as a value to "smep" and "smap" command line options.
* Clear SMEP/SMAP CPUID bits for pv guests if they are set to hvm only.
* Refine docs.
* Rewrite commit message.

---
 docs/misc/xen-command-line.markdown |  2 ++
 xen/arch/x86/setup.c                | 58 ++++++++++++++++++++++++++++++++-----
 xen/include/asm-x86/asm_defns.h     | 10 +++----
 xen/include/asm-x86/cpufeature.h    |  5 +++-
 4 files changed, 61 insertions(+), 14 deletions(-)
docs/misc/xen-command-line.markdown +2 -0
index 3a250cb..b15f3e7 100644
@@ -1433,6 +1433,7 @@ Set the serial transmit buffer size.
 > Default: `true`
 
 Flag to enable Supervisor Mode Execution Protection
+Use `smep=hvm` to enable SMEP for HVM guests only.
 
 ### smap
 > `= <boolean>`
@@ -1440,6 +1441,7 @@ Flag to enable Supervisor Mode Execution Protection
 > Default: `true`
 
 Flag to enable Supervisor Mode Access Prevention
+Use `smap=hvm` to enable SMAP for HVM guests only.
 
 ### snb\_igd\_quirk
 > `= <boolean> | cap | <integer>`
xen/arch/x86/setup.c +50 -8

94 lines not on this page · raw message

xen/include/asm-x86/asm_defns.h +5 -5
index e36e78f..f1c6fa1 100644
@@ -205,7 +205,7 @@ void ret_from_intr(void);
         .popsection;                                                   \
         .pushsection .altinstructions, "a";                            \
         altinstruction_entry 661b, 661b, X86_FEATURE_ALWAYS, 3, 0;     \
-        altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3;       \
+        altinstruction_entry 661b, 662b, X86_FEATURE_XEN_SMAP, 3, 3;       \
         .popsection
 
 #define ASM_STAC ASM_AC(STAC)
@@ -217,21 +217,21 @@ void ret_from_intr(void);
         668: call cr4_pv32_restore;                                \
         .section .altinstructions, "a";                            \
         altinstruction_entry 667b, 667b, X86_FEATURE_ALWAYS, 5, 0; \
-        altinstruction_entry 667b, 668b, X86_FEATURE_SMEP, 5, 5;   \
-        altinstruction_entry 667b, 668b, X86_FEATURE_SMAP, 5, 5;   \
+        altinstruction_entry 667b, 668b, X86_FEATURE_XEN_SMEP, 5, 5;   \
+        altinstruction_entry 667b, 668b, X86_FEATURE_XEN_SMAP, 5, 5;   \
         .popsection
 
 #else
 static always_inline void clac(void)
 {
     /* Note: a barrier is implicit in alternative() */
-    alternative(ASM_NOP3, __stringify(__ASM_CLAC), X86_FEATURE_SMAP);
+    alternative(ASM_NOP3, __stringify(__ASM_CLAC), X86_FEATURE_XEN_SMAP);
 }
 
 static always_inline void stac(void)
 {
     /* Note: a barrier is implicit in alternative() */
-    alternative(ASM_NOP3, __stringify(__ASM_STAC), X86_FEATURE_SMAP);
+    alternative(ASM_NOP3, __stringify(__ASM_STAC), X86_FEATURE_XEN_SMAP);
 }
 #endif
 
xen/include/asm-x86/cpufeature.h +4 -1
index bcdf5d6..a0b5b06 100644
@@ -17,6 +17,8 @@ XEN_CPUFEATURE(CPUID_FAULTING,  (FSCAPINTS+0)*32+ 6) /* cpuid faulting */
 XEN_CPUFEATURE(CLFLUSH_MONITOR, (FSCAPINTS+0)*32+ 7) /* clflush reqd with monitor */
 XEN_CPUFEATURE(APERFMPERF,      (FSCAPINTS+0)*32+ 8) /* APERFMPERF */
 XEN_CPUFEATURE(MFENCE_RDTSC,    (FSCAPINTS+0)*32+ 9) /* MFENCE synchronizes RDTSC */
+XEN_CPUFEATURE(XEN_SMEP,        (FSCAPINTS+0)*32+ 10) /* SMEP gets used by Xen itself */
+XEN_CPUFEATURE(XEN_SMAP,        (FSCAPINTS+0)*32+ 11) /* SMAP gets used by Xen itself */
 
 #define NCAPINTS (FSCAPINTS + 1) /* N 32-bit words worth of info */
 
@@ -67,7 +69,8 @@ XEN_CPUFEATURE(MFENCE_RDTSC,    (FSCAPINTS+0)*32+ 9) /* MFENCE synchronizes RDTS
 #define cpu_has_fsgsbase	boot_cpu_has(X86_FEATURE_FSGSBASE)
 #define cpu_has_aperfmperf	boot_cpu_has(X86_FEATURE_APERFMPERF)
 #define cpu_has_smep            boot_cpu_has(X86_FEATURE_SMEP)
-#define cpu_has_smap            boot_cpu_has(X86_FEATURE_SMAP)
+#define cpu_has_xen_smep        boot_cpu_has(X86_FEATURE_XEN_SMEP)
+#define cpu_has_xen_smap        boot_cpu_has(X86_FEATURE_XEN_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) \
                                  && boot_cpu_has(X86_FEATURE_FFXSR))
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

docs/misc/xen-command-line.markdown Set the serial transmit buffer size. · line 1436

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 31.08.16 at 11:49, <[email protected]> wrote:
docs/misc/xen-command-line.markdown +1 -0
@@ -1433,6 +1433,7 @@ Set the serial transmit buffer size.
 > Default: `true`
 
 Flag to enable Supervisor Mode Execution Protection
+Use `smep=hvm` to enable SMEP for HVM guests only.

I'm sorry for noticing this only now, but this wording is potentially
confusing: Xen doesn't enable SMEP (or SMAP) for HVM guests. It only _allows_ such guests to enable it for themselves. I.e. how about

Use smep=hvm to allow SMEP use by HVM guests only.

Quoted 6 lines
xen/arch/x86/setup.c +0 -0
@@ -62,12 +62,12 @@ static unsigned int __initdata max_cpus;
 integer_param("maxcpus", max_cpus);
 
 /* smep: Enable/disable Supervisor Mode Execution Protection (default on). */

xen/arch/x86/setup.c static unsigned int __initdata max_cpus; · line 70

Quoted 10 lines
xen/arch/x86/setup.c +4 -4
-static bool_t __initdata opt_smep = 1;
-boolean_param("smep", opt_smep);
+static void parse_smep_param(char *s);
+custom_param("smep", parse_smep_param);
 
 /* smap: Enable/disable Supervisor Mode Access Prevention (default on). */
-static bool_t __initdata opt_smap = 1;
-boolean_param("smap", opt_smap);
+static void parse_smap_param(char *s);
+custom_param("smap", parse_smap_param);

Already on v3 I did tell you that you hadn't reacted on certain of the the v2 review comments. And now I see that e.g. you still use forward declarations here when you don't really need to.

xen/arch/x86/setup.c struct cpuinfo_x86 __read_mostly boot_cpu_data = { 0, 0, · line 116

Quoted 7 lines
xen/arch/x86/setup.c +2 -0
@@ -111,6 +111,48 @@ struct cpuinfo_x86 __read_mostly boot_cpu_data = { 0, 0, 
 0, 0, -1 };
 
 unsigned long __read_mostly mmu_cr4_features = XEN_MINIMAL_CR4;
 
+#define SMEP_HVM_ONLY (-1)
+static int __initdata opt_smep = 1;

s8 would do.

Quoted 4 lines
xen/arch/x86/setup.c +4 -0
+static void __init parse_smep_param(char *s)
+{
+    if ( !strcmp(s, "hvm") )
+    {

xen/arch/x86/setup.c struct cpuinfo_x86 __read_mostly boot_cpu_data = { 0, 0, 0, 0, -1 }; · line 129

Quoted 10 lines
xen/arch/x86/setup.c +10 -0
+        opt_smep = SMEP_HVM_ONLY;
+    }
+    else if ( !parse_bool(s) )
+    {
+        opt_smep = 0;
+    }
+    else if ( parse_bool(s) && opt_smep != SMEP_HVM_ONLY )
+    {
+        opt_smep = 1;
+    }

All the braces are pointless here. And I still don't agree with the flow, especially with the double invocation of parse_bool(). Why don't you invoke it once first thing, and then properly handle its tristate return value? That'll then also hopefully (and finally) result in you no longer making not work certain multiple specifications of "smep=" on the command line (right now it looks like "smep=hvm smep=1" would not have the intended effect).

xen/arch/x86/setup.c XEN_CPUFEATURE(MFENCE_RDTSC, (FSCAPINTS+0)*32+ 9) /* MFENCE synchronizes RDTS · line 73

Quoted 7 lines
xen/arch/x86/setup.c +2 -1
@@ -67,7 +69,8 @@ XEN_CPUFEATURE(MFENCE_RDTSC,    (FSCAPINTS+0)*32+ 9) /* MFENCE synchronizes RDTS
 #define cpu_has_fsgsbase	boot_cpu_has(X86_FEATURE_FSGSBASE)
 #define cpu_has_aperfmperf	boot_cpu_has(X86_FEATURE_APERFMPERF)
 #define cpu_has_smep            boot_cpu_has(X86_FEATURE_SMEP)
-#define cpu_has_smap            boot_cpu_has(X86_FEATURE_SMAP)
+#define cpu_has_xen_smep        boot_cpu_has(X86_FEATURE_XEN_SMEP)
+#define cpu_has_xen_smap        boot_cpu_has(X86_FEATURE_XEN_SMAP)

Why do you remove cpu_has_smap but not also cpu_has_smep?

And the newly added shorthands are misnamed: You really mean e.g. cpu_using_smep. But then I think not having these short hands would be even better, the more that specifically the smep one is being used in exactly one place.

Jan

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