From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796423041; b=NUfRisRFIl+uAsAVFkoUS6ezW5O1J4Jfubobsa50011mYhB44E6bram1WcNcdHGpIazi /vlQ/uq4PbFxaojjnvGN/4PBfHEewb7H6kfjU67qFPNMYj6ARnkTfcMInG4YSeTD3/hfY aWOdNR6B6KSucqqPalt3dcwTVRg4TEffr1j/dTs5iq4cqlJPNDrRT8abN+PxgOrHVsq6+ 3u0oTBNFXp98UrxBY/qXjMykwsarz70zrAOKDL5GMuKQCVy73flZgZOFcL2uMnHxeMPqm gDw0PbPaJ/bBHCuhQi7fFXrfuQ3VEUXNMkZL+OBzJmK7g4eS/YD+uVQErr45P4C/Azg== 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=sf3WDC6aoZXIjoPGsADoPv1aoQJuEzS+E+71F5nScgFj9hhwfHGboLxOgA6sNGvldffj 61bSz0hKdcEhp67yYJQOscmGWDPzBKS3KTkaRjhVRi4Lf8PdYffYdpBJSHvx6cQ9lI70q 2wbxCGRdK5N5VNj8WRFAGAa7J4QlEaGsBV3Bu+6bAu+jMm+681i0fitoOlMj/A1mCtn1H pmv7uNsYv9q0Xw1odnbNi88twhxvsUCgk7/W3dnh4O4NGzwi8nUzaI7zaLClhAJi+bFyv coDq/8zLvZ2beZY77twqtnUh9hmNip+TQJYDXtyxhcnjbzgBDH11tIBP8/HLoYY6DSQ== 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.182 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (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 D6D06606A6BE for ; Fri, 4 Dec 2026 22:24:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D6D06606A6BE 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-f182.google.com with SMTP id 5a478bee46e88-38431138772so2102863eec.1 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=hQu2E0Si84CxSgs4zoeBW0rfbJgG/hgtg8D8L/scjgl/Sm1+gajfu++dAkKqQMBZ5O PKMeE/4+6tUJhPRSVWUn/9AryTSFJnqB5clIBLWeeSiE99WIjfhZaZOnbSmNugmIP5k1 Pjil/CTN06UTeeGXaL9UIYMF21v2vkppav6gJkgZP6yO/kpTFNn0dfzsCZ+CP1BnLiF4 FGEXbMOwNyvZKAPxhByh+3lsZXDdBN1hAa/lOYQSQPSsx2oCYgDMPaw4XxFc75ojxKkZ vtpMb5FyU0NkWAbNavYnJGx3as8NcAbtSOhdFE3Fm2SuxdgpEhQJdPgrsc7RFVKG+GJv nU2w== X-Forwarded-Encrypted: i=1; AL+hErHKVHJaRLSb8B09RUOZgywC7ziEBTbPOAcpeU/PMuBYsTdolTpYbT2V+/v/pwXKqWISw+ya138=@vger.kernel.org X-Gm-Message-State: AB4q+uFaBt0AXqvUYqE6Rbpg6AfdUJ9aJ9Ksoh7i+nf8pwlA69WOc5Gd 1ZISogbZHWQ+KD0b+UQ0oc4Nb9knsHrwgQ6HoZrruygEZBYeYnb4a9DwJJTekQd8 X-Gm-Gg: AZFdTZcSzudTzctlr9DXtoZ7qj4FAhAlg6qFKGJteBmXNlYg4m1PKqJVly7AY5FcAhN C0ecAwxMRNuz11xREit864kWOIM/IgzRVE3m11St5UAdYRLZ3evmdPMNmypYhUV/EW+E2s3mIdE 1vCjzBgr4+3C+bmgSXPFhHvn4Bdt57XzLL0syJN5Bev/DAKxRkVsilkjFD/CtrZsEtuFGVwlPyx MEmAk1Zy1bwWvcRvg6ERFbaquKIgeyjLp1AcUHRu2GTkFdy9LvYCEmrVlqX1vzazFf4qAjRVs/4 BMlJa0gsUq0oToUPp7BtHuCgcz1gfIUDu2omSTMmN+ugqs8UnW6+k/dCSdamwWAcTLWi6tZWV7l qe7cRO+7p0NB58gC2b5sICvyXbJhMfmY1FufajjVDPIyDEBxahftlzt2zgl/R+4YzHG+Xi+aCMT r63PvxWOd1PGIGr6rk/6T/vbjHeZSjSgRjDp/asYngtMLwhg0glo4gL4X2YWT+x0PCi9nNvkOyr onUAHhbJGQfejs5tER8CV5ZVZj6CimK1wo1Gu1w4EClUjUidh9pGMJlWKy2JFWNC99wGHEccjTM 8QJpuRcURVtutufZvyzVruLIlvnLRDUXSqX8YTlS+NC0XihwVtiJJcAG6l/fcKCwloqY 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: stable@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