Skip to contentSkip to conversation

KVM: x86/xen: Read long_mode only once in kvm_xen_set_evtchn_fast()

PATCH In review Hyunwoo Kim 3 participants 5 messages Jun 4

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: x86/xen: Read long_mode only once in kvm_xen_set_evtchn_fast()

kvm_xen_set_evtchn_fast() assumes the port range check in max_evtchn_port() and the bitmap layout selection observe the same long_mode, but each reads kvm->arch.xen.long_mode separately. If the guest changes long_mode in between, a port accepted by the 64-bit range check is handled with the 32-bit layout, and port_word_bit falls outside evtchn_pending_sel.

Read long_mode once on entry so the range check and both layout computations use the same value. Since max_evtchn_port() reads long_mode as well, compute the bound directly from the latched value.

Fixes 14243b387137 ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and event channel delivery")
---
 arch/x86/kvm/xen.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/xen.c +7 -3
index 91fd3673c09a..4d8337abf0fa 100644
@@ -1798,6 +1798,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
 	int port_word_bit;
 	bool kick_vcpu = false;
 	int vcpu_idx, idx, rc;
+	bool long_mode;
 
 	vcpu_idx = READ_ONCE(xe->vcpu_idx);
 	if (vcpu_idx >= 0)
@@ -1809,7 +1810,10 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
 		WRITE_ONCE(xe->vcpu_idx, vcpu->vcpu_idx);
 	}
 
-	if (xe->port >= max_evtchn_port(kvm))
+	long_mode = IS_ENABLED(CONFIG_64BIT) && READ_ONCE(kvm->arch.xen.long_mode);
+
+	if (xe->port >= (long_mode ? EVTCHN_2L_NR_CHANNELS :
+				     COMPAT_EVTCHN_2L_NR_CHANNELS))
 		return -EINVAL;
 
 	rc = -EWOULDBLOCK;
@@ -1820,7 +1824,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
 	if (!kvm_gpc_check(gpc, PAGE_SIZE))
 		goto out_rcu;
 
-	if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
+	if (long_mode) {
 		struct shared_info *shinfo = gpc->khva;
 		pending_bits = (unsigned long *)&shinfo->evtchn_pending;
 		mask_bits = (unsigned long *)&shinfo->evtchn_mask;
@@ -1861,7 +1865,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
 			goto out_rcu;
 		}
 
-		if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
+		if (long_mode) {
 			struct vcpu_info *vcpu_info = gpc->khva;
 			if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) {
 				WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Guest can trigger a host split-lock panic by placing vcpu_info such that evtchn_pending_sel crosses a cache line boundary. --

commit a112a8243664e8390567ef2c91d59d503bd5858b
Author: Hyunwoo Kim <[email protected]>

KVM: x86/xen: Read long_mode only once in kvm_xen_set_evtchn_fast()

This commit ensures long_mode is read exactly once in kvm_xen_set_evtchn_fast() to prevent a race condition where a guest might change long_mode between the port range check and bitmap layout selection. By latching the value, it guarantees both use the same layout.

Quoted 4 lines
arch/x86/kvm/xen.c +0 -0
index d6b2a665b499d..32af7731c59bd 100644

[ ... ]

arch/x86/kvm/xen.c int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm) · line 1871

Quoted 8 lines
arch/x86/kvm/xen.c +1 -1
@@ -1862,7 +1866,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
 			goto out_rcu;
 		}
 
-		if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
+		if (long_mode) {
 			struct vcpu_info *vcpu_info = gpc->khva;
 			if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) {

[Severity: High]
This is a pre-existing issue, but can this atomic operation trigger a host split-lock panic?

If a malicious guest aligns the GPA of vcpu_info such that the 8-byte evtchn_pending_sel crosses a 64-byte cache line boundary, this test_and_set_bit() will execute an unaligned locked instruction. On hosts with split_lock_detect=fatal enabled, could this result in a guest-to-host denial of service?

arch/x86/kvm/xen.c

Quoted 8 lines
arch/x86/kvm/xen.c +0 -0
 				WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1);
