Skip to contentSkip to conversation

KVM: enable Intel SMAP for KVM

PATCH In review Feng Wu 4 participants 18 messages Apr 1, 2014 – Apr 13, 2014

Subsystems (3)
lore
Share & tools
lore

Feng Wu

author
#0 lore raw

v4 cover KVM: enable Intel SMAP for KVM

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

Every access to a linear address is either a supervisor-mode access or a user-mode access. All accesses performed while the current privilege level (CPL) is less than 3 are supervisor-mode accesses. If CPL = 3, accesses are generally user-mode accesses. However, some operations implicitly access system data structures, and the resulting accesses to those data structures are supervisor-mode accesses regardless of CPL. Examples of such implicit supervisor accesses include the following:
accesses to the global descriptor table (GDT) or local descriptor table (LDT) to load a segment descriptor; accesses to the interrupt descriptor table (IDT) when delivering an interrupt or exception; and accesses to the task-state segment (TSS) as part of a task switch or change of CPL.

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 patchset pass-through SMAP feature to guests, and let guests benefit from it.

Version 1:
* Remove SMAP bit from CR4_RESERVED_BITS.
* Add SMAP support when setting CR4
* Disable SMAP for guests in EPT realmode and EPT unpaging mode
* Expose SMAP feature to guest

Version 2:
* Change the logic of updating mmu permission bitmap for SMAP violation
* Expose SMAP feature to guest in the last patch of this series.

Version 3:
* Changes in update_permission_bitmask().
* Use a branchless way suggested by Paolo Bonzini to detect SMAP violation in permission_fault().

Version 4:
* Changes to some comments and code style.

Feng Wu (4):
KVM: Remove SMAP bit from CR4_RESERVED_BITS.
KVM: Add SMAP support when setting CR4
KVM: Disable SMAP for guests in EPT realmode and EPT unpaging mode
KVM: expose SMAP feature to guest

 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/cpuid.c            |  2 +-
 arch/x86/kvm/cpuid.h            |  8 ++++++++
 arch/x86/kvm/mmu.c              | 34 ++++++++++++++++++++++++++++---
 arch/x86/kvm/mmu.h              | 44 +++++++++++++++++++++++++++++++++--------
 arch/x86/kvm/paging_tmpl.h      |  2 +-
 arch/x86/kvm/vmx.c              | 11 ++++++-----
 arch/x86/kvm/x86.c              |  9 ++++++++-
 8 files changed, 92 insertions(+), 20 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v4 1/4 KVM: Remove SMAP bit from CR4_RESERVED_BITS.

This patch removes SMAP bit from CR4_RESERVED_BITS.

---
 arch/x86/include/asm/kvm_host.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -1
index fdf83af..4eeb049 100644
@@ -60,7 +60,7 @@
 			  | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE     \
 			  | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
 			  | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
-			  | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
+			  | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE | X86_CR4_SMAP))
 
 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
 
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH v4 2/4 KVM: Add SMAP support when setting CR4

In reply to Feng Wu

This patch adds SMAP handling logic when setting CR4 for guests

Thanks a lot to Paolo Bonzini for his suggestion to use the branchless way to detect SMAP violation.

---
 arch/x86/kvm/cpuid.h       |  8 ++++++++
 arch/x86/kvm/mmu.c         | 34 +++++++++++++++++++++++++++++++---
 arch/x86/kvm/mmu.h         | 44 ++++++++++++++++++++++++++++++++++++--------
 arch/x86/kvm/paging_tmpl.h |  2 +-
 arch/x86/kvm/x86.c         |  9 ++++++++-
 5 files changed, 84 insertions(+), 13 deletions(-)
arch/x86/kvm/cpuid.h +8 -0
index a2a1bb7..eeecbed 100644
@@ -48,6 +48,14 @@ static inline bool guest_cpuid_has_smep(struct kvm_vcpu *vcpu)
 	return best && (best->ebx & bit(X86_FEATURE_SMEP));
 }
 
+static inline bool guest_cpuid_has_smap(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *best;
+
+	best = kvm_find_cpuid_entry(vcpu, 7, 0);
+	return best && (best->ebx & bit(X86_FEATURE_SMAP));
+}
+
 static inline bool guest_cpuid_has_fsgsbase(struct kvm_vcpu *vcpu)
 {
 	struct kvm_cpuid_entry2 *best;
arch/x86/kvm/mmu.c +31 -3
index 9b53135..a183783 100644
@@ -3601,20 +3601,27 @@ static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
 	}
 }
 
-static void update_permission_bitmask(struct kvm_vcpu *vcpu,
+void update_permission_bitmask(struct kvm_vcpu *vcpu,
 		struct kvm_mmu *mmu, bool ept)
 {
 	unsigned bit, byte, pfec;
 	u8 map;
-	bool fault, x, w, u, wf, uf, ff, smep;
+	bool fault, x, w, u, wf, uf, ff, smapf, cr4_smap, smep, smap = 0;
 
 	smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
+	cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
 	for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
 		pfec = byte << 1;
 		map = 0;
 		wf = pfec & PFERR_WRITE_MASK;
 		uf = pfec & PFERR_USER_MASK;
 		ff = pfec & PFERR_FETCH_MASK;
+		/*
+		 * PFERR_RSVD_MASK bit is set in PFEC if the access is not
+		 * subject to SMAP restrictions, and cleared otherwise. The
+		 * bit is only meaningful if the SMAP bit is set in CR4.
+		 */
+		smapf = !(pfec & PFERR_RSVD_MASK);
 		for (bit = 0; bit < 8; ++bit) {
 			x = bit & ACC_EXEC_MASK;
 			w = bit & ACC_WRITE_MASK;
@@ -3627,11 +3634,32 @@ static void update_permission_bitmask(struct kvm_vcpu *vcpu,
 				w |= !is_write_protection(vcpu) && !uf;
 				/* Disallow supervisor fetches of user code if cr4.smep */
 				x &= !(smep && u && !uf);
+
+				/*
+				 * SMAP:kernel-mode data accesses from user-mode
+				 * mappings should fault. A fault is considered
+				 * as a SMAP violation if all of the following
+				 * conditions are ture:
+				 *   - X86_CR4_SMAP is set in CR4
+				 *   - An user page is accessed
+				 *   - Page fault in kernel mode
+				 *   - if CPL = 3 or X86_EFLAGS_AC is clear
+				 *
+				 *   Here, we cover the first three conditions.
+				 *   The fourth is computed dynamically in
+				 *   permission_fault() and is in smapf.
+				 *
+				 *   Also, SMAP does not affect instruction
+				 *   fetches, add the !ff check here to make it
+				 *   clearer.
+				 */
+				smap = cr4_smap && u && !uf && !ff;
 			} else
 				/* Not really needed: no U/S accesses on ept  */
 				u = 1;
 
-			fault = (ff && !x) || (uf && !u) || (wf && !w);
+			fault = (ff && !x) || (uf && !u) || (wf && !w) ||
+				(smapf && smap);
 			map |= fault << bit;
 		}
 		mmu->permissions[byte] = map;
arch/x86/kvm/mmu.h +36 -8

63 lines not on this page · raw message

arch/x86/kvm/paging_tmpl.h +1 -1
index cba218a..4107765 100644
@@ -353,7 +353,7 @@ retry_walk:
 		walker->ptes[walker->level - 1] = pte;
 	} while (!is_last_gpte(mmu, walker->level, pte));
 
-	if (unlikely(permission_fault(mmu, pte_access, access))) {
+	if (unlikely(permission_fault(vcpu, mmu, pte_access, access))) {
 		errcode |= PFERR_PRESENT_MASK;
 		goto error;
 	}
arch/x86/kvm/x86.c +8 -1
index 2b85784..5869c6d 100644
@@ -646,6 +646,9 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 	if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
 		return 1;
 
+	if (!guest_cpuid_has_smap(vcpu) && (cr4 & X86_CR4_SMAP))
+		return 1;
+
 	if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_FSGSBASE))
 		return 1;
 
