Re: [PATCH v3] KVM: X86: Fix load RFLAGS w/o the fixed bit

2017-12-07 Thread Quan Xu



On 2017/12/07 16:30, Wanpeng Li wrote:

From: Wanpeng Li 

  *** Guest State ***
  CR0: actual=0x0030, shadow=0x6010, 
gh_mask=fff7
  CR4: actual=0x2050, shadow=0x, 
gh_mask=e871
  CR3 = 0xfffbc000
  RSP = 0x  RIP = 0x
  RFLAGS=0x DR7 = 0x0400
 ^^

The failed vmentry is triggered by the following testcase when ept=Y:

 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 
 long r[5];

 int main()
 {
r[2] = open("/dev/kvm", O_RDONLY);
r[3] = ioctl(r[2], KVM_CREATE_VM, 0);
r[4] = ioctl(r[3], KVM_CREATE_VCPU, 7);
struct kvm_regs regs = {
.rflags = 0,
};
ioctl(r[4], KVM_SET_REGS, ®s);
ioctl(r[4], KVM_RUN, 0);
 }

X86 RFLAGS bit 1 is fixed set, userspace can simply clearing bit 1
of RFLAGS with KVM_SET_REGS ioctl which results in vmentry fails.
This patch fixes it by oring X86_EFLAGS_FIXED during ioctl.

Suggested-by: Jim Mattson 
Reviewed-by: David Hildenbrand 
Cc: Paolo Bonzini 
Cc: Radim Krčmář 
Cc: Jim Mattson 
Signed-off-by: Wanpeng Li 

  Reviewed-by: Quan Xu 



[PATCH v3] KVM: X86: Fix load RFLAGS w/o the fixed bit

2017-12-07 Thread Wanpeng Li
From: Wanpeng Li 

 *** Guest State ***
 CR0: actual=0x0030, shadow=0x6010, 
gh_mask=fff7
 CR4: actual=0x2050, shadow=0x, 
gh_mask=e871
 CR3 = 0xfffbc000
 RSP = 0x  RIP = 0x
 RFLAGS=0x DR7 = 0x0400
^^

The failed vmentry is triggered by the following testcase when ept=Y:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

long r[5];
int main()
{
r[2] = open("/dev/kvm", O_RDONLY);
r[3] = ioctl(r[2], KVM_CREATE_VM, 0);
r[4] = ioctl(r[3], KVM_CREATE_VCPU, 7);
struct kvm_regs regs = {
.rflags = 0,
};
ioctl(r[4], KVM_SET_REGS, ®s);
ioctl(r[4], KVM_RUN, 0);
}

X86 RFLAGS bit 1 is fixed set, userspace can simply clearing bit 1 
of RFLAGS with KVM_SET_REGS ioctl which results in vmentry fails.
This patch fixes it by oring X86_EFLAGS_FIXED during ioctl.

Suggested-by: Jim Mattson 
Reviewed-by: David Hildenbrand 
Cc: Paolo Bonzini 
Cc: Radim Krčmář 
Cc: Jim Mattson 
Signed-off-by: Wanpeng Li 
---
v2 -> v3:
 * move Oring X86_EFLAGS_FIXED to kvm_arch_vcpu_ioctl_set_regs
v1 -> v2:
 * Oring X86_EFLAGS_FIXED

 arch/x86/kvm/x86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ebe0a3a..0c5d55c 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7405,7 +7405,7 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, 
struct kvm_regs *regs)
 #endif
 
kvm_rip_write(vcpu, regs->rip);
-   kvm_set_rflags(vcpu, regs->rflags);
+   kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
 
vcpu->arch.exception.pending = false;
 
-- 
2.7.4