Skip to contentSkip to conversation

KVM: pfncache: Fix uhva validity check in kvm_gpc_is_valid_len()

PATCH In review [email protected] 2 participants 4 messages Mar 9 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: pfncache: Fix uhva validity check in kvm_gpc_is_valid_len()

From: Nguyen Dinh Phi <[email protected]>

In kvm_gpc_is_valid_len(), if the GPA is an error GPA, the function uses uhva to calculate the page offset. However, if uhva is invalid, its value can still be page-aligned (for example, PAGE_OFFSET) and this function will still return true.

An invalid uhva could lead to incorrect offset calculations and potentially trigger a WARN_ON_ONCE in __kvm_gpc_refresh().

Fixing it by adding an additional check for uhva.

---
 virt/kvm/pfncache.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
virt/kvm/pfncache.c +10 -2
index 728d2c1b488a..707ead0a096c 100644
@@ -60,8 +60,16 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start,
 static bool kvm_gpc_is_valid_len(gpa_t gpa, unsigned long uhva,
 				 unsigned long len)
 {
-	unsigned long offset = kvm_is_error_gpa(gpa) ? offset_in_page(uhva) :
-						       offset_in_page(gpa);
+	unsigned long offset;
+
+	if (kvm_is_error_gpa(gpa)) {
+		if (kvm_is_error_hva(uhva))
+			return false;
+
+		offset = offset_in_page(uhva);
+	} else {
+		offset = offset_in_page(gpa);
+	}
 
 	/*
 	 * The cached access must fit within a single page. The 'len' argument
Signature · 1 line
2.43.0
#1 lore raw
Quoted 6 lines · On Mon, Mar 09, 2026, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

In kvm_gpc_is_valid_len(), if the GPA is an error GPA, the function uses uhva to calculate the page offset. However, if uhva is invalid, its value can still be page-aligned (for example, PAGE_OFFSET) and this function will still return true.

The HVA really shouldn't be invalid in the first place. Ideally, Xen code wouldn't call kvm_gpc_refresh() on an inactive cache, but I suspect we'd end up with TOCTOU flaws even if we tried to add checks.

The next best thing would be to explicitly check if the gpc is active. That should preserve the WARN if KVM tries to pass in a garbage address to __kvm_gpc_activate().

virt/kvm/pfncache.c +3 -0
index 728d2c1b488a..8372d1712471 100644
@@ -369,6 +369,9 @@ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len)
 
        guard(mutex)(&gpc->refresh_lock);
 
+       if (!gpc->active)
+               return -EINVAL;
+
        if (!kvm_gpc_is_valid_len(gpc->gpa, gpc->uhva, len))
                return -EINVAL;
author
#2 lore raw
Quoted 20 lines · Sean Christopherson
Quoted 6 lines · On Mon, Mar 09, 2026, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

In kvm_gpc_is_valid_len(), if the GPA is an error GPA, the function uses uhva to calculate the page offset. However, if uhva is invalid, its value can still be page-aligned (for example, PAGE_OFFSET) and this function will still return true.

The HVA really shouldn't be invalid in the first place. Ideally, Xen code wouldn't call kvm_gpc_refresh() on an inactive cache, but I suspect we'd end up with TOCTOU flaws even if we tried to add checks.

The next best thing would be to explicitly check if the gpc is active. That should preserve the WARN if KVM tries to pass in a garbage address to __kvm_gpc_activate().

virt/kvm/pfncache.c +0 -0
index 728d2c1b488a..8372d1712471 100644

virt/kvm/pfncache.c int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len) · line 376

Quoted 8 lines
virt/kvm/pfncache.c +3 -0
@@ -369,6 +369,9 @@ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len)
 
         guard(mutex)(&gpc->refresh_lock);
 
+       if (!gpc->active)
+               return -EINVAL;
+
         if (!kvm_gpc_is_valid_len(gpc->gpa, gpc->uhva, len))
                 return -EINVAL;

In this reproducer, userspace invokes KVM_XEN_HVM_EVTCHN_SEND without first configuring the cache. As a result, kvm_xen_set_evtchn_fast() returns -EWOULDBLOCK when kvm_gpc_check() fails. The -EWOULDBLOCK error then causes kvm_xen_set_evtchn() to fall back to calling kvm_gpc_refresh().

IMO, if the cache is not active, kvm_xen_set_evtchn_fast() should return -EINVAL instead. It may be better to check the active state of the GPC in kvm_xen_set_evtchn_fast() rather than kvm_gpc_refresh()? Br,
Phi

#3 lore raw
Quoted 32 lines · Phi Nguyen
Quoted 20 lines · Sean Christopherson
Quoted 6 lines · On Mon, Mar 09, 2026, [email protected] wrote:

From: Nguyen Dinh Phi <[email protected]>

In kvm_gpc_is_valid_len(), if the GPA is an error GPA, the function uses uhva to calculate the page offset. However, if uhva is invalid, its value can still be page-aligned (for example, PAGE_OFFSET) and this function will still return true.

The HVA really shouldn't be invalid in the first place. Ideally, Xen code wouldn't call kvm_gpc_refresh() on an inactive cache, but I suspect we'd end up with TOCTOU flaws even if we tried to add checks.

The next best thing would be to explicitly check if the gpc is active. That should preserve the WARN if KVM tries to pass in a garbage address to __kvm_gpc_activate().

virt/kvm/pfncache.c +0 -0
index 728d2c1b488a..8372d1712471 100644

virt/kvm/pfncache.c int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len) · line 374

Quoted 6 lines
virt/kvm/pfncache.c +3 -0
@@ -369,6 +369,9 @@ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len)
         guard(mutex)(&gpc->refresh_lock);
+       if (!gpc->active)
+               return -EINVAL;
+
         if (!kvm_gpc_is_valid_len(gpc->gpa, gpc->uhva, len))
                 return -EINVAL;

In this reproducer, userspace invokes KVM_XEN_HVM_EVTCHN_SEND without first configuring the cache. As a result, kvm_xen_set_evtchn_fast() returns -EWOULDBLOCK when kvm_gpc_check() fails. The -EWOULDBLOCK error then causes kvm_xen_set_evtchn() to fall back to calling kvm_gpc_refresh().

Quoted 3 lines

IMO, if the cache is not active, kvm_xen_set_evtchn_fast() should return -EINVAL instead. It may be better to check the active state of the GPC in kvm_xen_set_evtchn_fast() rather than kvm_gpc_refresh()?

That'd be subject to the TOCTOU race I mentioned. gpc->active is guarded by gpc->refresh_lock, which as the name suggests is taken only by __kvm_gpc_activate(), kvm_gpc_deactivate(), and kvm_gpc_refresh(). Checking gpc->active outside of those paths can get false positives, e.g. in this case if there's a racing call to deactivate a cache via KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA.

So no matter what, kvm_gpc_refresh() needs to check gpc->active. At that point, I don't see any value in having callers check, because they can't be trusted to do the right thing, and even worse might give a false sense of security.