* [PATCH v3 1/4] x86, cpu: Add CPU flags for SMEP
2011-05-18 1:44 [PATCH v3 0/4] Enable SMEP CPU Feature Fenghua Yu
@ 2011-05-18 1:44 ` Fenghua Yu
2011-05-18 4:01 ` [tip:x86/cpufeature] x86, cpufeature: Add cpufeature flag " tip-bot for Fenghua Yu
2011-05-18 1:44 ` [PATCH v3 2/4] x86, cpu: Add SMEP CPU feature in CR4 Fenghua Yu
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Fenghua Yu @ 2011-05-18 1:44 UTC (permalink / raw)
To: Ingo Molnar, Thomas Gleixner, H Peter Anvin, Asit K Mallick,
Linus Torvalds, Avi Kivity, Arjan van de Ven, Andrew Morton,
Andi Kleen
Cc: linux-kernel, Fenghua Yu
From: Fenghua Yu <fenghua.yu@intel.com>
Add support for newly documented SMEP (Supervisor Mode Execution Protection) CPU
feature flags.
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
---
arch/x86/include/asm/cpufeature.h | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h
index 50c0d30..ca1e0ac 100644
--- a/arch/x86/include/asm/cpufeature.h
+++ b/arch/x86/include/asm/cpufeature.h
@@ -195,6 +195,7 @@
/* Intel-defined CPU features, CPUID level 0x00000007:0 (ebx), word 9 */
#define X86_FEATURE_FSGSBASE (9*32+ 0) /* {RD/WR}{FS/GS}BASE instructions*/
+#define X86_FEATURE_SMEP (9*32+ 7) /* Supervisor Mode Execution Protection */
#if defined(__KERNEL__) && !defined(__ASSEMBLY__)
--
1.7.2
^ permalink raw reply related [flat|nested] 7+ messages in thread* [tip:x86/cpufeature] x86, cpufeature: Add cpufeature flag for SMEP
2011-05-18 1:44 ` [PATCH v3 1/4] x86, cpu: Add CPU flags for SMEP Fenghua Yu
@ 2011-05-18 4:01 ` tip-bot for Fenghua Yu
0 siblings, 0 replies; 7+ messages in thread
From: tip-bot for Fenghua Yu @ 2011-05-18 4:01 UTC (permalink / raw)
To: linux-tip-commits; +Cc: linux-kernel, hpa, mingo, fenghua.yu, tglx, hpa
Commit-ID: d0281a257f370b09c410e466571858b4e12869c9
Gitweb: http://git.kernel.org/tip/d0281a257f370b09c410e466571858b4e12869c9
Author: Fenghua Yu <fenghua.yu@intel.com>
AuthorDate: Tue, 17 May 2011 18:44:26 -0700
Committer: H. Peter Anvin <hpa@linux.intel.com>
CommitDate: Tue, 17 May 2011 20:56:59 -0700
x86, cpufeature: Add cpufeature flag for SMEP
Add support for newly documented SMEP (Supervisor Mode Execution Protection) CPU
feature flag.
SMEP prevents the CPU in kernel-mode to jump to an executable page
that has the user flag set in the PTE. This prevents the kernel from
executing user-space code accidentally or maliciously, so it for
example prevents kernel exploits from jumping to specially prepared
user-mode shell code.
[ hpa: added better description by Ingo Molnar ]
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
LKML-Reference: <1305683069-25394-2-git-send-email-fenghua.yu@intel.com>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
---
arch/x86/include/asm/cpufeature.h | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h
index 7f2f7b1..8808cdb 100644
--- a/arch/x86/include/asm/cpufeature.h
+++ b/arch/x86/include/asm/cpufeature.h
@@ -195,6 +195,7 @@
/* Intel-defined CPU features, CPUID level 0x00000007:0 (ebx), word 9 */
#define X86_FEATURE_FSGSBASE (9*32+ 0) /* {RD/WR}{FS/GS}BASE instructions*/
+#define X86_FEATURE_SMEP (9*32+ 7) /* Supervisor Mode Execution Protection */
#define X86_FEATURE_ERMS (9*32+ 9) /* Enhanced REP MOVSB/STOSB */
#if defined(__KERNEL__) && !defined(__ASSEMBLY__)
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 2/4] x86, cpu: Add SMEP CPU feature in CR4
2011-05-18 1:44 [PATCH v3 0/4] Enable SMEP CPU Feature Fenghua Yu
2011-05-18 1:44 ` [PATCH v3 1/4] x86, cpu: Add CPU flags for SMEP Fenghua Yu
@ 2011-05-18 1:44 ` Fenghua Yu
2011-05-18 5:13 ` [tip:x86/smep] " tip-bot for Fenghua Yu
2011-05-18 1:44 ` [PATCH v3 3/4] x86, head_32/64.S: Enable SMEP Fenghua Yu
2011-05-18 1:44 ` [PATCH v3 4/4] x86/kernel/cpu/common.c: Disable SMEP by kernel option nosmep Fenghua Yu
3 siblings, 1 reply; 7+ messages in thread
From: Fenghua Yu @ 2011-05-18 1:44 UTC (permalink / raw)
To: Ingo Molnar, Thomas Gleixner, H Peter Anvin, Asit K Mallick,
Linus Torvalds, Avi Kivity, Arjan van de Ven, Andrew Morton,
Andi Kleen
Cc: linux-kernel, Fenghua Yu
From: Fenghua Yu <fenghua.yu@intel.com>
Add support for newly documented SMEP (Supervisor Mode Execution Protection)
CPU feature in CR4.
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
---
arch/x86/include/asm/processor-flags.h | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/arch/x86/include/asm/processor-flags.h b/arch/x86/include/asm/processor-flags.h
index a898a2b..59ab4df 100644
--- a/arch/x86/include/asm/processor-flags.h
+++ b/arch/x86/include/asm/processor-flags.h
@@ -60,6 +60,7 @@
#define X86_CR4_OSXMMEXCPT 0x00000400 /* enable unmasked SSE exceptions */
#define X86_CR4_VMXE 0x00002000 /* enable VMX virtualization */
#define X86_CR4_OSXSAVE 0x00040000 /* enable xsave and xrestore */
+#define X86_CR4_SMEP 0x00100000 /* enable SMEP support */
/*
* x86-64 Task Priority Register, CR8
--
1.7.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [tip:x86/smep] x86, cpu: Add SMEP CPU feature in CR4
2011-05-18 1:44 ` [PATCH v3 2/4] x86, cpu: Add SMEP CPU feature in CR4 Fenghua Yu
@ 2011-05-18 5:13 ` tip-bot for Fenghua Yu
0 siblings, 0 replies; 7+ messages in thread
From: tip-bot for Fenghua Yu @ 2011-05-18 5:13 UTC (permalink / raw)
To: linux-tip-commits; +Cc: linux-kernel, hpa, mingo, fenghua.yu, tglx, hpa
Commit-ID: dc23c0bccf5eea171c87b3db285d032b9a5f06c4
Gitweb: http://git.kernel.org/tip/dc23c0bccf5eea171c87b3db285d032b9a5f06c4
Author: Fenghua Yu <fenghua.yu@intel.com>
AuthorDate: Tue, 17 May 2011 18:44:27 -0700
Committer: H. Peter Anvin <hpa@linux.intel.com>
CommitDate: Tue, 17 May 2011 21:06:42 -0700
x86, cpu: Add SMEP CPU feature in CR4
Add support for newly documented SMEP (Supervisor Mode Execution Protection)
CPU feature in CR4.
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
LKML-Reference: <1305683069-25394-3-git-send-email-fenghua.yu@intel.com>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
---
arch/x86/include/asm/processor-flags.h | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/arch/x86/include/asm/processor-flags.h b/arch/x86/include/asm/processor-flags.h
index a898a2b..59ab4df 100644
--- a/arch/x86/include/asm/processor-flags.h
+++ b/arch/x86/include/asm/processor-flags.h
@@ -60,6 +60,7 @@
#define X86_CR4_OSXMMEXCPT 0x00000400 /* enable unmasked SSE exceptions */
#define X86_CR4_VMXE 0x00002000 /* enable VMX virtualization */
#define X86_CR4_OSXSAVE 0x00040000 /* enable xsave and xrestore */
+#define X86_CR4_SMEP 0x00100000 /* enable SMEP support */
/*
* x86-64 Task Priority Register, CR8
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 3/4] x86, head_32/64.S: Enable SMEP
2011-05-18 1:44 [PATCH v3 0/4] Enable SMEP CPU Feature Fenghua Yu
2011-05-18 1:44 ` [PATCH v3 1/4] x86, cpu: Add CPU flags for SMEP Fenghua Yu
2011-05-18 1:44 ` [PATCH v3 2/4] x86, cpu: Add SMEP CPU feature in CR4 Fenghua Yu
@ 2011-05-18 1:44 ` Fenghua Yu
2011-05-18 1:44 ` [PATCH v3 4/4] x86/kernel/cpu/common.c: Disable SMEP by kernel option nosmep Fenghua Yu
3 siblings, 0 replies; 7+ messages in thread
From: Fenghua Yu @ 2011-05-18 1:44 UTC (permalink / raw)
To: Ingo Molnar, Thomas Gleixner, H Peter Anvin, Asit K Mallick,
Linus Torvalds, Avi Kivity, Arjan van de Ven, Andrew Morton,
Andi Kleen
Cc: linux-kernel, Fenghua Yu
From: Fenghua Yu <fenghua.yu@intel.com>
Enable newly documented SMEP (Supervisor Mode Execution Protection) CPU
feature in kernel.
SMEP prevents the CPU in kernel-mode to jump to an executable page that does
not have the kernel/system flag set in the pte. This prevents the kernel
from executing user-space code accidentally or maliciously, so it for example
prevents kernel exploits from jumping to specially prepared user-mode shell
code. The violation will cause page fault #PF and will have error code
identical to XD violation.
CR4.SMEP (bit 20) is 0 at power-on. If the feature is supported by CPU
(X86_FEATURE_SMEP), enable SMEP by setting CR4.SMEP. New kernel
option nosmep disables the feature even if the feature is supported by CPU.
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
---
arch/x86/kernel/head_32.S | 17 +++++++++++++----
arch/x86/kernel/head_64.S | 13 +++++++++++--
2 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/arch/x86/kernel/head_32.S b/arch/x86/kernel/head_32.S
index ce0be7c..5325c02 100644
--- a/arch/x86/kernel/head_32.S
+++ b/arch/x86/kernel/head_32.S
@@ -308,11 +308,20 @@ default_entry:
movl cr4_bits,%edx
andl %edx,%edx
jz 6f
- movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
- orl %edx,%eax
- movl %eax,%cr4
+ movl %cr4,%edi # Turn on paging options (PSE,PAE,..)
+ orl %edx,%edi
- testb $X86_CR4_PAE, %al # check if PAE is enabled
+ /* Check if SMEP is supported by the processor */
+ movl $0x7, %eax
+ movl $0, %ecx
+ cpuid
+ btl $7, %ebx
+ jnc 1f
+ /* Enable SMEP */
+ orl $(X86_CR4_SMEP), %edi
+1: movl %edi, %cr4
+
+ test $X86_CR4_PAE, %di # check if PAE is enabled
jz 6f
/* Check if extended functions are implemented */
diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
index e11e394..220ec5f 100644
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -161,8 +161,17 @@ ENTRY(secondary_startup_64)
*/
/* Enable PAE mode and PGE */
- movl $(X86_CR4_PAE | X86_CR4_PGE), %eax
- movq %rax, %cr4
+ movl $(X86_CR4_PAE | X86_CR4_PGE), %edi
+
+ /* Check if SMEP is supported by the processor */
+ movl $0x7, %eax
+ movl $0, %ecx
+ cpuid
+ btl $7, %ebx
+ jnc 1f
+ /* Enable PAE mode, PGE, and SMEP */
+ movl $(X86_CR4_PAE | X86_CR4_PGE | X86_CR4_SMEP), %edi
+1: movq %rdi, %cr4
/* Setup early boot stage 4 level pagetables. */
movq $(init_level4_pgt - __START_KERNEL_map), %rax
--
1.7.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 4/4] x86/kernel/cpu/common.c: Disable SMEP by kernel option nosmep
2011-05-18 1:44 [PATCH v3 0/4] Enable SMEP CPU Feature Fenghua Yu
` (2 preceding siblings ...)
2011-05-18 1:44 ` [PATCH v3 3/4] x86, head_32/64.S: Enable SMEP Fenghua Yu
@ 2011-05-18 1:44 ` Fenghua Yu
3 siblings, 0 replies; 7+ messages in thread
From: Fenghua Yu @ 2011-05-18 1:44 UTC (permalink / raw)
To: Ingo Molnar, Thomas Gleixner, H Peter Anvin, Asit K Mallick,
Linus Torvalds, Avi Kivity, Arjan van de Ven, Andrew Morton,
Andi Kleen
Cc: linux-kernel, Fenghua Yu
From: Fenghua Yu <fenghua.yu@intel.com>
SMEP is enabled unconditionally on all CPUs that support it. The nosmep boot
option would turn it off shortly afterwards.
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
---
Documentation/kernel-parameters.txt | 4 ++++
arch/x86/kernel/cpu/common.c | 22 ++++++++++++++++++++++
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
index cc85a92..76c67e5 100644
--- a/Documentation/kernel-parameters.txt
+++ b/Documentation/kernel-parameters.txt
@@ -1664,6 +1664,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
noexec=on: enable non-executable mappings (default)
noexec=off: disable non-executable mappings
+ nosmep [X86]
+ Disable SMEP (Supervisor Mode Execution Protection)
+ even if it is supported by the processor.
+
noexec32 [X86-64]
This affects only 32-bit executables.
noexec32=on: enable non-executable mappings (default)
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index e2ced00..9dcec58 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -254,6 +254,27 @@ static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
}
#endif
+static int disable_smep __initdata;
+
+static __init int setup_nosmep(char *arg)
+{
+ disable_smep = 1;
+ return 1;
+}
+__setup("nosmep", setup_nosmep);
+
+/*
+ * If SMEP is supported by the processor, SMEP has been enabled in CR4 earlier.
+ * But if kernel option "nosmep" is given, we disable SMEP here.
+ */
+static __init void config_smep(struct cpuinfo_x86 *c)
+{
+ if (cpu_has(c, X86_FEATURE_SMEP) && disable_smep) {
+ setup_clear_cpu_cap(X86_FEATURE_SMEP);
+ clear_in_cr4(X86_CR4_SMEP);
+ }
+}
+
/*
* Some CPU features depend on higher CPUID levels, which may not always
* be available due to CPUID level capping or broken virtualization
@@ -737,6 +758,7 @@ static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
get_cpu_vendor(c);
get_cpu_cap(c);
+ config_smep(c);
if (c->cpuid_level >= 0x00000001) {
c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
--
1.7.2
^ permalink raw reply related [flat|nested] 7+ messages in thread