Skip to contentSkip to conversation

KVM: nVMX: Fix a (benign) overwrite to a userspace buffer

PATCH In review Sean Christopherson 2 participants 5 messages Oct 22 – Nov 13

Subsystems (4)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: nVMX: Fix a (benign) overwrite to a userspace buffer

Fix a bug where KVM writes more bytes for KVM_GET_NESTED_STATE than it reports in the output size if L1 is active and has a valid shadow VMCS pointer. The bug is all but guaranteed to be benign in practice, as all VMMs I could find that support nested virtualization just preallocate the max buffer size.

Sean Christopherson (2):
KVM: nVMX: Save shadow VMCS when getting nested state iff L2 is active
KVM: selftests: Add regression test for KVM writing but not reporting

    vmcs12
 arch/x86/kvm/vmx/nested.c                     |  2 +-
 .../testing/selftests/kvm/lib/x86/processor.c |  6 +++++
 tools/testing/selftests/kvm/x86/state_test.c  | 24 ++++++++++---------
 3 files changed, 20 insertions(+), 12 deletions(-)

base-commit: 164405e8e9a0378c7e1aff2254625a6553a291f1

Signature · 1 line
2.56.0.218.g1ec904b173-goog
author maintainer
#1 lore raw

PATCH 1/2 KVM: nVMX: Save shadow VMCS when getting nested state iff L2 is active

When getting nested state, copy the shadow VMCS to the userspace buffer if and only if the vCPU is in L2, as that is the only scenario where the shadow VMCS is "active" and thus needs to be explicitly save/restored via KVM_GET_NESTED_STATE. If the vCPU is in L1, then the shadow VMCS isn't active/cached by KVM, i.e. is just a pointer to memory, and doesn't need to be saved/restored (userspace is responsible for saving/restore guest memory as needed).

This fixes a bug where KVM would save the shadow VMCS, but not account the size of the shadow VMCS, as the accounting correctly checks for L2 being active. Note, the restore path also consumes the shadow VMCS payload if and only if L2 is active.

In practice, writing more bytes than are reported is likely benign, as KVM correctly reports the maximum size for KVM_CAP_NESTED_STATE, and no known VMM invokes KVM_GET_NESTED_STATE twice on every save to precisely allocate its buffer, e.g. QEMU and Google's VMM both allocate the maximum size when the vCPU is first created/allocated.

Fixes fa58a9fa7497 ("KVM: nVMX: include shadow vmcs12 in nested state")
Cc Chris Mason <[email protected]>
---
 arch/x86/kvm/vmx/nested.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +1 -1
index 9b0bfa2f854c..c2586bc9ebb5 100644
@@ -6875,7 +6875,7 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
 	if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
 		return -EFAULT;
 