@@ -674,6 +677,9 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 	    (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
 		kvm_mmu_reset_context(vcpu);
 
+	if ((cr4 ^ old_cr4) & X86_CR4_SMAP)
+		update_permission_bitmask(vcpu, vcpu->arch.walk_mmu, false);
+
 	if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
 		kvm_update_cpuid(vcpu);
 
@@ -4108,7 +4114,8 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
 		| (write ? PFERR_WRITE_MASK : 0);
 
 	if (vcpu_match_mmio_gva(vcpu, gva)
-	    && !permission_fault(vcpu->arch.walk_mmu, vcpu->arch.access, access)) {
+	    && !permission_fault(vcpu, vcpu->arch.walk_mmu,
+				 vcpu->arch.access, access)) {
 		*gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
 					(gva & (PAGE_SIZE - 1));
 		trace_vcpu_match_mmio(gva, *gpa, write, false);
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH v4 2/4] KVM: Add SMAP support when setting CR4

Quoted 9 lines · Feng Wu

This patch adds SMAP handling logic when setting CR4 for guests

Thanks a lot to Paolo Bonzini for his suggestion to use the branchless way to detect SMAP violation.

arch/x86/kvm/mmu.h +0 -0
@@ -110,10 +118,30 @@ static inline bool is_write_protection(struct kvm_vcpu *vcpu)
  * Will a fault with a given page-fault error code (pfec) cause a permission

arch/x86/kvm/mmu.h static inline bool is_write_protection(struct kvm_vcpu *vcpu) · line 125

Quoted 10 lines
arch/x86/kvm/mmu.h +4 -3
  * fault with the given access (in ACC_* format)?
  */
-static inline bool permission_fault(struct kvm_mmu *mmu, unsigned pte_access,
-				    unsigned pfec)
+static inline bool permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
+				    unsigned pte_access, unsigned pfec)
 {
-	return (mmu->permissions[pfec >> 1] >> pte_access) & 1;
+	int cpl = kvm_x86_ops->get_cpl(vcpu);
+	unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);

Some branches added here (can't think of anything useful to avoid them).

maintainer
#4 lore raw

Re: [PATCH v4 2/4] KVM: Add SMAP support when setting CR4

Il 10/04/2014 16:12, Marcelo Tosatti ha scritto:

Quoted 8 lines · Feng Wu
Quoted 7 lines

This patch adds SMAP handling logic when setting CR4 for guests

Thanks a lot to Paolo Bonzini for his suggestion to use the branchless way to detect SMAP violation.

Quoted 14 lines

arch/x86/kvm/mmu.h static inline bool is_write_protection(struct kvm_vcpu *vcpu) · line 125

Quoted 12 lines
arch/x86/kvm/mmu.h +4 -3
@@ -110,10 +118,30 @@ static inline bool is_write_protection(struct kvm_vcpu *vcpu)
  * Will a fault with a given page-fault error code (pfec) cause a permission
  * fault with the given access (in ACC_* format)?
  */
-static inline bool permission_fault(struct kvm_mmu *mmu, unsigned pte_access,
-				    unsigned pfec)
+static inline bool permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
+				    unsigned pte_access, unsigned pfec)
 {
-	return (mmu->permissions[pfec >> 1] >> pte_access) & 1;
+	int cpl = kvm_x86_ops->get_cpl(vcpu);
+	unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);

Some branches added here (can't think of anything useful to avoid them).

Yeah, but most of them should be well predicted.

Paolo

author
#5 lore raw

PATCH v4 3/4 KVM: Disable SMAP for guests in EPT realmode and EPT unpaging mode

In reply to Feng Wu

SMAP is disabled if CPU is in non-paging mode in hardware. However KVM always uses paging mode to emulate guest non-paging mode with TDP. To emulate this behavior, SMAP needs to be manually disabled when guest switches to non-paging mode.

---
 arch/x86/kvm/vmx.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx.c +6 -5
index 3927528..e58cb5f 100644
@@ -3452,13 +3452,14 @@ static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 			hw_cr4 &= ~X86_CR4_PAE;
 			hw_cr4 |= X86_CR4_PSE;
 			/*
-			 * SMEP is disabled if CPU is in non-paging mode in
-			 * hardware. However KVM always uses paging mode to
+			 * SMEP/SMAP is disabled if CPU is in non-paging mode
+			 * in hardware. However KVM always uses paging mode to
 			 * emulate guest non-paging mode with TDP.
-			 * To emulate this behavior, SMEP needs to be manually
-			 * disabled when guest switches to non-paging mode.
+			 * To emulate this behavior, SMEP/SMAP needs to be
+			 * manually disabled when guest switches to non-paging
+			 * mode.
 			 */
-			hw_cr4 &= ~X86_CR4_SMEP;
+			hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
 		} else if (!(cr4 & X86_CR4_PAE)) {
 			hw_cr4 &= ~X86_CR4_PAE;
 		}
Signature · 1 line
1.8.3.1
author
#6 lore raw

PATCH v4 4/4 KVM: expose SMAP feature to guest

