KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
Subsystems (2)
Sean Christopherson
maintainerPATCH v2 1/1 KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
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.
---
v2: Explain how this all works in even gorier detail. [Yan]
v1: https://lore.kernel.org/all/[email protected]
virt/kvm/guest_memfd.c | 50 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-)
virt/kvm/guest_memfd.c +44 -6
@@ -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
Signature · 1 line
2.55.0.887.g758fc8c411-goog
[email protected]
AI reviewThank 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:
- [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 <[email protected]>
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.
Quoted 4 lines
virt/kvm/guest_memfd.c +0
-0
[ ... ]
Quoted 11 lines
virt/kvm/guest_memfd.c +2
-0@@ -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
[ ... ]
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 353
Quoted 8 lines
virt/kvm/guest_memfd.c +5
-0+* 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);
}
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
maintainerQuoted 17 lines · On Wed, Aug 26, 2026, [email protected] wrote:
Quoted 4 lines
virt/kvm/guest_memfd.c +0
-0[ ... ]
Quoted 11 lines
virt/kvm/guest_memfd.c +2
-0@@ -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 [ ... ]
Quoted 13 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 353
Quoted 8 lines
virt/kvm/guest_memfd.c +5
-0+* 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 bypassingfilemap_invalidate_lockinkvm_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/[email protected]
Quoted 47 lines · Sean Christopherson
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 commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_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 betweenunbind()andrelease().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%[email protected] [*] open hereCc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>---
v2: Explain how this all works in even gorier detail. [Yan]
v1: https://lore.kernel.org/all/[email protected]
virt/kvm/guest_memfd.c | 50 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -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);
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 315
Quoted 10 lines
virt/kvm/guest_memfd.c +8
-0filemap_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.
Nit:
Do we need to update the code comment in __kvm_gmem_unbind() from
/* * synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn() * cannot see this memslot. */ to /* * synchronize_srcu_expedited() in kvm_swap_active_memslots() ensured * that kvm_gmem_get_pfn() cannot see this memslot. */
to align with the above comment.
Quoted 25 lines
virt/kvm/guest_memfd.c +25
-0+* +* 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) +*
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 350
Quoted 10 lines
virt/kvm/guest_memfd.c +10
-0+* 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.
reallocated -> reloaded?
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 353
Quoted 3 lines
virt/kvm/guest_memfd.c +1
-0+*/ xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL );
With the nits addressed,
Sean Christopherson
maintainerQuoted 72 lines · Yan Zhao
Quoted 47 lines · Sean Christopherson
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 commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_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 betweenunbind()andrelease().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%[email protected] [*] open hereCc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>---
v2: Explain how this all works in even gorier detail. [Yan]
v1: https://lore.kernel.org/all/[email protected]
virt/kvm/guest_memfd.c | 50 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -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);virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 315
Quoted 10 lines
virt/kvm/guest_memfd.c +8
-0filemap_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. Nit:
Do we need to update the code comment in
__kvm_gmem_unbind()from/* * synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn() * cannot see this memslot. */ to /* * synchronize_srcu_expedited() in kvm_swap_active_memslots() ensured * that kvm_gmem_get_pfn() cannot see this memslot. */
Quoted 1 line
to align with the above comment.
How about this? Because the "rule" is that kvm_gmem_unbind() can only be called on a memslot that is unreachable, either by synchronizing SRCU after uninstalling the memslot *or* because the memslot was never installed. Simply stating that synchronize_srcu_expedited() makes everything safe isn't the whole story, as it's specifically synchronzing after removing/deleting/deactivating the slot that makes this safe.
/* * Note, the caller is responsible for ensuring the slot is unreachable * before unbinding, e.g. by synchronizing SRCU after deleting the slot. */
Quoted 28 lines
Quoted 28 lines
virt/kvm/guest_memfd.c +28
-0+* +* 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
Quoted 8 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 350
Quoted 7 lines
virt/kvm/guest_memfd.c +7
-0+* +* <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. reallocated -> reloaded?
No, "reallocated" is correct. From the comment for SLAB_TYPESAFE_BY_RCU:
* This delays freeing the SLAB page by a grace period, it does _NOT_
* delay object freeing. This means that if you do kmem_cache_free()
* that memory location is free to be reused at any time. Thus it may
* be possible to see another object there in the same RCU grace period. *
* This feature only ensures the memory location backing the object
* stays valid, the trick to using this is relying on an independent
* object validation pass. Something like:
*
* ::
*
* begin:
* rcu_read_lock();
* obj = lockless_lookup(key);
* if (obj) {
* if (!try_get_ref(obj)) // might fail for free objects
* rcu_read_unlock();
* goto begin;
*
* if (obj->key != key) { // not the object we expected
* put_ref(obj);
* rcu_read_unlock();
* goto begin; * }
* }
* rcu_read_unlock();
The above pseudocode is what get_file_active() is doing; it verifies the found file ("obj" above) is the correct file. Specifically, from __get_file_rcu():
* If the pointers don't match the file has been reallocated by
* SLAB_TYPESAFE_BY_RCU.
Sean Christopherson
maintainerQuoted 22 lines · Sean Christopherson
Quoted 12 lines · Yan Zhao
On Wed, Aug 26, 2026 at 09:56:47AM -0700, Sean Christopherson wrote:
Do we need to update the code comment in__kvm_gmem_unbind()from/* * synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn() * cannot see this memslot. */ to /* * synchronize_srcu_expedited() in kvm_swap_active_memslots() ensured * that kvm_gmem_get_pfn() cannot see this memslot. */Quoted 1 line
to align with the above comment.
How about this? Because the "rule" is that
kvm_gmem_unbind()can only be called on a memslot that is unreachable, either by synchronizing SRCU after uninstalling the memslot *or* because the memslot was never installed. Simply stating thatsynchronize_srcu_expedited()makes everything safe isn't the whole story, as it's specifically synchronzing after removing/deleting/deactivating the slot that makes this safe.
Quoted 4 lines
/* * Note, the caller is responsible for ensuring the slot is unreachable * before unbinding, e.g. by synchronizing SRCU after deleting the slot. */
Hmm, though it's probably a good idea to preserve the connection to kvm_gmem_get_pfn():
/*
* Note, the caller is responsible for ensuring the slot is unreachable
* before unbinding, e.g. by synchronizing SRCU after deleting the slot,
* to guarantee kvm_gmem_get_pfn() can't see the slot+file. */
Quoted 7 lines · Sean Christopherson
Quoted 7 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 315
Quoted 7 lines
virt/kvm/guest_memfd.c +7
-0+* 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.
Does "reallocated" here refer to the case (*) below?
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
virt/kvm/guest_memfd.c +15
-0+* 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())
Quoted 8 lines
Quoted 8 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 339
Quoted 8 lines
virt/kvm/guest_memfd.c +8
-0+* slot->gmem.file = NULL +* +* kvm_set_memory_region() +* slot deleted +* +* kvm_set_memory_region() +* slot created +* slot->gmem.file = f (alloc the same object)
Case (*):
I thought "reallocated" refers to this case, which get_file_active() cannot guarantee against.
Quoted 42 lines
Quoted 4 lines
Quoted 4 lines
virt/kvm/guest_memfd.c +4
-0+* +* get_file_active() +* file = f +* file_reloaded = f Quoted 8 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 350
Quoted 7 lines
virt/kvm/guest_memfd.c +7
-0+* +* <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. reallocated -> reloaded?
No, "reallocated" is correct. From the comment for
SLAB_TYPESAFE_BY_RCU:* This delays freeing the SLAB page by a grace period, it does _NOT_
* delay object freeing. This means that if you dokmem_cache_free()
* that memory location is free to be reused at any time. Thus it may
* be possible to see another object there in the same RCU grace period. *
* This feature only ensures the memory location backing the object
* stays valid, the trick to using this is relying on an independent
* object validation pass. Something like:
*
* ::
*
* begin:* rcu_read_lock(); * obj = lockless_lookup(key); * if (obj) { * if (!try_get_ref(obj)) // might fail for free objects * rcu_read_unlock(); * goto begin; * * if (obj->key != key) { // not the object we expected * put_ref(obj); * rcu_read_unlock(); * goto begin;* }
* }
*rcu_read_unlock();
Quoted 5 lines
The above pseudocode is what
get_file_active()is doing; it verifies the found file ("obj" above) is the correct file. Specifically, from__get_file_rcu():* If the pointers don't match the file has been reallocated by
*SLAB_TYPESAFE_BY_RCU.
Yeah, I understand this is what you mean by "reallocated" :) However, I think case (*) above could also be considered a form of "reallocated". Would it make sense to rename "reallocated" to "reassigned" in case (*)?
Quoted 28 lines · Sean Christopherson
Quoted 22 lines · Sean Christopherson
Quoted 12 lines · Yan Zhao
On Wed, Aug 26, 2026 at 09:56:47AM -0700, Sean Christopherson wrote:
Do we need to update the code comment in__kvm_gmem_unbind()from/* * synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn() * cannot see this memslot. */ to /* * synchronize_srcu_expedited() in kvm_swap_active_memslots() ensured * that kvm_gmem_get_pfn() cannot see this memslot. */Quoted 1 line
to align with the above comment.
How about this? Because the "rule" is that
kvm_gmem_unbind()can only be called on a memslot that is unreachable, either by synchronizing SRCU after uninstalling the memslot *or* because the memslot was never installed. Simply stating thatsynchronize_srcu_expedited()makes everything safe isn't the whole story, as it's specifically synchronzing after removing/deleting/deactivating the slot that makes this safe.Quoted 4 lines
/* * Note, the caller is responsible for ensuring the slot is unreachable * before unbinding, e.g. by synchronizing SRCU after deleting the slot. */
Quoted 7 lines
Hmm, though it's probably a good idea to preserve the connection to
kvm_gmem_get_pfn():/*
* Note, the caller is responsible for ensuring the slot is unreachable
* before unbinding, e.g. by synchronizing SRCU after deleting the slot,
* to guaranteekvm_gmem_get_pfn()can't see the slot+file. */
LGTM.
Sean Christopherson
maintainerQuoted 9 lines · Yan Zhao
Quoted 7 lines · Sean Christopherson
Quoted 7 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * 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.Does "reallocated" here refer to the case (*) below?
Yes. I'll clarify this with:
* is reallocated (for use as a different file).
Quoted 25 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
(quoted diff) +15
-0+ * 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())Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ * slot->gmem.file = NULL + * + * kvm_set_memory_region() + * slot deleted + * + * kvm_set_memory_region() + * slot created + * slot->gmem.file = f (alloc the same object)Case (*):
Quoted 2 lines
I thought "reallocated" refers to this case, which
get_file_active()cannot guarantee against.
Ya.
Quoted 42 lines
Quoted 42 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4
-0+ * + * get_file_active() + * file = f + * file_reloaded = fQuoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * + * <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.reallocated -> reloaded?
No, "reallocated" is correct. From the comment for
SLAB_TYPESAFE_BY_RCU:* This delays freeing the SLAB page by a grace period, it does _NOT_
* delay object freeing. This means that if you dokmem_cache_free()
* that memory location is free to be reused at any time. Thus it may
* be possible to see another object there in the same RCU grace period. *
* This feature only ensures the memory location backing the object
* stays valid, the trick to using this is relying on an independent
* object validation pass. Something like:
*
* ::
*
* begin:* rcu_read_lock(); * obj = lockless_lookup(key); * if (obj) { * if (!try_get_ref(obj)) // might fail for free objects * rcu_read_unlock(); * goto begin; * * if (obj->key != key) { // not the object we expected * put_ref(obj); * rcu_read_unlock(); * goto begin;* }
* }
*rcu_read_unlock();
Quoted 8 lines
Quoted 5 lines
The above pseudocode is what
get_file_active()is doing; it verifies the found file ("obj" above) is the correct file. Specifically, from__get_file_rcu():* If the pointers don't match the file has been reallocated by
*SLAB_TYPESAFE_BY_RCU.Yeah, I understand this is what you mean by "reallocated" :) However, I think case (*) above could also be considered a form of "reallocated". Would it make sense to rename "reallocated" to "reassigned" in case (*)?
Oooh, I see what you're getting at. Yes, I agree, "reallocated" isn't the best description here. Hmm, but neither is "reloaded" or "reassigned", because it doesn't fully guard against the pointer being reassigned, e.g. if the pointer is reassigned before the the first load.
How about this?
* 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 "original" file was obtained, only
* that it grabbed a reference for the returned file, i.e. didn't grab
* a reference for file A, but then returned a pointer to file B.
And the whole thing as a diff:
virt/kvm/guest_memfd.c +8 -7
@@ -312,7 +312,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* 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. +* is reallocated (for use as a different file). * * file_ref_put() provides a full barrier, and __get_file_rcu() the * matching acquire barrier, to ensure that kvm_gmem_get_file() (via
@@ -327,7 +327,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* * CPU0 CPU1 * kvm_gmem_get_pfn() -* f = X (from slot->gmem.file) +* slot->gmem.file == A * kvm_gmem_release()) * slot->gmem.file = NULL *
@@ -336,18 +336,19 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* * kvm_set_memory_region() * slot created -* slot->gmem.file = f (alloc the same object) +* slot->gmem.file = B (alloc the same object) * * get_file_active() -* file = f -* file_reloaded = f +* file = B +* file_reloaded = B * * <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. +* NOT guarantee a reference to the "original" file was obtained, only +* that it grabbed a reference for the returned file, i.e. didn't grab +* a reference for file A, but then returned a pointer to file B. */ xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL );
Quoted 1 line · Sean Christopherson
And the whole thing as a diff:
LGTM.
Quoted 47 lines
virt/kvm/guest_memfd.c +8
-7@@ -312,7 +312,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* 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. +* is reallocated (for use as a different file). * * file_ref_put() provides a full barrier, and __get_file_rcu() the * matching acquire barrier, to ensure that kvm_gmem_get_file() (via @@ -327,7 +327,7 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* * CPU0 CPU1 * kvm_gmem_get_pfn() -* f = X (from slot->gmem.file) +* slot->gmem.file == A * kvm_gmem_release()) * slot->gmem.file = NULL * @@ -336,18 +336,19 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* * kvm_set_memory_region() * slot created -* slot->gmem.file = f (alloc the same object) +* slot->gmem.file = B (alloc the same object) * * get_file_active() -* file = f -* file_reloaded = f +* file = B +* file_reloaded = B * * <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. +* NOT guarantee a reference to the "original" file was obtained, only +* that it grabbed a reference for the returned file, i.e. didn't grab +* a reference for file A, but then returned a pointer to file B. */ xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL );
Quoted 9 lines · Sean Christopherson
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 commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().[...]
Applied to kvm-x86 coco. I'll follow-up with another cleanup for get_pfn() at some point. Thanks!
[1/1] KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
https://github.com/kvm-x86/linux/commit/53c34396c19e