Xen Security Advisory 190 (CVE-2016-7777) - CR0.TS and CR0.EM not always honored for x86 HVM guests
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Xen Security Advisory CVE-2016-7777 / XSA-190
version 5
CR0.TS and CR0.EM not always honored for x86 HVM guests
UPDATES IN VERSION 5
====================
Public release.
ISSUE DESCRIPTION
=================
Instructions touching FPU, MMX, or XMM registers are required to raise a Device Not Available Exception (#NM) when either CR0.EM or CR0.TS are set. (Their AVX or AVX-512 extensions would consider only CR0.TS.) While during normal operation this is ensured by the hardware, if a guest modifies instructions while the hypervisor is preparing to emulate them, the #NM delivery could be missed.
Guest code in one task may thus (unintentionally or maliciously) read or modify register state belonging to another task in the same VM.
IMPACT
======
A malicious unprivileged guest user may be able to obtain or corrupt sensitive information (including cryptographic material) in other programs in the same guest.
VULNERABLE SYSTEMS
==================
All versions of Xen expose the vulnerabilty to their x86 HVM guests.
In order to exploit the vulnerability, the attacker needs to be able to trigger the Xen instruction emulator.
On Xen 4.7 the emulator can only be triggered: by user mode tasks which have been given access to memory-mapped IO; in guests which have been migrated between systems with CPUs from different vendors; or in guests which have been configured with a CPU vendor different from the host's.
On Xen 4.6 and earlier, all HVM guests can trigger the emulator by attempting to execute an invalid opcode, exposing the vulnerability.
The vulnerability is only exposed to x86 HVM guests.
The vulnerability is not exposed to x86 PV or ARM guests.
MITIGATION
==========
On Xen 4.7, not migrating across CPU vendors will avoid this vulnerability. (Unless the guest grants mmio access to unprivileged tasks, or has been configured with a specific CPU vendor, eg using the xl "cpuid" configuraton option.)
CREDITS
=======
This issue was discovered by Jan Beulich from SUSE.
RESOLUTION
==========
Applying the appropriate attached patch resolves this issue.
xsa190.patch xen-unstable, Xen 4.7.x xsa190-4.6.patch Xen 4.6.x xsa190-4.5.patch Xen 4.5.x, Xen 4.4.x
$ sha256sum xsa190* 21e7b1d08874527ab2e4cd23d467e9945afcd753dd3390ab2aaf9d24d231916c xsa190.patch 477d56c41cc2101432459ab79e4d5663aade779c36285f5c1d6d6ed4e34e1009 xsa190-4.5.patch dbfc4b36132c841959847dfbb85a188ee6489ad3b8d7ecec43c55a303a43df21 xsa190-4.6.patch $
DEPLOYMENT DURING EMBARGO
=========================
Deployment of the patches and/or mitigations described above (or others which are substantially similar) is permitted during the embargo, even on public-facing systems with untrusted guest users and administrators.
But: Distribution of updated software is prohibited (except to other members of the predisclosure list).
Predisclosure list members who wish to deploy significantly different patches and/or mitigations, please contact the Xen Project Security Team.
(Note: this during-embargo deployment notice is retained in post-embargo publicly released Xen Project advisories, even though it is then no longer applicable. This is to enable the community to have oversight of the Xen Project Security Team's decisionmaking.)
For more information about permissible uses of embargoed information, consult the Xen Project community's agreed Security Policy:
http://www.xenproject.org/security-policy.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBAgAGBQJX86WyAAoJEIP+FMlX6CvZZOQH/0rLFSZeiGeWDlKzQJoB3VLy zDvpDKjfhuwPyWT9+oyfwUHxARWuJkYSy85bpVuNWmxtb1tGy+QTjbSZgyVrsRXY 4t09MzhTF9CuNhqTghEGbFeGdh20ht3EoDjiwkjlbfb4TQ439e189qo9Oe0J/LvD 4XjL/oHza0YMI/wFviANUZvvTzAcjTAw1Zwk6dpnM17cwK4HduPYBncUyfDrSa3G 97nOraBXh/CiwWlm6goRSOI73ORUkYYBwJLGcq3a50HJPJ7pCbBaRJpDCalMPZ2B Lf+HO38HROEGBbTfkOjyZKkbTjQ2njTu0kHaBl+IVK8LI3PLv35n5MQ6qStYL/U= =7/xB
-----END PGP SIGNATURE-----
x86emul: honor guest CR0.TS and CR0.EM
We must not emulate any instructions accessing respective registers when either of these flags is set in the guest view of the register, or else we may do so on data not belonging to the guest's current task.
Being architecturally required behavior, the logic gets placed in the instruction emulator instead of hvmemul_get_fpu(). It should be noted, though, that hvmemul_get_fpu() being the only current handler for the get_fpu() callback, we don't have an active problem with CR4: Both CR4.OSFXSR and CR4.OSXSAVE get handled as necessary by that function.
This is XSA-190.
---
v4: Only raise #NM on FWAIT when CR0.TS and CR0.MP are set.
v3: Correct which exception to raise upon set CR0.EM.
v2: Require the read_cr hook to be set, which then requires a change to
the test code too.
---
The change to xen/arch/x86/hvm/emulate.c isn't strictly needed for fixing the security issue, but the patch would be rather incomplete without.
tools/tests/x86_emulator/test_x86_emulator.c +17 -0
@@ -158,6 +158,22 @@ static inline uint64_t xgetbv(uint32_t x (ebx & (1U <<5 )) !=0 ; \ }) +static int read_cr ( +unsigned int reg, +unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +/* Fake just enough state for the emulator's _get_fpu() to be happy. */ +switch ( reg ) + { +case 0 : + *val =0x00000001 ;/* PE */ +return X86EMUL_OKAY ; + } + +return X86EMUL_UNHANDLEABLE ; +} + int get_fpu( void (*exception_callback)(void *, struct cpu_user_regs *), void *exception_callback_arg,
@@ -189,6 +205,7 @@ static struct x86_emulate_ops emulops = .write = write, .cmpxchg = cmpxchg, .cpuid = cpuid, + .read_cr = read_cr, .get_fpu = get_fpu, };
xen/arch/x86/hvm/emulate.c +2 -2
@@ -1628,14 +1628,14 @@ static int hvmemul_get_fpu(switch ( type ) {case X86EMUL_FPU_fpu: +case X86EMUL_FPU_wait:break ;case X86EMUL_FPU_mmx:if ( !cpu_has_mmx )return X86EMUL_UNHANDLEABLE ;break ;case X86EMUL_FPU_xmm: -if ( (curr->arch.hvm_vcpu.guest_cr[0 ] &X86_CR0_EM ) || - !(curr->arch.hvm_vcpu.guest_cr[4 ] &X86_CR4_OSFXSR ) ) +if ( !(curr->arch.hvm_vcpu.guest_cr[4 ] &X86_CR4_OSFXSR ) )return X86EMUL_UNHANDLEABLE ;break ;case X86EMUL_FPU_ymm:
xen/arch/x86/x86_emulate/x86_emulate.c +49 -4
79 lines not on this page · raw message
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -115,6 +115,7 @@ struct __packed segment_register {/* FPU sub-types which may be requested via ->get_fpu(). */ enum x86_emulate_fpu_type { X86EMUL_FPU_fpu,/* Standard FPU coprocessor instruction set */ + X86EMUL_FPU_wait,/* WAIT/FWAIT instruction */ X86EMUL_FPU_mmx,/* MMX instruction set (%mm0-%mm7) */ X86EMUL_FPU_xmm,/* SSE instruction set (%xmm0-%xmm7/15) */ X86EMUL_FPU_ymm/* AVX/XOP instruction set (%ymm0-%ymm7/15) */
x86emul: honor guest CR0.TS and CR0.EM
We must not emulate any instructions accessing respective registers when either of these flags is set in the guest view of the register, or else we may do so on data not belonging to the guest's current task.
Being architecturally required behavior, the logic gets placed in the instruction emulator instead of hvmemul_get_fpu(). It should be noted, though, that hvmemul_get_fpu() being the only current handler for the get_fpu() callback, we don't have an active problem with CR4: Both CR4.OSFXSR and CR4.OSXSAVE get handled as necessary by that function.
This is XSA-190.
tools/tests/x86_emulator/test_x86_emulator.c +17 -0
@@ -129,6 +129,22 @@ static inline uint64_t xgetbv(uint32_t x (ebx & (1U <<5 )) !=0 ; \ }) +static int read_cr ( +unsigned int reg, +unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +/* Fake just enough state for the emulator's _get_fpu() to be happy. */ +switch ( reg ) + { +case 0 : + *val =0x00000001 ;/* PE */ +return X86EMUL_OKAY ; + } + +return X86EMUL_UNHANDLEABLE ; +} + int get_fpu( void (*exception_callback)(void *, struct cpu_user_regs *), void *exception_callback_arg,
@@ -160,6 +176,7 @@ static struct x86_emulate_ops emulops = .write = write, .cmpxchg = cmpxchg, .cpuid = cpuid, + .read_cr = read_cr, .get_fpu = get_fpu, };
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1192,6 +1192,7 @@ static int hvmemul_get_fpu(switch ( type ) {case X86EMUL_FPU_fpu: +case X86EMUL_FPU_wait:break ;case X86EMUL_FPU_mmx:if ( !cpu_has_mmx )
@@ -1199,7 +1200,6 @@ static int hvmemul_get_fpu(break ;case X86EMUL_FPU_xmm:if ( !cpu_has_xmm || - (curr->arch.hvm_vcpu.guest_cr[0 ] &X86_CR0_EM ) || !(curr->arch.hvm_vcpu.guest_cr[4 ] &X86_CR4_OSFXSR ) )return X86EMUL_UNHANDLEABLE ;break ;
xen/arch/x86/x86_emulate/x86_emulate.c +49 -4
79 lines not on this page · raw message
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -114,6 +114,7 @@ struct __packed segment_register {/* FPU sub-types which may be requested via ->get_fpu(). */ enum x86_emulate_fpu_type { X86EMUL_FPU_fpu,/* Standard FPU coprocessor instruction set */ + X86EMUL_FPU_wait,/* WAIT/FWAIT instruction */ X86EMUL_FPU_mmx,/* MMX instruction set (%mm0-%mm7) */ X86EMUL_FPU_xmm,/* SSE instruction set (%xmm0-%xmm7/15) */ X86EMUL_FPU_ymm/* AVX/XOP instruction set (%ymm0-%ymm7/15) */
x86emul: honor guest CR0.TS and CR0.EM
We must not emulate any instructions accessing respective registers when either of these flags is set in the guest view of the register, or else we may do so on data not belonging to the guest's current task.
Being architecturally required behavior, the logic gets placed in the instruction emulator instead of hvmemul_get_fpu(). It should be noted, though, that hvmemul_get_fpu() being the only current handler for the get_fpu() callback, we don't have an active problem with CR4: Both CR4.OSFXSR and CR4.OSXSAVE get handled as necessary by that function.
This is XSA-190.
tools/tests/x86_emulator/test_x86_emulator.c +17 -0
@@ -129,6 +129,22 @@ static inline uint64_t xgetbv(uint32_t x (ebx & (1U <<5 )) !=0 ; \ }) +static int read_cr ( +unsigned int reg, +unsigned long *val, +struct x86_emulate_ctxt *ctxt) +{ +/* Fake just enough state for the emulator's _get_fpu() to be happy. */ +switch ( reg ) + { +case 0 : + *val =0x00000001 ;/* PE */ +return X86EMUL_OKAY ; + } + +return X86EMUL_UNHANDLEABLE ; +} + int get_fpu( void (*exception_callback)(void *, struct cpu_user_regs *), void *exception_callback_arg,
@@ -160,6 +176,7 @@ static struct x86_emulate_ops emulops = .write = write, .cmpxchg = cmpxchg, .cpuid = cpuid, + .read_cr = read_cr, .get_fpu = get_fpu, };
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1557,6 +1557,7 @@ static int hvmemul_get_fpu(switch ( type ) {case X86EMUL_FPU_fpu: +case X86EMUL_FPU_wait:break ;case X86EMUL_FPU_mmx:if ( !cpu_has_mmx )
@@ -1564,7 +1565,6 @@ static int hvmemul_get_fpu(break ;case X86EMUL_FPU_xmm:if ( !cpu_has_xmm || - (curr->arch.hvm_vcpu.guest_cr[0 ] &X86_CR0_EM ) || !(curr->arch.hvm_vcpu.guest_cr[4 ] &X86_CR4_OSFXSR ) )return X86EMUL_UNHANDLEABLE ;break ;
xen/arch/x86/x86_emulate/x86_emulate.c +49 -4
79 lines not on this page · raw message
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
@@ -115,6 +115,7 @@ struct __packed segment_register {/* FPU sub-types which may be requested via ->get_fpu(). */ enum x86_emulate_fpu_type { X86EMUL_FPU_fpu,/* Standard FPU coprocessor instruction set */ + X86EMUL_FPU_wait,/* WAIT/FWAIT instruction */ X86EMUL_FPU_mmx,/* MMX instruction set (%mm0-%mm7) */ X86EMUL_FPU_xmm,/* SSE instruction set (%xmm0-%xmm7/15) */ X86EMUL_FPU_ymm/* AVX/XOP instruction set (%ymm0-%ymm7/15) */
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel