The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: David Hildenbrand <david@kernel.org>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Praneeth Balasubramanian <praneethb2002@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] KVM: guest_memfd: Hold the inode lock when calling file_modified()
Date: Fri,  4 Dec 2026 14:23:52 -0800	[thread overview]
Message-ID: <20261204222352.1869330-1-praneethb2002@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-04 22:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204222352.1869330-1-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=david@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox