* [PATCH] KVM: guest_memfd: Hold the inode lock when calling file_modified()
@ 2026-12-04 22:23 Praneeth Balasubramanian
0 siblings, 0 replies; only message in thread
From: Praneeth Balasubramanian @ 2026-12-04 22:23 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini
Cc: David Hildenbrand, kvm, linux-kernel, Praneeth Balasubramanian,
stable
kvm_gmem_fallocate() calls file_modified() after a successful
allocation or hole punch, but it does not hold the inode lock.
file_modified() says that the caller must hold the file's inode lock,
and shmem_fallocate() does. The lock serializes the ctime and mtime
updates. inode_update_cmtime() reads the clock, stores ctime and then
stores mtime, all with plain stores.
Two fallocate() calls on the same guest_memfd can therefore interleave
their updates. If the call that read the clock first stores last, the
timestamps go back to a time before a fallocate() that has already
returned, and ctime and mtime can end up different.
Take the inode lock around file_modified() only. The mapping's
invalidate lock already serializes the page cache changes, so
allocations still run in parallel, and the new lock is never held
together with another guest_memfd lock.
The missing lock was found by a runtime check that turns the locking
rule in a function's comment into an assertion, while fuzzing with
syzkaller.
Fixes: a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---
Notes:
This was found by a runtime check, part of research at UCSB SecLab. The
locking rule in the kernel-doc comment of file_modified_flags() ("Caller
must hold the file's inode lock") was turned into an assertion at the
function's entry, and syzkaller reached it through fallocate() on a
guest_memfd. An LLM was used to turn the comment into the assertion.
The patch and changelog were drafted with Claude Opus 5.5. I reviewed
the patch and take responsibility for it.
Testing on next-20261204, x86_64, in QEMU with nested KVM and
PROVE_LOCKING. Two test-only changes were made in fs/inode.c. One is a
lockdep check in file_modified_flags() for guest_memfd inodes. The other
is an optional 1000 ms sleep between the ctime and mtime stores in
inode_update_cmtime().
- Without the patch, the check warns on the first fallocate() of a
guest_memfd. With the patch, it stays quiet.
- With the sleep, two hole punches 100 ms apart left mtime older than
the mtime seen after the second punch returned, and older than ctime,
in 5 of 5 runs. With the patch, the second punch waited for the first
one and the timestamps stayed in order, in 5 of 5 runs. Without the
sleep I did not see the race. The window is a few stores long.
- 8 threads mixed allocate, punch hole, fstat(), and page touches plus
MADV_REMOVE on an mmap()ed guest_memfd for 2 x 20 s, about 1.2M
calls. No call failed and lockdep reported nothing.
- Builds without warnings with W=1 (x86_64 allmodconfig, clang 23).
virt/kvm/guest_memfd.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 3e02acf88939..327e167e3d1a 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -330,8 +330,11 @@ static long kvm_gmem_fallocate(struct file *file, int mode, loff_t offset,
else
ret = kvm_gmem_allocate(file_inode(file), offset, len);
- if (!ret)
+ if (!ret) {
+ inode_lock(file_inode(file));
file_modified(file);
+ inode_unlock(file_inode(file));
+ }
return ret;
}
--
2.25.1
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-04 22:24 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 22:23 [PATCH] KVM: guest_memfd: Hold the inode lock when calling file_modified() Praneeth Balasubramanian
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox