Enable SMEP CPU Feature
Subsystems (2)
v3 cover Enable SMEP CPU Feature
From: Fenghua Yu <[email protected]>
Intel new CPU supports SMEP (Supervisor Mode Execution Protection). SMEP prevents kernel from executing code in application. Updated Intel SDM describes this CPU feature. The document will be published soon.
Note: This patch set doesn't enable the SMEP feature in KVM. A seperate patch will be pushed for enabling the feature in KVM.
Fenghua Yu (4):
x86, cpu: Add CPU flags for SMEP
x86, cpu: Add SMEP CPU feature in CR4
x86, head_32/64.S: Enable SMEP x86/kernel/cpu/common.c: Disable SMEP by kernel option nosmep
Documentation/kernel-parameters.txt | 4 ++++ arch/x86/include/asm/cpufeature.h | 1 + arch/x86/include/asm/processor-flags.h | 1 + arch/x86/kernel/cpu/common.c | 22 ++++++++++++++++++++++ arch/x86/kernel/head_32.S | 17 +++++++++++++---- arch/x86/kernel/head_64.S | 13 +++++++++++-- 6 files changed, 52 insertions(+), 6 deletions(-)
Signature · 1 line
1.7.2
PATCH v3 1/4 x86, cpu: Add CPU flags for SMEP
From: Fenghua Yu <[email protected]>
Add support for newly documented SMEP (Supervisor Mode Execution Protection) CPU feature flags.
--- arch/x86/include/asm/cpufeature.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/include/asm/cpufeature.h +1 -0
@@ -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__)
Signature · 1 line
1.7.2
[tip:x86/cpufeature] x86, cpufeature: Add cpufeature flag for SMEP
Commit-ID: d0281a257f370b09c410e466571858b4e12869c9 Gitweb: http://git.kernel.org/tip/d0281a257f370b09c410e466571858b4e12869c9 Author: Fenghua Yu <[email protected]> AuthorDate: Tue, 17 May 2011 18:44:26 -0700 Committer: H. Peter Anvin <[email protected]> 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 ]
LKML-Reference: <[email protected]>
--- arch/x86/include/asm/cpufeature.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/include/asm/cpufeature.h +1 -0
@@ -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__)
PATCH v3 2/4 x86, cpu: Add SMEP CPU feature in CR4
In reply to Fenghua YuFrom: Fenghua Yu <[email protected]>
Add support for newly documented SMEP (Supervisor Mode Execution Protection) CPU feature in CR4.
--- arch/x86/include/asm/processor-flags.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/include/asm/processor-flags.h +1 -0
@@ -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
Signature · 1 line
1.7.2
[tip:x86/smep] x86, cpu: Add SMEP CPU feature in CR4
Commit-ID: dc23c0bccf5eea171c87b3db285d032b9a5f06c4 Gitweb: http://git.kernel.org/tip/dc23c0bccf5eea171c87b3db285d032b9a5f06c4 Author: Fenghua Yu <[email protected]> AuthorDate: Tue, 17 May 2011 18:44:27 -0700 Committer: H. Peter Anvin <[email protected]> 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.
LKML-Reference: <[email protected]>
--- arch/x86/include/asm/processor-flags.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/include/asm/processor-flags.h +1 -0
@@ -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
PATCH v3 3/4 x86, head_32/64.S: Enable SMEP
In reply to Fenghua YuFrom: Fenghua Yu <[email protected]>
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.
--- arch/x86/kernel/head_32.S | 17 +++++++++++++---- arch/x86/kernel/head_64.S | 13 +++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-)
arch/x86/kernel/head_32.S +13 -4
@@ -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 */
arch/x86/kernel/head_64.S +11 -2
@@ -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
Signature · 1 line
1.7.2
PATCH v3 4/4 x86/kernel/cpu/common.c: Disable SMEP by kernel option nosmep
In reply to Fenghua YuFrom: Fenghua Yu <[email protected]>
SMEP is enabled unconditionally on all CPUs that support it. The nosmep boot option would turn it off shortly afterwards.
--- Documentation/kernel-parameters.txt | 4 ++++ arch/x86/kernel/cpu/common.c | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 0 deletions(-)
Documentation/kernel-parameters.txt +4 -0
@@ -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)
arch/x86/kernel/cpu/common.c +22 -0
@@ -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 ;
Signature · 1 line
1.7.2