In reply to Feng Wu

This patch exposes SMAP feature to guest

---
 arch/x86/kvm/cpuid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
index c697625..deb5f9b 100644
@@ -303,7 +303,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 	/* cpuid 7.0.ebx */
 	const u32 kvm_supported_word9_x86_features =
 		F(FSGSBASE) | F(BMI1) | F(HLE) | F(AVX2) | F(SMEP) |
-		F(BMI2) | F(ERMS) | f_invpcid | F(RTM);
+		F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | F(SMAP);
 
 	/* all calls to cpuid_count() should be made on the same cpu */
 	get_cpu();
Signature · 1 line
1.8.3.1
maintainer
#7 lore raw
In reply to Feng Wu

Il 01/04/2014 11:46, Feng Wu ha scritto:

Quoted 6 lines

Feng Wu (4):
KVM: Remove SMAP bit from CR4_RESERVED_BITS.
KVM: Add SMAP support when setting CR4
KVM: Disable SMAP for guests in EPT realmode and EPT unpaging mode
KVM: expose SMAP feature to guest

Hi,

I prepared some testcases. You can find them in branch "smap" of git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git

To compile them for 32-bits:

	git clean -xdf
	./configure --arch=i386
	make
	./x86-run x86/smap.flat -cpu host

For 64-bits:

	git clean -xdf
	./configure
	make
	./x86-run x86/smap.flat -cpu host

I tried them with QEMU and they all pass. The output should be something like this:

enabling apic
paging enabled
cr0 = 80010011
cr3 = 7fff000
cr4 = 10
testing without INVLPG
PASS: write to supervisor page
PASS: read from user page with AC=1
PASS: read from user page with AC=0
PASS: write to user page with AC=1
PASS: read from user page with AC=0
PASS: write to user stack with AC=1
PASS: write to user stack with AC=0
PASS: executing on user page with AC=0
testing with INVLPG
PASS: write to supervisor page
PASS: read from user page with AC=1
PASS: read from user page with AC=0
PASS: write to user page with AC=1
PASS: read from user page with AC=0
PASS: write to user stack with AC=1
PASS: write to user stack with AC=0
PASS: executing on user page with AC=0

SUMMARY: 16 tests, 0 failures

Please test them (both 32- and 64-bits) with both ept=1 and ept=0. If the tests pass, the series is okay.

The only part that is not covered is the implicit kernel accesses at CPL=3, which QEMU doesn't implement that (I fixed it, but didn't have time to think about tests). Since I'm going on vacation next week, I wanted to throw this out today. I'll post the test patches when I'm back.

Paolo

author
#8 lore raw
Quoted 61 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Paolo Bonzini
Sent: Friday, April 04, 2014 12:46 AM
To: Wu, Feng; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v4 0/4] KVM: enable Intel SMAP for KVM

Il 01/04/2014 11:46, Feng Wu ha scritto:

Quoted 6 lines

Feng Wu (4):
KVM: Remove SMAP bit from CR4_RESERVED_BITS.
KVM: Add SMAP support when setting CR4
KVM: Disable SMAP for guests in EPT realmode and EPT unpaging mode
KVM: expose SMAP feature to guest

Hi,

I prepared some testcases. You can find them in branch "smap" of
git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git

To compile them for 32-bits:

	git clean -xdf
	./configure --arch=i386
	make
	./x86-run x86/smap.flat -cpu host

For 64-bits:

	git clean -xdf
	./configure
	make
	./x86-run x86/smap.flat -cpu host

I tried them with QEMU and they all pass. The output should be something like this:

enabling apic
paging enabled
cr0 = 80010011
cr3 = 7fff000
cr4 = 10
testing without INVLPG
PASS: write to supervisor page
PASS: read from user page with AC=1
PASS: read from user page with AC=0
PASS: write to user page with AC=1
PASS: read from user page with AC=0
PASS: write to user stack with AC=1
PASS: write to user stack with AC=0
PASS: executing on user page with AC=0
testing with INVLPG
PASS: write to supervisor page
PASS: read from user page with AC=1
PASS: read from user page with AC=0
PASS: write to user page with AC=1
PASS: read from user page with AC=0
PASS: write to user stack with AC=1
PASS: write to user stack with AC=0
PASS: executing on user page with AC=0

Quoted 4 lines

SUMMARY: 16 tests, 0 failures

Please test them (both 32- and 64-bits) with both ept=1 and ept=0. If the tests pass, the series is okay.

Thank you for providing these test cases. I tested it in related hardware (both 32- and 64-bits) with both ept=1 and ept=0, they all pass.

I also did some similar testing before posting the patch set. Since SMAP has been already supported in Linux kernel, in which, stac() and clac() are added in functions like copy_from_user(), copy_to_user(), etc.. From my previous test, Linux guest can run well on top of KVM with SMAP enabled. I think this covers the AC bit logic for testing. I also tested whether it can induce an SMAP violation when accessing user pages in kernel mode with AC bit cleared, I successfully got the SMAP violation fault in guest in that case.

Besides that, I got some patches for SMAP testing in native Linux, which are attached, I applied them to the guest kernel and everything works well.

Quoted 11 lines

The only part that is not covered is the implicit kernel accesses at CPL=3, which QEMU doesn't implement that (I fixed it, but didn't have time to think about tests). Since I'm going on vacation next week, I wanted to throw this out today. I'll post the test patches when I'm back.

Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

From 23da102c05ceda22d09499f3a16134d5cdddddb3 Mon Sep 17 00:00:00 2001
From: Ricardo Neri <[email protected]>
Date: Thu, 8 Aug 2013 18:08:39 -0700
Subject: [PATCH 1/8] x86, smap: add debug code for local interrupts

When interrupts are disabled, we are in kernel mode. Thus, SMAP should prevent accessing user-space memory. We make sure that SMAP feature enforces this by checking if EFLAGS.AC is clear.

---
 arch/x86/Kconfig                |  9 +++++++++
 arch/x86/include/asm/irqflags.h | 12 ++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/Kconfig +9 -0
index 15b5cef..59f34bf 100644
@@ -1546,6 +1546,15 @@ config X86_SMAP
 
 	  If unsure, say Y.
 
+config X86_SMAP_DEBUG_INTERRUPTS
+	def_bool n
+	prompt "Debug interrupts for SMAP" if X86_SMAP
+	---help---
+	  Enable code to debug problems related to the SMAP feature: when
+	  interrupts are disabled (i.e., only in kernel mode), EFLAGS.AC
+	  should be clear. Thus, access to user-space memory while in kernel
+	  mode is prevented by SMAP.
+
 config EFI
 	bool "EFI runtime service support"
 	depends on ACPI