arch/x86/kvm/xen.c +1 -1
 				kick_vcpu = true;
 			}
 		} else {
 			struct compat_vcpu_info *vcpu_info = gpc->khva;
 			if (!test_and_set_bit(port_word_bit,
-					      (unsigned long *)&vcpu_info->evtchn_pending_sel)) {
+					      (unsigned long *)&vcpu_info->evtchn_pending_sel)) {

[Severity: High]
This is also a pre-existing issue, but does this cast in compat mode create the exact same split-lock vector?

The compat_vcpu_info struct uses a 4-byte evtchn_pending_sel, but casting it to an unsigned long pointer and using test_and_set_bit() forces an 8-byte atomic access. If the guest strategically places this struct so the field crosses a cache line boundary, will this similarly crash a hypervisor enforcing strict split-lock detection?

Quoted 3 lines
arch/x86/kvm/xen.c +0 -0
 				WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1);
 				kick_vcpu = true;
 			}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/aiHPPUk5DY7rH-zL@v4bel?part=1
maintainer
#2 lore raw
In reply to Hyunwoo Kim
Quoted 6 lines · Hyunwoo Kim

kvm_xen_set_evtchn_fast() assumes the port range check in max_evtchn_port() and the bitmap layout selection observe the same long_mode, but each reads kvm->arch.xen.long_mode separately. If the guest changes long_mode in between, a port accepted by the 64-bit range check is handled with the 32-bit layout, and port_word_bit falls outside evtchn_pending_sel.

Quoted 6 lines

Read long_mode once on entry so the range check and both layout computations use the same value. Since max_evtchn_port() reads long_mode as well, compute the bound directly from the latched value.

Fixes 14243b387137 ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and event channel delivery")

Thanks. I think we have the same issue in kvm_xen_schedop_poll() too, don't we? Just slightly harder to see because the bitmap access is done in wait_pending_event().

Quoted 9 lines
arch/x86/kvm/xen.c +0 -0
@@ -1809,7 +1810,10 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
  		WRITE_ONCE(xe->vcpu_idx, vcpu->vcpu_idx);
  	}

arch/x86/kvm/xen.c +4 -1
-	if (xe->port >= max_evtchn_port(kvm))
+	long_mode = IS_ENABLED(CONFIG_64BIT) && READ_ONCE(kvm->arch.xen.long_mode);
+
+	if (xe->port >= (long_mode ? EVTCHN_2L_NR_CHANNELS :
+				     COMPAT_EVTCHN_2L_NR_CHANNELS))
Quoted 3 lines

  return -EINVAL;

  rc = -EWOULDBLOCK;

Rather than open-coding that, I wonder if we should make max_evtchn_port() take "bool long_mode" as an argument instead of kvm? Those callers which only need a single instantaneous answer can just pass it kvm->arch.xen.long_mode instead?

We'll want to pass a 'bool long_mode' down to wait_pending_events() too.

To be clear: these are not security issues because we'll only ever scribble *within* the vcpu_info or shared_info pages, even if the wrong places therein.

I'm actually more concerned about what Sashiko saw. Xen does enforce alignment, so we should do so too. I'll take a look...

#3 lore raw

Thanks for the review.

Quoted 11 lines · David Woodhouse
Quoted 10 lines · Hyunwoo Kim

kvm_xen_set_evtchn_fast() assumes the port range check in max_evtchn_port() and the bitmap layout selection observe the same long_mode, but each reads kvm->arch.xen.long_mode separately. If the guest changes long_mode in between, a port accepted by the 64-bit range check is handled with the 32-bit layout, and port_word_bit falls outside evtchn_pending_sel.

Read long_mode once on entry so the range check and both layout computations use the same value. Since max_evtchn_port() reads long_mode as well, compute the bound directly from the latched value.

Quoted 6 lines
Quoted 2 lines
Fixes 14243b387137 ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and event channel delivery")

Thanks. I think we have the same issue in kvm_xen_schedop_poll() too, don't we? Just slightly harder to see because the bitmap access is done in wait_pending_event().

Yes, kvm_xen_schedop_poll() needs the same fix.

Quoted 19 lines
Quoted 10 lines
arch/x86/kvm/xen.c +0 -0
@@ -1809,7 +1810,10 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
  		WRITE_ONCE(xe->vcpu_idx, vcpu->vcpu_idx);
  	}