-	if (nested_cpu_has_shadow_vmcs(vmcs12) &&
+	if (is_guest_mode(vcpu) && nested_cpu_has_shadow_vmcs(vmcs12) &&
 	    vmcs12->vmcs_link_pointer != INVALID_GPA) {
 		if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
 				 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
Signature · 1 line
2.56.0.218.g1ec904b173-goog
author maintainer
#2 lore raw

PATCH 2/2 KVM: selftests: Add regression test for KVM writing but not reporting vmcs12

In reply to Sean Christopherson

When saving nested vCPU state, verify that KVM didn't write any more bytes than it reported, and add a sync+save point in the state test to trigger a save when L1 is active with a valid shadow VMCS. Before commit <TBD>, KVM would incorrectly save the shadow VMCS into the userspace buffer without accounting for the size of the shadow VMCS.

---
 .../testing/selftests/kvm/lib/x86/processor.c |  6 +++++
 tools/testing/selftests/kvm/x86/state_test.c  | 24 ++++++++++---------
 2 files changed, 19 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/lib/x86/processor.c +6 -0
index 8374afd08baf..976d53af6de7 100644
@@ -1282,10 +1282,16 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu)
 	if (nested_size) {
 		state->nested.size = sizeof(state->nested_);
 
+		memset(state->nested_, 0xaa, sizeof(state->nested_));
+
 		vcpu_nested_state_get(vcpu, &state->nested);
 		TEST_ASSERT(state->nested.size <= nested_size,
 			    "Nested state size too big, %i (KVM_CHECK_CAP gave %i)",
 			    state->nested.size, nested_size);
+
+		for (i = state->nested.size; i < nested_size; i++)
+			TEST_ASSERT((unsigned char)state->nested_[i] == 0xaa,
+				    "KVM killed the canary at offset %u", i);
 	} else {
 		state->nested.size = 0;
 	}
tools/testing/selftests/kvm/x86/state_test.c +13 -11
index 500b09762555..5bd9a04a3911 100644
@@ -60,13 +60,13 @@ void vmx_l2_guest_code(void)
 
 	/* L1 has now set up a shadow VMCS for us.  */
 	GUEST_ASSERT(vmread(GUEST_RIP) == 0xc0ffee);
-	GUEST_SYNC(10);
-	GUEST_ASSERT(vmread(GUEST_RIP) == 0xc0ffee);
-	vmwrite(GUEST_RIP, 0xc0fffee);
 	GUEST_SYNC(11);
+	GUEST_ASSERT(vmread(GUEST_RIP) == 0xc0ffee);
+	vmwrite(GUEST_RIP, 0xc0fffee);
+	GUEST_SYNC(12);
 	GUEST_ASSERT(vmread(GUEST_RIP) == 0xc0fffee);
 	vmwrite(GUEST_RIP, 0xc0ffffee);
-	GUEST_SYNC(12);
+	GUEST_SYNC(13);
 
 	/* Done, exit to L1 and never come back.  */
 	vmcall();
@@ -107,15 +107,17 @@ static void vmx_l1_guest_code(struct vmx_pages *vmx_pages)
 
 	vmwrite(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
 	vmwrite(VMCS_LINK_POINTER, vmx_pages->shadow_vmcs_gpa);
-
-	vmptrld(vmx_pages->shadow_vmcs_gpa);
-	GUEST_ASSERT(__vmlaunch());
 	GUEST_SYNC(8);
-	GUEST_ASSERT(__vmlaunch());
-	GUEST_ASSERT(__vmresume());
 
-	vmwrite(GUEST_RIP, 0xc0ffee);
+	vmptrld(vmx_pages->shadow_vmcs_gpa);
+	GUEST_ASSERT(__vmlaunch());
 	GUEST_SYNC(9);
+
+	GUEST_ASSERT(__vmlaunch());
+	GUEST_ASSERT(__vmresume());
+
+	vmwrite(GUEST_RIP, 0xc0ffee);
+	GUEST_SYNC(10);
 	GUEST_ASSERT(vmread(GUEST_RIP) == 0xc0ffee);
 
 	vmptrld(vmx_pages->vmcs_gpa);
@@ -126,7 +128,7 @@ static void vmx_l1_guest_code(struct vmx_pages *vmx_pages)
 	GUEST_ASSERT(vmread(GUEST_RIP) == 0xc0ffffee);
 	GUEST_ASSERT(__vmlaunch());
 	GUEST_ASSERT(__vmresume());
-	GUEST_SYNC(13);
+	GUEST_SYNC(14);
 	GUEST_ASSERT(vmread(GUEST_RIP) == 0xc0ffffee);
 	GUEST_ASSERT(__vmlaunch());
 	GUEST_ASSERT(__vmresume());
Signature · 1 line
2.56.0.218.g1ec904b173-goog
#3 lore raw

Re: [PATCH 2/2] KVM: selftests: Add regression test for KVM writing but not reporting vmcs12

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] KVM: selftests: memset() in vcpu_save_state() overwrites state->nested.size --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM: selftests: memset() in vcpu_save_state() overwrites state->nested.size --

commit 352912c420268741f0078843ad13ef7b51d43d5c
Author: Sean Christopherson <[email protected]>

KVM: selftests: Add regression test for KVM writing but not reporting vmcs12

This commit adds a canary check in vcpu_save_state() to verify that saving nested vCPU state does not write beyond the reported nested state size, and adds a guest sync point in state_test when L1 is active with a shadow vmcs.

tools/testing/selftests/kvm/lib/x86/processor.c struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu) · line 1287

Quoted 11 lines
tools/testing/selftests/kvm/lib/x86/processor.c +2 -0
index 8374afd08bafe..976d53af6de7d 100644
@@ -1282,10 +1282,16 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu)
 	if (nested_size) {
 		state->nested.size = sizeof(state->nested_);
 
+		memset(state->nested_, 0xaa, sizeof(state->nested_));
+
 		vcpu_nested_state_get(vcpu, &state->nested);

[Severity: Medium]
Does this memset() overwrite state->nested.size before calling vcpu_nested_state_get()?

In struct kvm_x86_state (tools/testing/selftests/kvm/include/x86/processor.h), nested and nested_ share an anonymous union:

	union {
		struct kvm_nested_state nested;
		char nested_[16384];
	};

Calling memset(state->nested_, 0xaa, sizeof(state->nested_)) after setting state->nested.size = sizeof(state->nested_) clobbers state->nested.size with 0xaaaaaaaa before vcpu_nested_state_get(vcpu, &state->nested) is called. Should the memset() be placed before setting state->nested.size?

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/processor.c +4 -0
 		TEST_ASSERT(state->nested.size <= nested_size,
 			    "Nested state size too big, %i (KVM_CHECK_CAP gave %i)",
 			    state->nested.size, nested_size);
+
+		for (i = state->nested.size; i < nested_size; i++)
+			TEST_ASSERT((unsigned char)state->nested_[i] == 0xaa,
+				    "KVM killed the canary at offset %u", i);
 	} else {
 		state->nested.size = 0;
 	}

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#4 lore raw
In reply to Sean Christopherson
Quoted 11 lines · Sean Christopherson

Fix a bug where KVM writes more bytes for KVM_GET_NESTED_STATE than it reports in the output size if L1 is active and has a valid shadow VMCS pointer. The bug is all but guaranteed to be benign in practice, as all VMMs I could find that support nested virtualization just preallocate the max buffer size.

Sean Christopherson (2):
KVM: nVMX: Save shadow VMCS when getting nested state iff L2 is active
KVM: selftests: Add regression test for KVM writing but not reporting

    vmcs12

[...]

Applied patch 1 to kvm-x86 vmx, I'll follow-up on the selftest.

[1/2] KVM: nVMX: Save shadow VMCS when getting nested state iff L2 is active

      https://github.com/kvm-x86/linux/commit/0333e967fb05

--
https://github.com/kvm-x86/linux/tree/next