arch/x86/include/asm/irqflags.h +12 -0
index bba3cf8..53f8c58 100644
@@ -2,6 +2,15 @@
 #define _X86_IRQFLAGS_H_
 
 #include <asm/processor-flags.h>
+#ifndef __ASSEMBLY__
+#include <asm/bug.h>
+
+#ifdef X86_SMAP_DEBUG_INTERRUPTS
+# define ENFORCE_CLEAR_EFLAGS_AC(x) BUG_ON((x) & X86_EFLAGS_AC)
+#else
+# define ENFORCE_CLEAR_EFLAGS_AC(x)
+#endif
+#endif
 
 #ifndef __ASSEMBLY__
 /*
@@ -107,6 +116,9 @@ static inline notrace unsigned long arch_local_irq_save(void)
 {
 	unsigned long flags = arch_local_save_flags();
 	arch_local_irq_disable();
+
+	ENFORCE_CLEAR_EFLAGS_AC(flags);
+
 	return flags;
 }
 #else
Signature · 1 line
1.8.3.1

From 5d3b919cfd9b6f180e025a7027c27233b4d07be9 Mon Sep 17 00:00:00 2001
From: Ricardo Neri <[email protected]>
Date: Wed, 21 Aug 2013 15:52:56 -0700
Subject: [PATCH 2/8] x86, smap: add debug code for STAC opcode

When SMAP is temporary disabled to legally access user-space address space in kernel mode, we expect EFLAGS.AC=0. This code verifies that such condition is satisifed. Otherwise, an invalid opcode exception is generated.

---
 arch/x86/Kconfig            |  8 +++++
 arch/x86/include/asm/smap.h | 85 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 86 insertions(+), 7 deletions(-)
arch/x86/Kconfig +8 -0
index 59f34bf..34bc8ba 100644
@@ -1555,6 +1555,14 @@ config X86_SMAP_DEBUG_INTERRUPTS
 	  should be clear. Thus, access to user-space memory while in kernel
 	  mode is prevented by SMAP.
 
+config X86_SMAP_DEBUG_STAC
+	def_bool n
+	prompt "Debug STAC for SMAP" if X86_SMAP
+	---help---
+	  Enable code to debug problems related to the SMAP feature. When STAC
+	  is called, EFLAGS.AC should be clear. This option verifies that this
+	  condition holds; otherwise, it generates an invalid opcode exception.
+
 config EFI
 	bool "EFI runtime service support"
 	depends on ACPI
arch/x86/include/asm/smap.h +78 -7

131 lines not on this page · raw message

Signature · 1 line
1.8.3.1

From d75cc118d0fdb75a4649a951e16e341ca87af199 Mon Sep 17 00:00:00 2001
From: Ricardo Neri <[email protected]>
Date: Tue, 10 Sep 2013 17:41:03 -0700
Subject: [PATCH 3/8] x86, smap: add debug code for CLAC opcode

In most cases, when SMAP enforced by calling CLAC, we expect it to be previosly disabled (i.e., EFLAGS.AC=1) by a call to STAC. This debug code verifies that this condition is met.

There are cases, however, in which CLAC is called without previously having EFLAGS.AC=0. This is the case of error paths. For this reason, debug functions are defined separately of the actual CLAC macros and functions. This is to be able to selectively put debug points only where required.

The CLAC debug code is implemented as an ALTERNATIVE, this is to prevent that CPUs that do not support SMAP feature this code in case it is enabled at build time.

---
 arch/x86/Kconfig            |  9 ++++++
 arch/x86/include/asm/smap.h | 77 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
arch/x86/Kconfig +9 -0
index 34bc8ba..1ece076 100644
@@ -1563,6 +1563,15 @@ config X86_SMAP_DEBUG_STAC
 	  is called, EFLAGS.AC should be clear. This option verifies that this
 	  condition holds; otherwise, it generates an invalid opcode exception.
 
+config X86_SMAP_DEBUG_CLAC
+	def_bool n
+	prompt "Debug CLAC for SMAP" if X86_SMAP
+	---help---
+	  Enable code to debug problems related to the SMAP feature. When CLAC
+	  is called, EFLAGS.AC should be set in the majority of the cases. This
+	  option verifies that this condition holds; otherwise, it generates an
+	  invalid opcode exception.
+
 config EFI
 	bool "EFI runtime service support"
 	depends on ACPI
arch/x86/include/asm/smap.h +77 -0

89 lines not on this page · raw message

Signature · 1 line
1.8.3.1

From 3478516096c793e64bb4fb6635f8a33bd9774832 Mon Sep 17 00:00:00 2001
From: Ricardo Neri <[email protected]>
Date: Tue, 10 Sep 2013 17:46:41 -0700
Subject: [PATCH 4/8] x86, smap: add debug points for CLAC calls

Add debug points for CLAC only where we know that a previous STAC must have been issued; in other words, error paths are not considered.

---
 arch/x86/ia32/ia32entry.S           |  2 ++
 arch/x86/include/asm/fpu-internal.h |  3 ++-
 arch/x86/include/asm/futex.h        |  9 ++++++---
 arch/x86/include/asm/uaccess.h      | 12 ++++++++----
 arch/x86/include/asm/xsave.h        |  6 ++++--
 arch/x86/kernel/entry_32.S          |  1 +
 arch/x86/lib/copy_user_64.S         |  3 +++
 arch/x86/lib/copy_user_nocache_64.S |  1 +
 arch/x86/lib/getuser.S              |  5 +++++
 arch/x86/lib/putuser.S              |  3 ++-
 arch/x86/lib/usercopy_32.c          |  8 +++++++-
 11 files changed, 41 insertions(+), 12 deletions(-)
arch/x86/ia32/ia32entry.S +2 -0
index 474dc1b..6741122 100644
@@ -150,6 +150,7 @@ ENTRY(ia32_sysenter_target)
 	ASM_STAC
 1:	movl	(%rbp),%ebp
 	_ASM_EXTABLE(1b,ia32_badarg)
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	orl     $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP-ARGOFFSET)
 	testl   $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
@@ -307,6 +308,7 @@ ENTRY(ia32_cstar_target)
 	ASM_STAC
 1:	movl	(%r8),%r9d
 	_ASM_EXTABLE(1b,ia32_badarg)
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	orl     $TS_COMPAT,TI_status+THREAD_INFO(%rsp,RIP-ARGOFFSET)
 	testl   $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
arch/x86/include/asm/fpu-internal.h +2 -1
index e25cc33..c226c67 100644
@@ -128,7 +128,8 @@ static inline void sanitize_i387_state(struct task_struct *tsk)
 	int err;							\
 	asm volatile(ASM_STAC "\n"					\
 		     "1:" #insn "\n\t"					\
-		     "2: " ASM_CLAC "\n"				\
+		     "2: " ASM_CLAC_DEBUG "\n"				\
+		     "   " ASM_CLAC "\n"				\
 		     ".section .fixup,\"ax\"\n"				\
 		     "3:  movl $-1,%[err]\n"				\
 		     "    jmp  2b\n"					\
arch/x86/include/asm/futex.h +6 -3
index be27ba1..da519e3 100644
@@ -14,7 +14,8 @@
 #define __futex_atomic_op1(insn, ret, oldval, uaddr, oparg)	\
 	asm volatile("\t" ASM_STAC "\n"				\
 		     "1:\t" insn "\n"				\
-		     "2:\t" ASM_CLAC "\n"			\
+		     "2:\t" ASM_CLAC_DEBUG "\n"			\
+		     "\t" ASM_CLAC "\n"				\
 		     "\t.section .fixup,\"ax\"\n"		\
 		     "3:\tmov\t%3, %1\n"			\
 		     "\tjmp\t2b\n"				\
@@ -30,7 +31,8 @@
 		     "\t" insn "\n"				\
 		     "2:\t" LOCK_PREFIX "cmpxchgl %3, %2\n"	\
 		     "\tjnz\t1b\n"				\
-		     "3:\t" ASM_CLAC "\n"			\
+		     "3:\t" ASM_CLAC_DEBUG "\n"			\
+		     "\t" ASM_CLAC "\n"				\
 		     "\t.section .fixup,\"ax\"\n"		\
 		     "4:\tmov\t%5, %1\n"			\
 		     "\tjmp\t3b\n"				\
@@ -117,7 +119,8 @@ static inline int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
 
 	asm volatile("\t" ASM_STAC "\n"
 		     "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n"
-		     "2:\t" ASM_CLAC "\n"
+		     "2:\t" ASM_CLAC_DEBUG "\n"
+		     "\t" ASM_CLAC "\n"
 		     "\t.section .fixup, \"ax\"\n"
 		     "3:\tmov     %3, %0\n"
 		     "\tjmp     2b\n"
arch/x86/include/asm/uaccess.h +8 -4
index 5ee2687..f6c0863 100644
@@ -183,7 +183,8 @@ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
 	asm volatile(ASM_STAC "\n"					\
 		     "1:	movl %%eax,0(%2)\n"			\
 		     "2:	movl %%edx,4(%2)\n"			\
-		     "3: " ASM_CLAC "\n"				\
+		     "3: " ASM_CLAC_DEBUG "\n"				\
+		     "   " ASM_CLAC "\n"				\
 		     ".section .fixup,\"ax\"\n"				\
 		     "4:	movl %3,%0\n"				\
 		     "	jmp 3b\n"					\
@@ -197,7 +198,8 @@ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
 	asm volatile(ASM_STAC "\n"					\
 		     "1:	movl %%eax,0(%1)\n"			\
 		     "2:	movl %%edx,4(%1)\n"			\
-		     "3: " ASM_CLAC "\n"				\
+		     "3: " ASM_CLAC_DEBUG "\n"				\
+		     "   " ASM_CLAC "\n"				\
 		     _ASM_EXTABLE_EX(1b, 2b)				\
 		     _ASM_EXTABLE_EX(2b, 3b)				\
 		     : : "A" (x), "r" (addr))
@@ -346,7 +348,8 @@ do {									\
 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
 	asm volatile(ASM_STAC "\n"					\
 		     "1:	mov"itype" %2,%"rtype"1\n"		\
-		     "2: " ASM_CLAC "\n"				\
+		     "2: " ASM_CLAC_DEBUG "\n"				\
+		     "   " ASM_CLAC "\n"				\
 		     ".section .fixup,\"ax\"\n"				\
 		     "3:	mov %3,%0\n"				\
 		     "	xor"itype" %"rtype"1,%"rtype"1\n"		\
@@ -411,7 +414,8 @@ struct __large_struct { unsigned long buf[100]; };
 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
 	asm volatile(ASM_STAC "\n"					\
 		     "1:	mov"itype" %"rtype"1,%2\n"		\
-		     "2: " ASM_CLAC "\n"				\
+		     "2: " ASM_CLAC_DEBUG "\n"				\
+		     "   " ASM_CLAC "\n"				\
 		     ".section .fixup,\"ax\"\n"				\
 		     "3:	mov %3,%0\n"				\
 		     "	jmp 2b\n"					\
arch/x86/include/asm/xsave.h +4 -2
index 0415cda..c738b72 100644
@@ -72,7 +72,8 @@ static inline int xsave_user(struct xsave_struct __user *buf)
 
 	__asm__ __volatile__(ASM_STAC "\n"
 			     "1: .byte " REX_PREFIX "0x0f,0xae,0x27\n"
-			     "2: " ASM_CLAC "\n"
+			     "2: " ASM_CLAC_DEBUG "\n"
+			     "   " ASM_CLAC "\n"
 			     ".section .fixup,\"ax\"\n"
 			     "3:  movl $-1,%[err]\n"
 			     "    jmp  2b\n"
@@ -93,7 +94,8 @@ static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
 
 	__asm__ __volatile__(ASM_STAC "\n"
 			     "1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n"
-			     "2: " ASM_CLAC "\n"
+			     "2: " ASM_CLAC_DEBUG "\n"
+			     "   " ASM_CLAC "\n"
 			     ".section .fixup,\"ax\"\n"
 			     "3:  movl $-1,%[err]\n"
 			     "    jmp  2b\n"
arch/x86/kernel/entry_32.S +1 -0
index 8f3e2de..b1bf048 100644
@@ -424,6 +424,7 @@ sysenter_past_esp:
 	jae syscall_fault
 	ASM_STAC
 1:	movl (%ebp),%ebp
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	movl %ebp,PT_EBP(%esp)
 	_ASM_EXTABLE(1b,syscall_fault)
arch/x86/lib/copy_user_64.S +3 -0
index a30ca15..7207fad 100644
@@ -179,6 +179,7 @@ ENTRY(copy_user_generic_unrolled)
 	decl %ecx
 	jnz 21b
 23:	xor %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 
@@ -250,6 +251,7 @@ ENTRY(copy_user_generic_string)
 3:	rep
 	movsb
 4:	xorl %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 
@@ -285,6 +287,7 @@ ENTRY(copy_user_enhanced_fast_string)
 1:	rep
 	movsb
 2:	xorl %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 
arch/x86/lib/copy_user_nocache_64.S +1 -0
index 6a4f43c..09980ea 100644
@@ -97,6 +97,7 @@ ENTRY(__copy_user_nocache)
 	decl %ecx
 	jnz 21b
 23:	xorl %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	sfence
 	ret
arch/x86/lib/getuser.S +5 -0
index a451235..fe75870 100644
@@ -43,6 +43,7 @@ ENTRY(__get_user_1)
 	ASM_STAC
 1:	movzbl (%_ASM_AX),%edx
 	xor %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 	CFI_ENDPROC
@@ -58,6 +59,7 @@ ENTRY(__get_user_2)
 	ASM_STAC
 2:	movzwl -1(%_ASM_AX),%edx
 	xor %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 	CFI_ENDPROC
@@ -73,6 +75,7 @@ ENTRY(__get_user_4)
 	ASM_STAC
 3:	movl -3(%_ASM_AX),%edx
 	xor %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 	CFI_ENDPROC
@@ -89,6 +92,7 @@ ENTRY(__get_user_8)
 	ASM_STAC
 4:	movq -7(%_ASM_AX),%rdx
 	xor %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 #else
@@ -101,6 +105,7 @@ ENTRY(__get_user_8)
 4:	movl -7(%_ASM_AX),%edx
 5:	movl -3(%_ASM_AX),%ecx
 	xor %eax,%eax
+	ASM_CLAC_DEBUG
 	ASM_CLAC
 	ret
 #endif
arch/x86/lib/putuser.S +2 -1
index fc6ba17..b59fb60 100644
@@ -32,7 +32,8 @@
 
 #define ENTER	CFI_STARTPROC ; \
 		GET_THREAD_INFO(%_ASM_BX)
-#define EXIT	ASM_CLAC ;	\
+#define EXIT	ASM_CLAC_DEBUG ;\
+		ASM_CLAC ;	\
 		ret ;		\
 		CFI_ENDPROC
 
arch/x86/lib/usercopy_32.c +7 -1
index f0312d7..70a1f0a 100644
@@ -46,7 +46,8 @@ do {									\
 		"0:	rep; stosl\n"					\
 		"	movl %2,%0\n"					\
 		"1:	rep; stosb\n"					\
-		"2: " ASM_CLAC "\n"					\
+		"2: " ASM_CLAC_DEBUG "\n"				\
+		"   " ASM_CLAC "\n"					\
 		".section .fixup,\"ax\"\n"				\
 		"3:	lea 0(%2,%0,4),%0\n"				\
 		"	jmp 2b\n"					\
@@ -575,6 +576,7 @@ unsigned long __copy_to_user_ll(void __user *to, const void *from,
 		__copy_user(to, from, n);
 	else
 		n = __copy_user_intel(to, from, n);
+	clac_debug();
 	clac();
 	return n;
 }
@@ -588,6 +590,7 @@ unsigned long __copy_from_user_ll(void *to, const void __user *from,
 		__copy_user_zeroing(to, from, n);
 	else
 		n = __copy_user_zeroing_intel(to, from, n);
+	clac_debug();
 	clac();
 	return n;
 }
@@ -602,6 +605,7 @@ unsigned long __copy_from_user_ll_nozero(void *to, const void __user *from,
 	else
 		n = __copy_user_intel((void __user *)to,
 				      (const void *)from, n);
+	clac_debug();
 	clac();
 	return n;
 }
@@ -619,6 +623,7 @@ unsigned long __copy_from_user_ll_nocache(void *to, const void __user *from,
 #else
 	__copy_user_zeroing(to, from, n);
 #endif
+	clac_debug();
 	clac();
 	return n;
 }
@@ -636,6 +641,7 @@ unsigned long __copy_from_user_ll_nocache_nozero(void *to, const void __user *fr
 #else
 	__copy_user(to, from, n);
 #endif
+	clac_debug();
 	clac();
 	return n;
 }
Signature · 1 line
1.8.3.1

From 9dcac6c7aebae26deea336fc507bfdd7fea3fadf Mon Sep 17 00:00:00 2001
From: Ricardo Neri <[email protected]>
Date: Tue, 17 Sep 2013 16:57:11 -0700
Subject: [PATCH 7/8] x86, smap: add more debug points for CLAC calls

Add debug points for CLAC in the checksum functions.

---
 arch/x86/include/asm/checksum_32.h | 2 ++
 arch/x86/lib/csum-wrappers_64.c    | 2 ++
 2 files changed, 4 insertions(+)
arch/x86/include/asm/checksum_32.h +2 -0
index f50de69..f3df574 100644
@@ -55,6 +55,7 @@ static inline __wsum csum_partial_copy_from_user(const void __user *src,
 	stac();
 	ret = csum_partial_copy_generic((__force void *)src, dst,
 					len, sum, err_ptr, NULL);
+	clac_debug();
 	clac();
 
 	return ret;
@@ -189,6 +190,7 @@ static inline __wsum csum_and_copy_to_user(const void *src,
 		stac();
 		ret = csum_partial_copy_generic(src, (__force void *)dst,
 						len, sum, NULL, err_ptr);
+		clac_debug();
 		clac();
 		return ret;
 	}
arch/x86/lib/csum-wrappers_64.c +2 -0
index 7609e0e..c598185 100644
@@ -56,6 +56,7 @@ csum_partial_copy_from_user(const void __user *src, void *dst,
 	stac();
 	isum = csum_partial_copy_generic((__force const void *)src,
 				dst, len, isum, errp, NULL);
+	clac_debug();
 	clac();
 	if (unlikely(*errp))
 		goto out_err;
@@ -113,6 +114,7 @@ csum_partial_copy_to_user(const void *src, void __user *dst,
 	stac();
 	ret = csum_partial_copy_generic(src, (void __force *)dst,
 					len, isum, NULL, errp);
+	clac_debug();
 	clac();
 	return ret;
 }
Signature · 1 line
1.8.3.1
maintainer Reviewed-by
#9 lore raw

Il 04/04/2014 04:22, Wu, Feng ha scritto:

Quoted 10 lines

Thank you for providing these test cases. I tested it in related hardware (both 32- and 64-bits) with both ept=1 and ept=0, they all pass.

I also did some similar testing before posting the patch set. Since SMAP has been already supported in Linux kernel, in which, stac() and clac() are added in functions like copy_from_user(), copy_to_user(), etc.. From my previous test, Linux guest can run well on top of KVM with SMAP enabled. I think this covers the AC bit logic for testing. I also tested whether it can induce an SMAP violation when accessing user pages in kernel mode with AC bit cleared, I successfully got the SMAP violation fault in guest in that case.

Thanks, that is useful to know. Knowing that you tried the failure path is good (next time point it out when submitting the patch).

I made unit tests because I'm not sure how much the new code is stimulated in normal runs of Linux with ept=1. After the EPT tables are built on the first access, the processor will take care of doing SMAP checks. With ept=0, more page faults should happen on the first access to a page. Still, it seemed safer to have unit tests and have them try both without and with invlpg.

Even though the tests do not cover the CPL=3/implicit access case, the logic to compute PFERR_RSVD_MASK dynamically is already covered by AC=1.
So I'm quite happy with the coverage. Series is

author
#10 lore raw
Quoted 21 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Paolo Bonzini
Sent: Friday, April 04, 2014 3:28 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v4 0/4] KVM: enable Intel SMAP for KVM

Il 04/04/2014 04:22, Wu, Feng ha scritto:

Quoted 10 lines

Thank you for providing these test cases. I tested it in related hardware (both 32- and 64-bits) with both ept=1 and ept=0, they all pass.

I also did some similar testing before posting the patch set. Since SMAP has been already supported in Linux kernel, in which, stac() and clac() are added in functions like copy_from_user(), copy_to_user(), etc.. From my previous test, Linux guest can run well on top of KVM with SMAP enabled. I think this covers the AC bit logic for testing. I also tested whether it can induce an SMAP violation when accessing user pages in kernel mode with AC bit cleared, I successfully got the SMAP violation fault in guest in that case.

Quoted 2 lines

Thanks, that is useful to know. Knowing that you tried the failure path is good (next time point it out when submitting the patch).

Sure, will do this next time!:)

Quoted 7 lines

I made unit tests because I'm not sure how much the new code is stimulated in normal runs of Linux with ept=1. After the EPT tables are built on the first access, the processor will take care of doing SMAP checks. With ept=0, more page faults should happen on the first access to a page. Still, it seemed safer to have unit tests and have them try both without and with invlpg.

Yes, Having the unit test code is really helpful for the code stability. Really appreciate the effort you put on these test cases!

Quoted 6 lines

Even though the tests do not cover the CPL=3/implicit access case, the logic to compute PFERR_RSVD_MASK dynamically is already covered by AC=1.
So I'm quite happy with the coverage. Series is

Thanks very much for your review on this.
BTW: Since 3.15 merge window is still open, I am wondering whether there is any possibility to make SMAP into 3.15 with another pull request.

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Thanks,
Feng

maintainer
#11 lore raw

Il 07/04/2014 21:06, Wu, Feng ha scritto:

Quoted 9 lines
Quoted 5 lines
Quoted 5 lines

Even though the tests do not cover the CPL=3/implicit access case, the logic to compute PFERR_RSVD_MASK dynamically is already covered by AC=1.
So I'm quite happy with the coverage. Series is

Thanks very much for your review on this.
BTW: Since 3.15 merge window is still open, I am wondering whether there is any possibility to make SMAP into 3.15 with another pull request.

This is up to Marcelo who is currently managing the KVM tree.

Paolo

#12 lore raw
Quoted 6 lines · Paolo Bonzini

Il 07/04/2014 21:06, Wu, Feng ha scritto:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Even though the tests do not cover the CPL=3/implicit access case, the logic to compute PFERR_RSVD_MASK dynamically is already covered by AC=1.
So I'm quite happy with the coverage. Series is

Quoted 8 lines
Quoted 4 lines

Thanks very much for your review on this.
BTW: Since 3.15 merge window is still open, I am wondering whether there is any possibility to make SMAP into 3.15 with another pull request.

This is up to Marcelo who is currently managing the KVM tree.

Paolo

The merge window is for patches which have been tested in queue/next for sometime. This patch has received no testing other than the developer testing.

Lack of implicit supervisor mode by instructions such as "Examples of such implicit..." in section 9.3.2, in KVM's emulator, makes the feature incomplete, does it not ?

(would have to signal those accesses as supervisor ones).

maintainer
#13 lore raw

Il 10/04/2014 16:01, Marcelo Tosatti ha scritto:

Quoted 12 lines · Paolo Bonzini
Quoted 11 lines

Il 07/04/2014 21:06, Wu, Feng ha scritto:

Quoted 9 lines
Quoted 5 lines
Quoted 5 lines

Even though the tests do not cover the CPL=3/implicit access case, the logic to compute PFERR_RSVD_MASK dynamically is already covered by AC=1.
So I'm quite happy with the coverage. Series is

Thanks very much for your review on this.
BTW: Since 3.15 merge window is still open, I am wondering whether there is any possibility to make SMAP into 3.15 with another pull request.

Quoted 7 lines
Quoted 3 lines

This is up to Marcelo who is currently managing the KVM tree.

Paolo

The merge window is for patches which have been tested in queue/next for sometime. This patch has received no testing other than the developer testing.

This is not going to change unfortunately since this is not shipping in any real silicon. The only hope could be to use QEMU's SVM and SMAP emulation.

Quoted 3 lines

Lack of implicit supervisor mode by instructions such as "Examples of such implicit..." in section 9.3.2, in KVM's emulator, makes the feature incomplete, does it not ?

Implicit supervisor mode is handled by KVM emulator using read/write_std. These accesses do not set PFERR_USER_MASK, and should work fine with SMAP. Am I misunderstanding?

Paolo

Quoted 1 line

(would have to signal those accesses as supervisor ones).

maintainer
#14 lore raw

Il 11/04/2014 20:16, Paolo Bonzini ha scritto:

Quoted 20 lines

Il 10/04/2014 16:01, Marcelo Tosatti ha scritto:

Quoted 19 lines · Paolo Bonzini
Quoted 17 lines

Il 07/04/2014 21:06, Wu, Feng ha scritto:

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Even though the tests do not cover the CPL=3/implicit access case, the
logic to compute PFERR_RSVD_MASK dynamically is already covered by AC=1.
So I'm quite happy with the coverage. Series is

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Thanks very much for your review on this.
BTW: Since 3.15 merge window is still open, I am wondering whether there is
any possibility to make SMAP into 3.15 with another pull request.

This is up to Marcelo who is currently managing the KVM tree.

Paolo

Quoted 7 lines
Quoted 3 lines

The merge window is for patches which have been tested in queue/next for sometime. This patch has received no testing other than the developer testing.

This is not going to change unfortunately since this is not shipping in any real silicon. The only hope could be to use QEMU's SVM and SMAP emulation.

Note this is not speaking in favor or against merging this (I would be slightly against if I were managing the tree, for the record).

Paolo

Quoted 16 lines
Quoted 3 lines

Lack of implicit supervisor mode by instructions such as "Examples of such implicit..." in section 9.3.2, in KVM's emulator, makes the feature incomplete, does it not ?

Implicit supervisor mode is handled by KVM emulator using read/write_std. These accesses do not set PFERR_USER_MASK, and should work fine with SMAP. Am I misunderstanding?

Paolo

Quoted 1 line

(would have to signal those accesses as supervisor ones).

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw
In reply to Paolo Bonzini
Quoted 17 lines · Paolo Bonzini

Il 10/04/2014 16:01, Marcelo Tosatti ha scritto:

Quoted 16 lines · Paolo Bonzini
Quoted 14 lines

Il 07/04/2014 21:06, Wu, Feng ha scritto:

Quoted 9 lines
Quoted 5 lines
Quoted 5 lines

Even though the tests do not cover the CPL=3/implicit access case, the logic to compute PFERR_RSVD_MASK dynamically is already covered by AC=1.
So I'm quite happy with the coverage. Series is

Thanks very much for your review on this.
BTW: Since 3.15 merge window is still open, I am wondering whether there is any possibility to make SMAP into 3.15 with another pull request.

This is up to Marcelo who is currently managing the KVM tree.

Paolo

Quoted 7 lines
Quoted 3 lines

The merge window is for patches which have been tested in queue/next for sometime. This patch has received no testing other than the developer testing.

This is not going to change unfortunately since this is not shipping in any real silicon. The only hope could be to use QEMU's SVM and SMAP emulation.

Well, let me know if you want an exception to the rule so i should merge this patchset and submit it for 3.15.

Quoted 10 lines
Quoted 4 lines

Lack of implicit supervisor mode by instructions such as "Examples of such implicit..." in section 9.3.2, in KVM's emulator, makes the feature incomplete, does it not ?

Implicit supervisor mode is handled by KVM emulator using read/write_std. These accesses do not set PFERR_USER_MASK, and should work fine with SMAP. Am I misunderstanding?

Paolo

Right.

#16 lore raw

I would like to see this in 3.15.

-hpa

Quoted 46 lines · Marcelo Tosatti
Quoted 4 lines · Paolo Bonzini

Il 10/04/2014 16:01, Marcelo Tosatti ha scritto:

Quoted 3 lines · Paolo Bonzini
Quoted 2 lines

Il 07/04/2014 21:06, Wu, Feng ha scritto:

Quoted 1 line
Quoted 1 line
Quoted 1 line

Even though the tests do not cover the CPL=3/implicit access

case, the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

logic to compute PFERR_RSVD_MASK dynamically is already covered

by AC=1.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

So I'm quite happy with the coverage. Series is

Thanks very much for your review on this.
BTW: Since 3.15 merge window is still open, I am wondering whether

there is

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

any possibility to make SMAP into 3.15 with another pull request.

This is up to Marcelo who is currently managing the KVM tree.

Paolo

Quoted 7 lines
Quoted 3 lines

The merge window is for patches which have been tested in queue/next for sometime. This patch has received no testing other than the developer testing.

This is not going to change unfortunately since this is not shipping in any real silicon. The only hope could be to use QEMU's SVM and SMAP emulation.

Well, let me know if you want an exception to the rule so i should merge this patchset and submit it for 3.15.

Quoted 2 lines
Quoted 2 lines

Lack of implicit supervisor mode by instructions such as "Examples

of

Quoted 1 line
Quoted 1 line

such implicit..." in section 9.3.2, in KVM's emulator, makes the

feature

Quoted 7 lines
Quoted 1 line

incomplete, does it not ?

Implicit supervisor mode is handled by KVM emulator using read/write_std. These accesses do not set PFERR_USER_MASK, and should work fine with SMAP. Am I misunderstanding?

Paolo

Right.

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
Reviewed-by on the v4 series
#17 lore raw
In reply to Feng Wu
Quoted 50 lines · Feng Wu

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

Every access to a linear address is either a supervisor-mode access or a user-mode access. All accesses performed while the current privilege level (CPL) is less than 3 are supervisor-mode accesses. If CPL = 3, accesses are generally user-mode accesses. However, some operations implicitly access system data structures, and the resulting accesses to those data structures are supervisor-mode accesses regardless of CPL. Examples of such implicit supervisor accesses include the following:
accesses to the global descriptor table (GDT) or local descriptor table (LDT) to load a segment descriptor; accesses to the interrupt descriptor table (IDT) when delivering an interrupt or exception; and accesses to the task-state segment (TSS) as part of a task switch or change of CPL.

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 patchset pass-through SMAP feature to guests, and let guests benefit from it.

Version 1:
* Remove SMAP bit from CR4_RESERVED_BITS.
* Add SMAP support when setting CR4
* Disable SMAP for guests in EPT realmode and EPT unpaging mode
* Expose SMAP feature to guest

Version 2:
* Change the logic of updating mmu permission bitmap for SMAP violation
* Expose SMAP feature to guest in the last patch of this series.

Version 3:
* Changes in update_permission_bitmask().
* Use a branchless way suggested by Paolo Bonzini to detect SMAP violation in permission_fault().

Version 4:
* Changes to some comments and code style.

Feng Wu (4):
KVM: Remove SMAP bit from CR4_RESERVED_BITS.
KVM: Add SMAP support when setting CR4
KVM: Disable SMAP for guests in EPT realmode and EPT unpaging mode
KVM: expose SMAP feature to guest

Quoted 9 lines
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/cpuid.c            |  2 +-
 arch/x86/kvm/cpuid.h            |  8 ++++++++
 arch/x86/kvm/mmu.c              | 34 ++++++++++++++++++++++++++++---
 arch/x86/kvm/mmu.h              | 44 +++++++++++++++++++++++++++++++++--------
 arch/x86/kvm/paging_tmpl.h      |  2 +-
 arch/x86/kvm/vmx.c              | 11 ++++++-----
 arch/x86/kvm/x86.c              |  9 ++++++++-
 8 files changed, 92 insertions(+), 20 deletions(-)