Re: [PATCH v5 1/3] KVM: X86: Save&restore the triple fault request

From: Chenyi Qiang
Date: Thu Apr 07 2022 - 02:13:09 EST




On 4/7/2022 5:15 AM, Sean Christopherson wrote:
On Tue, Apr 05, 2022, Sean Christopherson wrote:
On Tue, Apr 05, 2022, Sean Christopherson wrote:
On Fri, Mar 18, 2022, Chenyi Qiang wrote:
@@ -4976,6 +4980,9 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
}
}
+ if (events->flags & KVM_VCPUEVENT_TRIPLE_FAULT)
+ kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
+
kvm_make_request(KVM_REQ_EVENT, vcpu);

Looks correct, but this really needs a selftest, at least for the SET path since
the intent is to use that for the NOTIFY handling. Doesn't need to be super fancy,
e.g. do port I/O from L2, inject a triple fault, and verify L1 sees the appropriate
exit.

Aha! And for the GET path, abuse KVM_X86_SET_MCE with CR4.MCE=0 to coerce KVM into
making a KVM_REQ_TRIPLE_FAULT, that way there's no need to try and hit a timing
window to intercept the request.

Drat, I bet that MCE path means the WARN in nested_vmx_vmexit() can be triggered
by userspace. If so, this patch makes it really, really easy to hit, e.g. queue the
request while L2 is active, then do KVM_SET_NESTED_STATE to force an "exit" without
bouncing through kvm_check_nested_events().

WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu))

I don't think SVM has a user-triggerable WARN, but the request should still be
dropped on forced exit from L2, e.g. I believe this is the correct fix:

Confirmed the WARN can be triggered by abusing this patch, I'll get a patch out
once I figure out why kvm/queue is broken.

diff --git a/tools/testing/selftests/kvm/x86_64/state_test.c b/tools/testing/selftests/kvm/x86_64/state_test.c
index 2e0a92da8ff5..b7faeae3dcc4 100644
--- a/tools/testing/selftests/kvm/x86_64/state_test.c
+++ b/tools/testing/selftests/kvm/x86_64/state_test.c
@@ -210,6 +210,12 @@ int main(int argc, char *argv[])
memset(&regs1, 0, sizeof(regs1));
vcpu_regs_get(vm, VCPU_ID, &regs1);

+ if (stage == 6) {
+ state->events.flags |= 0x20;
+ vcpu_events_set(vm, VCPU_ID, &state->events);
+ vcpu_nested_state_set(vm, VCPU_ID, &state->nested, false);
+ }
+
kvm_vm_release(vm);

/* Restore state in a new VM. */

Also verified the WARN with this. Then, is it still necessary to add an individual selftest about the working flow of save/restore triple fault event?