* [PATCH v2] KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
@ 2026-08-26 16:56 Sean Christopherson
2026-08-26 17:11 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2026-08-26 16:56 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: David Hildenbrand, kvm, linux-kernel, Yan Zhao, Vishal Annapurve
Add more context and information to the comment in kvm_gmem_release() that
explains why there's no synchronization on RCU _or_ kvm->srcu. Point (b)
from commit 67b43038ce14 ("KVM: guest_memfd: Remove RCU-protected attribute
from slot->gmem.file")
b) kvm->srcu ensures that kvm_gmem_unbind() and freeing of a memslot
occur after the memslot is no longer visible to kvm_gmem_get_pfn().
is especially difficult to fully grok, particularly in light of commit
ae431059e75d ("KVM: guest_memfd: Remove bindings on memslot deletion when
gmem is dying"), which addressed a race between unbind() and release().
See the extended on-list discussion[*] for more details about exactly what
KVM guards against, and how.
No functional change intended.
Link: https://lore.kernel.org/all/CAEvNRgGmyd1yqQXsnz5hWRZpBZUs%3DpiEWbEaqP9%2Bcz9ZqEMQ6g@mail.gmail.com [*]
Cc: Yan Zhao <yan.y.zhao@intel.com>
Cc: Vishal Annapurve <vannapurve@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
v2: Explain how this all works in even gorier detail. [Yan]
v1: https://lore.kernel.org/all/20251113232229.1698886-1-seanjc@google.com
virt/kvm/guest_memfd.c | 50 +++++++++++++++++++++++++++++++++++++-----
1 file changed, 44 insertions(+), 6 deletions(-)
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index b596486d184c..7f1c6a0f8039 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -300,17 +300,55 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)
* dereferencing the slot for existing bindings needs to be protected
* against memslot updates, specifically so that unbind doesn't race
* and free the memslot (kvm_gmem_get_file() will return NULL).
- *
- * Since .release is called only when the reference count is zero,
- * after which file_ref_get() and get_file_active() fail,
- * kvm_gmem_get_pfn() cannot be using the file concurrently.
- * file_ref_put() provides a full barrier, and get_file_active() the
- * matching acquire barrier.
*/
mutex_lock(&kvm->slots_lock);
filemap_invalidate_lock(inode->i_mapping);
+ /*
+ * Note! synchronize_srcu() is _not_ needed after nullifying memslot
+ * bindings as slot->gmem.file cannot be set back to a non-null value
+ * without the memslot first being deleted. I.e. this relies on the
+ * synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure
+ * kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't
+ * grab a reference to slot->gmem.file even if the struct file object
+ * is reallocated.
+ *
+ * file_ref_put() provides a full barrier, and __get_file_rcu() the
+ * matching acquire barrier, to ensure that kvm_gmem_get_file() (via
+ * __get_file_rcu()) sees refcount==0 or fails the "file reloaded"
+ * check (file != NULL due to nullifying the file pointer here).
+ *
+ * Unlike most other users of get_file_rcu(), where callers don't care
+ * if they race with a write, only that they have a reference to _a_
+ * live file, kvm_gmem_get_pfn() needs to get the exact file that is
+ * associated with the memslot. Without the aforementioned SRCU
+ * synchronization, the following could happen:
+ *
+ * CPU0 CPU1
+ * kvm_gmem_get_pfn()
+ * f = X (from slot->gmem.file)
+ * kvm_gmem_release())
+ * slot->gmem.file = NULL
+ *
+ * kvm_set_memory_region()
+ * slot deleted
+ *
+ * kvm_set_memory_region()
+ * slot created
+ * slot->gmem.file = f (alloc the same object)
+ *
+ * get_file_active()
+ * file = f
+ * file_reloaded = f
+ *
+ * <KVM does weird things with an old memslot+file>
+ *
+ * Obviously KVM would be broken in many places if the synchronization
+ * were omitted, but it's important to note that get_file_active() does
+ * NOT guarantee a reference to the correct file was obtained, only
+ * that the file doesn't point at a reallocated object.
+ */
xa_for_each(&f->bindings, index, slot)
WRITE_ONCE(slot->gmem.file, NULL);
base-commit: 76671054f9a1ff6abb976583cd8da37650acdc97
--
2.55.0.887.g758fc8c411-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
2026-08-26 16:56 [PATCH v2] KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF Sean Christopherson
@ 2026-08-26 17:11 ` sashiko-bot
2026-08-26 18:39 ` Sean Christopherson
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-08-26 17:11 UTC (permalink / raw)
To: Sean Christopherson; +Cc: kvm
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Bypassing filemap_invalidate_lock in kvm_gmem_unbind when the file is dying allows concurrent xarray modifications, causing Use-After-Free and leaked mmu_invalidate_in_progress.
--
commit 22b749f62017c828a40c33364eefe3474406907f
Author: Sean Christopherson <seanjc@google.com>
KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
This commit updates the comment in kvm_gmem_release() to elaborate on the
synchronization between release() and get_pfn(). It explains why neither
RCU nor kvm->srcu synchronization is necessary when nullifying memslot
bindings, detailing the barriers used to prevent a use-after-free.
Link: https://lore.kernel.org/all/CAEvNRgGmyd1yqQXsnz5hWRZpBZUs%3DpiEWbEaqP9%2Bcz9ZqEMQ6g@mail.gmail.com [*]
> diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> index b596486d184ca..7f1c6a0f8039d 100644
> --- a/virt/kvm/guest_memfd.c
> +++ b/virt/kvm/guest_memfd.c
[ ... ]
> @@ -300,17 +300,55 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)
> * dereferencing the slot for existing bindings needs to be protected
> * against memslot updates, specifically so that unbind doesn't race
> * and free the memslot (kvm_gmem_get_file() will return NULL).
> */
> mutex_lock(&kvm->slots_lock);
>
> filemap_invalidate_lock(inode->i_mapping);
>
> + /*
> + * Note! synchronize_srcu() is _not_ needed after nullifying memslot
[ ... ]
> + * Obviously KVM would be broken in many places if the synchronization
> + * were omitted, but it's important to note that get_file_active() does
> + * NOT guarantee a reference to the correct file was obtained, only
> + * that the file doesn't point at a reallocated object.
> + */
> xa_for_each(&f->bindings, index, slot)
> WRITE_ONCE(slot->gmem.file, NULL);
>
[Severity: Critical]
This is a pre-existing issue, but could bypassing filemap_invalidate_lock in
kvm_gmem_unbind() when the file is dying allow concurrent xarray modifications
that result in a use-after-free?
In kvm_gmem_unbind(), when the file is dying:
if (!file) {
__kvm_gmem_unbind(slot, slot->gmem.file->private_data);
return;
}
Because kvm_gmem_error_folio() handles memory failure on guest_memfd pages by
holding filemap_invalidate_lock_shared(mapping) and iterates f->bindings
without holding kvm->srcu or kvm->slots_lock, it relies entirely on
filemap_invalidate_lock.
If kvm_gmem_unbind() bypasses filemap_invalidate_lock when the underlying
file is dying, it can run concurrently with kvm_gmem_error_folio() and
remove a slot from f->bindings. This allows kvm_set_memory_region() to free
the memslot while kvm_gmem_error_folio() is still holding a pointer to it.
Could this cause a use-after-free during the iteration:
virt/kvm/guest_memfd.c:__kvm_gmem_invalidate_start() {
xa_for_each_range(&f->bindings, index, slot, start, end - 1) {
pgoff_t pgoff = slot->gmem.pgoff;
...
}
Additionally, if the slot is removed before __kvm_gmem_invalidate_end(),
xa_find() returns false, which skips kvm_mmu_invalidate_end() and
permanently leaks mmu_invalidate_in_progress:
virt/kvm/guest_memfd.c:__kvm_gmem_invalidate_end() {
if (xa_find(&f->bindings, &start, end - 1, XA_PRESENT)) {
KVM_MMU_LOCK(kvm);
kvm_mmu_invalidate_end(kvm);
KVM_MMU_UNLOCK(kvm);
}
}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260826165647.769231-1-seanjc@google.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
2026-08-26 17:11 ` sashiko-bot
@ 2026-08-26 18:39 ` Sean Christopherson
0 siblings, 0 replies; 3+ messages in thread
From: Sean Christopherson @ 2026-08-26 18:39 UTC (permalink / raw)
To: sashiko-reviews; +Cc: kvm
On Wed, Aug 26, 2026, sashiko-bot@kernel.org wrote:
> > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> > index b596486d184ca..7f1c6a0f8039d 100644
> > --- a/virt/kvm/guest_memfd.c
> > +++ b/virt/kvm/guest_memfd.c
> [ ... ]
> > @@ -300,17 +300,55 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)
> > * dereferencing the slot for existing bindings needs to be protected
> > * against memslot updates, specifically so that unbind doesn't race
> > * and free the memslot (kvm_gmem_get_file() will return NULL).
> > */
> > mutex_lock(&kvm->slots_lock);
> >
> > filemap_invalidate_lock(inode->i_mapping);
> >
> > + /*
> > + * Note! synchronize_srcu() is _not_ needed after nullifying memslot
> [ ... ]
> > + * Obviously KVM would be broken in many places if the synchronization
> > + * were omitted, but it's important to note that get_file_active() does
> > + * NOT guarantee a reference to the correct file was obtained, only
> > + * that the file doesn't point at a reallocated object.
> > + */
> > xa_for_each(&f->bindings, index, slot)
> > WRITE_ONCE(slot->gmem.file, NULL);
> >
>
> [Severity: Critical]
> This is a pre-existing issue, but could bypassing filemap_invalidate_lock in
> kvm_gmem_unbind() when the file is dying allow concurrent xarray modifications
> that result in a use-after-free?
Yep, already reported by Sashiko and being fixed:
https://lore.kernel.org/all/20260823-shivank-gmem-fix-split-v1-1-512a29fb8e86@amd.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-26 18:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-26 16:56 [PATCH v2] KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF Sean Christopherson
2026-08-26 17:11 ` sashiko-bot
2026-08-26 18:39 ` Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox