[SRU][J][PATCH 1/1] KVM: x86: Use __try_cmpxchg_user() to update guest PTE A/D bits

Cengiz Can cengiz.can at canonical.com
Wed Sep 9 13:57:17 UTC 2026


From: Sean Christopherson <seanjc at google.com>

Use the recently introduced __try_cmpxchg_user() to update guest PTE A/D
bits instead of mapping the PTE into kernel address space.  The VM_PFNMAP
path is broken as it assumes that vm_pgoff is the base pfn of the mapped
VMA range, which is conceptually wrong as vm_pgoff is the offset relative
to the file and has nothing to do with the pfn.  The horrific hack worked
for the original use case (backing guest memory with /dev/mem), but leads
to accessing "random" pfns for pretty much any other VM_PFNMAP case.

Fixes: bd53cb35a3e9 ("X86/KVM: Handle PFNs outside of kernel reach when touching GPTEs")
Debugged-by: Tadeusz Struk <tadeusz.struk at linaro.org>
Tested-by: Tadeusz Struk <tadeusz.struk at linaro.org>
Reported-by: syzbot+6cde2282daa792c49ab8 at syzkaller.appspotmail.com
Cc: stable at vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc at google.com>
Message-Id: <20220202004945.2540433-4-seanjc at google.com>
Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
(backported from commit f122dfe4476890d60b8c679128cd2259ec96a24c)
[bot_kybele: Removed this tree's variant of FNAME(cmpxchg_gpte) (uses
 _ASM_EXTABLE_UA / int r) which differed from upstream's version, resolving the
 conflict by deleting it entirely; the call-site __try_cmpxchg_user hunk applied
 cleanly and the macro already exists in this tree.]
CVE-2022-49562
Assisted-by: kybele:claude-opus-4.8
Signed-off-by: Cengiz Can <cengiz.can at canonical.com>
---
 arch/x86/kvm/mmu/paging_tmpl.h | 41 +---------------------------------
 1 file changed, 1 insertion(+), 40 deletions(-)

diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index cacba0bdc5fb..08fb78870716 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -144,45 +144,6 @@ static bool FNAME(is_rsvd_bits_set)(struct kvm_mmu *mmu, u64 gpte, int level)
 	       FNAME(is_bad_mt_xwr)(&mmu->guest_rsvd_check, gpte);
 }
 
-static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
-			       pt_element_t __user *ptep_user, unsigned index,
-			       pt_element_t orig_pte, pt_element_t new_pte)
-{
-	int r = -EFAULT;
-
-	if (!user_access_begin(ptep_user, sizeof(pt_element_t)))
-		return -EFAULT;
-
-#ifdef CMPXCHG
-	asm volatile("1:" LOCK_PREFIX CMPXCHG " %[new], %[ptr]\n"
-		     "mov $0, %[r]\n"
-		     "setnz %b[r]\n"
-		     "2:"
-		     _ASM_EXTABLE_UA(1b, 2b)
-		     : [ptr] "+m" (*ptep_user),
-		       [old] "+a" (orig_pte),
-		       [r] "+q" (r)
-		     : [new] "r" (new_pte)
-		     : "memory");
-#else
-	asm volatile("1:" LOCK_PREFIX "cmpxchg8b %[ptr]\n"
-		     "movl $0, %[r]\n"
-		     "jz 2f\n"
-		     "incl %[r]\n"
-		     "2:"
-		     _ASM_EXTABLE_UA(1b, 2b)
-		     : [ptr] "+m" (*ptep_user),
-		       [old] "+A" (orig_pte),
-		       [r] "+rm" (r)
-		     : [new_lo] "b" ((u32)new_pte),
-		       [new_hi] "c" ((u32)(new_pte >> 32))
-		     : "memory");
-#endif
-
-	user_access_end();
-	return r;
-}
-
 static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
 				  struct kvm_mmu_page *sp, u64 *spte,
 				  u64 gpte)
@@ -281,7 +242,7 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,
 		if (unlikely(!walker->pte_writable[level - 1]))
 			continue;
 
-		ret = FNAME(cmpxchg_gpte)(vcpu, mmu, ptep_user, index, orig_pte, pte);
+		ret = __try_cmpxchg_user(ptep_user, &orig_pte, pte, fault);
 		if (ret)
 			return ret;
 
-- 
2.53.0




More information about the kernel-team mailing list