x86: Enable Supervisor Mode Access Prevention (SMAP) for Xen
PATCH v4 8/10 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 | 14 +++++++++ xen/arch/x86/traps.c | 58 +++++++++++++++++++++++++++---------- xen/include/asm-x86/cpufeature.h | 1 + xen/include/asm-x86/domain.h | 6 ++-- 5 files changed, 69 insertions(+), 17 deletions(-)
docs/misc/xen-command-line.markdown +7 -0
@@ -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 +14 -0
@@ -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. */
@@ -1279,6 +1283,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)if ( cpu_has_smep )set_in_cr4 (X86_CR4_SMEP ); +if ( disable_smap ) +setup_clear_cpu_cap (X86_FEATURE_SMAP ); +if ( cpu_has_fsgsbase )set_in_cr4 (X86_CR4_FSGSBASE );
@@ -1394,6 +1401,13 @@ 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 ( 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
@@ -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
@@ -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
xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1410
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 10:23, <[email protected]> wrote:
xen/arch/x86/setup.c +7
-0@@ -1394,6 +1401,13 @@ 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 ( cpu_has_smap ) +set_in_cr4 (X86_CR4_SMAP ); +
I should have noticed this on the previous round already - the way it's being done right now will leave APs with SMAP disabled for an indeterminate amount of time (until they first reload CR4 from mmu_cr4_features).
xen/arch/x86/traps.c static int handle_gdt_ldt_mapping_fault( · line 1190
Quoted 13 lines
xen/arch/x86/traps.c +2
-1@@ -1182,11 +1182,12 @@ static int handle_gdt_ldt_mapping_fault(enum pf_type { real_fault, smep_fault, + smap_fault, spurious_fault };static enum pf_type __page_fault_type ( -unsigned long addr,unsigned int error_code) +unsigned long addr,const struct cpu_user_regs *regs)
I see you followed the constification request here.
xen/arch/x86/traps.c static enum pf_type __page_fault_type( · line 1297
Quoted 3 lines
xen/arch/x86/traps.c +1
-1static enum pf_type spurious_page_fault ( -unsigned long addr,unsigned int error_code) +unsigned long addr,struct cpu_user_regs *regs)
But why not here?
Jan
Quoted 26 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 7:20 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: Re: [PATCH v4 08/10] x86: Enable Supervisor Mode Access Prevention (SMAP) for XenReview void __init noreturn __start_xen(unsigned long
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 10:23, <[email protected]> wrote:
(quoted diff) +0
-0@@ -1394,6 +1401,13 @@ void __init noreturn __start_xen(unsigned long
mbi_p)xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p)
Quoted 5 lines
xen/arch/x86/setup.c +2
-0bootstrap_map, cmdline) != 0)panic ("Could not set up DOM0 guest OS" ); +/* +* Enable SMAP after constructing domain0, since there are lots of accesses to
xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1410
Quoted 5 lines
xen/arch/x86/setup.c +5
-0+* user pages in construct_dom0(), which is safe at the current stage. +*/ +if ( cpu_has_smap ) +set_in_cr4 (X86_CR4_SMAP ); +
Quoted 4 lines
I should have noticed this on the previous round already - the way it's being done right now will leave APs with SMAP disabled for an indeterminate amount of time (until they first reload CR4 from
mmu_cr4_features).
Yes, that's a question. In that case, we should move this part to the place where it was. So we should come back to the question about how to handle stac()/clac() in construct_dom0(), right?
Quoted 17 lines
Quoted 4 lines
xen/arch/x86/traps.c static int handle_gdt_ldt_mapping_fault( · line 1190
Quoted 10 lines
xen/arch/x86/traps.c +2
-1@@ -1182,11 +1182,12 @@ static int handle_gdt_ldt_mapping_fault(enum pf_type { real_fault, smep_fault, + smap_fault, spurious_fault };static enum pf_type __page_fault_type ( -unsigned long addr,unsigned int error_code) +unsigned long addr,const struct cpu_user_regs *regs)I see you followed the constification request here.
Quoted 7 lines
xen/arch/x86/traps.c static enum pf_type __page_fault_type( · line 1297
Quoted 3 lines
xen/arch/x86/traps.c +1
-1static enum pf_type spurious_page_fault ( -unsigned long addr,unsigned int error_code) +unsigned long addr,struct cpu_user_regs *regs)But why not here?
Jan
Thanks,
Feng
Quoted 32 lines
Quoted 27 lines
Quoted 1 line · On 06.05.14 at 07:19, <[email protected]> wrote:
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 7:20 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: Re: [PATCH v4 08/10] x86: Enable Supervisor Mode Access Prevention (SMAP) for XenReview void __init noreturn __start_xen(unsigned long
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 10:23, <[email protected]> wrote:
(quoted diff) +0
-0@@ -1394,6 +1401,13 @@ void __init noreturn __start_xen(unsigned long
mbi_p)xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p)
Quoted 5 lines
xen/arch/x86/setup.c +2
-0bootstrap_map, cmdline) != 0)panic ("Could not set up DOM0 guest OS" ); +/* +* Enable SMAP after constructing domain0, since there are lots of accesses to
xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1410
Quoted 5 lines
xen/arch/x86/setup.c +5
-0+* user pages in construct_dom0(), which is safe at the current stage. +*/ +if ( cpu_has_smap ) +set_in_cr4 (X86_CR4_SMAP ); +Quoted 4 lines
I should have noticed this on the previous round already - the way it's being done right now will leave APs with SMAP disabled for an indeterminate amount of time (until they first reload CR4 from
mmu_cr4_features).
Quoted 5 lines
Yes, that's a question. In that case, we should move this part to the place where
it was. So we should come back to the question about how to handle stac()/clac()
inconstruct_dom0(), right?
An option might be to move it back to where it was, but clear the flag temporarily on the one CPU calling construct_dom0(). That's a little hackish, but properly commented acceptable imo.
Jan
Quoted 52 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, May 06, 2014 4:02 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v4 08/10] x86: Enable Supervisor Mode Access Prevention (SMAP) for XenQuoted 10 lines
Quoted 10 lines
Quoted 1 line · On 06.05.14 at 07:19, <[email protected]> wrote:
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 7:20 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: Re: [PATCH v4 08/10] x86: Enable Supervisor Mode AccessPrevention
Quoted 4 lines
Quoted 4 lines
(SMAP) for Xen
Review void __init noreturn __start_xen(unsigned
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 10:23, <[email protected]> wrote:
(quoted diff) +0
-0@@ -1394,6 +1401,13 @@ void __init noreturn __start_xen(unsignedlong
Quoted 8 lines
Quoted 8 lines
mbi_p)xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p)
Quoted 5 lines
xen/arch/x86/setup.c +2
-0bootstrap_map, cmdline) != 0)panic ("Could not set up DOM0 guest OS" ); +/* +* Enable SMAP after constructing domain0, since there are lots of accesses to
Quoted 1 line
(quoted diff) +1
-0+ * user pages in construct_dom0(), which is safe at the currentstage.
Quoted 10 lines
Quoted 9 lines
xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1410
Quoted 4 lines
xen/arch/x86/setup.c +4
-0+*/ +if ( cpu_has_smap ) +set_in_cr4 (X86_CR4_SMAP ); +I should have noticed this on the previous round already - the way it's being done right now will leave APs with SMAP disabled for an indeterminate amount of time (until they first reload CR4 from
mmu_cr4_features).Quoted 5 lines
Yes, that's a question. In that case, we should move this part to the place where
it was. So we should come back to the question about how to handle stac()/clac()
inconstruct_dom0(), right?
Quoted 3 lines
An option might be to move it back to where it was, but clear the flag temporarily on the one CPU calling
construct_dom0(). That's a little hackish, but properly commented acceptable imo.
Do you mean clear SMAP bit in CR4 before construct_dom0() and set it after that like this?
write_cr4(read_cr4() & ~X86_CR4_SMAP);construct_dom0();write_cr4(read_cr4() | X86_CR4_SMAP);
Quoted 2 lines
Jan
Thanks,
Feng
Quoted 19 lines
Quoted 15 lines
Quoted 1 line · On 06.05.14 at 10:24, <[email protected]> wrote:
From: Jan Beulich [mailto:[email protected]]
Quoted 7 lines
Quoted 6 lines
Quoted 1 line · On 06.05.14 at 07:19, <[email protected]> wrote:
From: Jan Beulich [mailto:[email protected]]
I should have noticed this on the previous round already - the way it's being done right now will leave APs with SMAP disabled for an indeterminate amount of time (until they first reload CR4 frommmu_cr4_features).Quoted 5 lines
Yes, that's a question. In that case, we should move this part to the place where
it was. So we should come back to the question about how to handle stac()/clac()
inconstruct_dom0(), right?Quoted 3 lines
An option might be to move it back to where it was, but clear the flag temporarily on the one CPU calling
construct_dom0(). That's a little hackish, but properly commented acceptable imo.
Quoted 6 lines
Do you mean clear SMAP bit in CR4 before
construct_dom0()and set it after that like this?
write_cr4(read_cr4() & ~X86_CR4_SMAP);construct_dom0();write_cr4(read_cr4() | X86_CR4_SMAP);
Yes, properly conditionalized of course (at least the second one).
Jan
Quoted 30 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, May 06, 2014 5:11 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v4 08/10] x86: Enable Supervisor Mode Access Prevention (SMAP) for XenQuoted 19 lines
Quoted 18 lines
Quoted 1 line · On 06.05.14 at 10:24, <[email protected]> wrote:
From: Jan Beulich [mailto:[email protected]]
Quoted 7 lines
Quoted 6 lines
Quoted 1 line · On 06.05.14 at 07:19, <[email protected]> wrote:
From: Jan Beulich [mailto:[email protected]]
I should have noticed this on the previous round already - the way it's being done right now will leave APs with SMAP disabled for an indeterminate amount of time (until they first reload CR4 frommmu_cr4_features).Quoted 5 lines
Yes, that's a question. In that case, we should move this part to the place where
it was. So we should come back to the question about how to handle stac()/clac()
inconstruct_dom0(), right?An option might be to move it back to where it was, but clear the flag temporarily on the one CPU calling
construct_dom0(). That's a little hackish, but properly commented acceptable imo.
Quoted 9 lines
Quoted 6 lines
Do you mean clear SMAP bit in CR4 before
construct_dom0()and set it after that like this?
write_cr4(read_cr4() & ~X86_CR4_SMAP);construct_dom0();write_cr4(read_cr4() | X86_CR4_SMAP);Yes, properly conditionalized of course (at least the second one).
Yes, need to check cpu_has_smap first. Thanks!
Quoted 1 line
Jan
Thanks,
Feng