arch/x86/kvm/xen.c +4 -1
-	if (xe->port >= max_evtchn_port(kvm))
+	long_mode = IS_ENABLED(CONFIG_64BIT) && READ_ONCE(kvm->arch.xen.long_mode);
+
+	if (xe->port >= (long_mode ? EVTCHN_2L_NR_CHANNELS :
+				     COMPAT_EVTCHN_2L_NR_CHANNELS))
Quoted 3 lines

  return -EINVAL;

  rc = -EWOULDBLOCK;

Rather than open-coding that, I wonder if we should make max_evtchn_port() take "bool long_mode" as an argument instead of kvm? Those callers which only need a single instantaneous answer can just pass it kvm->arch.xen.long_mode instead?

Quoted 2 lines

We'll want to pass a 'bool long_mode' down to wait_pending_events() too.

Makes sense. Shall I send that as v2?

Quoted 7 lines

To be clear: these are not security issues because we'll only ever scribble *within* the vcpu_info or shared_info pages, even if the wrong places therein.

I'm actually more concerned about what Sashiko saw. Xen does enforce alignment, so we should do so too. I'll take a look...

In addition to what Sashiko flagged, kvm_xen_inject_pending_events() looks worth checking too.

Best regards,
Hyunwoo Kim

maintainer
#4 lore raw
Quoted 42 lines · Hyunwoo Kim

Thanks for the review.

Quoted 11 lines · David Woodhouse
Quoted 10 lines · Hyunwoo Kim

kvm_xen_set_evtchn_fast() assumes the port range check in max_evtchn_port() and the bitmap layout selection observe the same long_mode, but each reads kvm->arch.xen.long_mode separately. If the guest changes long_mode in between, a port accepted by the 64-bit range check is handled with the 32-bit layout, and port_word_bit falls outside evtchn_pending_sel.

Read long_mode once on entry so the range check and both layout computations use the same value. Since max_evtchn_port() reads long_mode as well, compute the bound directly from the latched value.

Quoted 6 lines
Quoted 2 lines
Fixes 14243b387137 ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and event channel delivery")

Thanks. I think we have the same issue in kvm_xen_schedop_poll() too, don't we? Just slightly harder to see because the bitmap access is done in wait_pending_event().

Yes, kvm_xen_schedop_poll() needs the same fix.

Quoted 19 lines
Quoted 10 lines
arch/x86/kvm/xen.c +0 -0
@@ -1809,7 +1810,10 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
  		WRITE_ONCE(xe->vcpu_idx, vcpu->vcpu_idx);
  	}

arch/x86/kvm/xen.c +4 -1
-	if (xe->port >= max_evtchn_port(kvm))
+	long_mode = IS_ENABLED(CONFIG_64BIT) && READ_ONCE(kvm->arch.xen.long_mode);
+
+	if (xe->port >= (long_mode ? EVTCHN_2L_NR_CHANNELS :
+				     COMPAT_EVTCHN_2L_NR_CHANNELS))
Quoted 3 lines

  return -EINVAL;

  rc = -EWOULDBLOCK;

Rather than open-coding that, I wonder if we should make max_evtchn_port() take "bool long_mode" as an argument instead of kvm? Those callers which only need a single instantaneous answer can just pass it kvm->arch.xen.long_mode instead?

Quoted 5 lines
Quoted 2 lines

We'll want to pass a 'bool long_mode' down to wait_pending_events() too.

Makes sense. Shall I send that as v2?

I'm working on it. WIP at
https://git.infradead.org/?p=users/dwmw2/linux.git;a=shortlog;h=refs/heads/xen

David Woodhouse (4):
KVM: x86/xen: Rename 'longmode' to 'is_64bit' in hypercall handling
KVM: x86/xen: Introduce kvm_xen_is_long_mode() macro
KVM: x86/xen: Make max_evtchn_port() take a bool long_mode argument
KVM: x86/xen: Fix TOCTOU race in kvm_xen_schedop_poll()

Hyunwoo Kim (1):
KVM: x86/xen: Read long_mode only once in kvm_xen_set_evtchn_fast()

Quoted 10 lines
Quoted 7 lines

To be clear: these are not security issues because we'll only ever scribble *within* the vcpu_info or shared_info pages, even if the wrong places therein.

I'm actually more concerned about what Sashiko saw. Xen does enforce alignment, so we should do so too. I'll take a look...

In addition to what Sashiko flagged, kvm_xen_inject_pending_events() looks worth checking too.

Yeah, working on the alignment thing next and comparing with what Xen actually does.

Thanks.