From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796423041; b=xCCc0SzS3VhJSZY/c36fV+DPGU9YryHgHm9pX14dWYrVvWA8OExPCU5DQ+bOFbMXvxKS iFzk8KRwoeJnYYEasxemFqCNK/0n3Spp+T9IM/hIBWu5W6nZPpW5YeaMXHZciSZwhb23P Q3icVfjGHtPQJdr8Xuo7+9Rj3kWeipRQGMb3u2EXvVCI7pNEgspI+1AtHuAEpsvQx/hp2 e1dZ6zFuwiEAj6si0TP/Kl+WuwgGTBR5jBH/WStQaejGfp8tUJJCINQ69fW++e64Y1UT+ hta+pvjdb0K7KKRWxE0YKio1LBB8hruCbZyTwF6hRkM5QQLj0U93tZCMmUzYPAv/bIA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796423041; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=07fVaHHTSaX4a17OiCGDdGUAZbkGxMYox0rTtWEMspI=; b=FRGXE5d9cnn7W4cOTou18mlKKHmIQ6+saEaCiRgcQXXkys3X0FH8TzMDhS4DtzK2vBUG JogXpt9EiDzvQuX4mtDuvpmZ9tDv/XATyIpgnaCxtVkBpWJyqwZfZh9l0QWwXqFtIJYAq Pyohsb4xs266q+LSCgzAeumXlIBQdldCb+i4By+fcjsb9kpdaOl0FVjWpm16TVQRwe6Rb xHj+w6tAcelbfZi6JPoQMH5ITKnUbjHDzIBOZO38MtXq+iHjuVLGM03bF1grrbnp6pDK9 M7YKLbkJh3O8f1gd+U45HzFItPK8XnpjUaDb0ELh1Xj7qLBbe0FF1Bx2k9m+pqNj/Pw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SKhZhttI; arc=none smtp.remote-ip=74.125.82.180 Received: from mail-dy1-f180.google.com (mail-dy1-f180.google.com [74.125.82.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3580606A6BD for ; Fri, 4 Dec 2026 22:24:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D3580606A6BD Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SKhZhttI Received: by mail-dy1-f180.google.com with SMTP id 5a478bee46e88-386c71feeeaso1879202eec.0 for ; Fri, 04 Dec 2026 14:24:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796423040; x=1797027840; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=07fVaHHTSaX4a17OiCGDdGUAZbkGxMYox0rTtWEMspI=; b=SKhZhttIsEZQul4NO1SAai6mAg/E32Fn+NRUyKPIvO9I76RyBV4J1L5vUQ2vVI9cPC NsG/OwY2cxdO3no2k2uuFZu0Zw6fIQpTfoKyhPmlvmWBtn8FAdEcNZI8RTjS9XWrnxb8 4tEVUS0pDxfgtroe1f14Ex49M8geFKk3+pf01Vv4zeA5J7MNJMa646irgXKpHBg1hY5V YBshVPK4/Z5Oq8s+aJuztpI2NGWTrkzAiQyxFO3Sn0QzD1OfXHzNN9Ld8UZ/WxLGByvM 6+FrfXoRQ5U7MTLE4iXzM8xqLmkCZsfPLr6st99K1NjYJkBjxq3vXaOoB90xvc6Quwew cR5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796423040; x=1797027840; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=07fVaHHTSaX4a17OiCGDdGUAZbkGxMYox0rTtWEMspI=; b=A27OvwlUBi5NVWsxPfDNcAtX8UZwh6s4sfEcYCV9jieCYAAjd9Ry+iqSqd80AeoHzL Q3MjC5ijhOWiormHWB8mOqvsALrYNtwbFnRwg+7SGTANjB2h5oyXTqyoGcfGzSe8DIkg rxJmqllg/zk4uRdnnWymQNYqeQe/NQklMXrEDmXJ86ZKQO08ebqLPyHTMb/MI+8CxzcQ jTDBlwKM6NFiN3ouQtKHOk8cD/VppbxDf/eB4kiqZmf+oDCUUjXYZikiAcXpvRC0OgOF ofjbYIBVFh6s6vT4KTW4ma430mU8hV/jYUB6FBrVIEOOSAgNFysF0bmpyk0ZHRiJnIms AUKQ== X-Forwarded-Encrypted: i=1; AL+hErEg7elx7205K5Ff7076Zwg9m9JA27Il/mIqtqd7fcK1BxnKjtHIDxnAJMZfKDfyoVxHU8d5JgNxNBluGQ0=@vger.kernel.org X-Gm-Message-State: AB4q+uEyO6FhMZeuTpZFTdEXbbYmeODDBhSUaoxoRinUQrBStXezTB2z dww9Brfyu3vHj3h8w+tCyuRe4Qz9DnWQ9w25h1cPdbH6RRVXYG5K0b5/ X-Gm-Gg: AZFdTZfNnfhpBEfcVjbedyu8Zka+IKLqTg0WrwPUAUXetv6CaoGkM4TORJIOICgFupW z+HGMZH/1ThFfEW8uvzor8xCmcekz25tnbbKKnCmYvEWhQiinBr+JkwpHHry8Af+JBRYHZ/Md6r SjEFFTn2UpBM/lpqH9k4BQ0bWrzlD9iePKXoi1RV3udjMtGK8Wk+vMFjBtheer12wxfgd3nbD5j JyJhAb5zbU855XVvTpK2XqcfDWj1iV01RPMTSYwAwCGb6GyRpMm8AFIwXdHW1b6HpC6utS6weI3 mVYxV3KWgugnAEQiCQhBxNoO4ZRFSY1q8ll3VoIQ//1qcN8GmOnsC5xamqFregXLdJPsS3lF4Q8 kB5zK1LU1FxMvHDUDKXkC2/NKg4ExaI/0f0zylgo5lLB/kjJPDO5rHYEcExC3rYR/921MHUF2L4 Tk0IDySLzEl4LDQO/BfpfM9RT96CnmrzX75gvhmcX/wBCT2oV57MXtE6T+g6L0yZKQt1UPNhayl 19idWFQpLAr79SEolM4iLLosUul4z++vlHX6pbLxJGQj7vzbAascdZM2quY1M5Th1XlZ6DOZ1dQ nsLWZzO8npy8ug/5jCYwC/eNwfpiluV02OMsaUNDZ/wEtbhHDWnudl9WoNCHJWNtBdVH X-Received: by 2002:a05:7301:180c:b0:386:cffd:bda6 with SMTP id 5a478bee46e88-387520190a6mr3186824eec.5.1796423039755; Fri, 04 Dec 2026 14:23:59 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521e5626sm10075368eec.17.2026.12.04.14.23.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 14:23:59 -0800 (PST) From: Praneeth Balasubramanian To: Sean Christopherson , Paolo Bonzini Cc: David Hildenbrand , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Praneeth Balasubramanian , 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 Message-Id: <20261204222352.1869330-1-praneethb2002@gmail.com> X-Mailer: git-send-email 2.25.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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