From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796423041; b=nNQNF9xggvME8VVQvW1SUmZmQpRd0o2gSxi/8Asb7V59wWS2kwV2ehmSTvDghMcFRtxM ESsofiEwG7IyFYr5ZNBzGlWa44Vbv4Zw9pOpljuBwtZf8BXJY6bXhSqjI54Xg7q/TVjr3 IHL0MS5TbjlaPUTtK/sxy6NIoEpLDoHvcP8bpp1if/XWoDCjtK3o8wqQgEtdqV+KqbkhY WaDTQiGVHFfq7+KnJMFbFIEzKXwJl7zuGr8nRRyNbaf0wWoYzsnw5tFQT98v00RbH/5JU TyY3fBZDO7FnYCAzVuFxolUICm00L0JkbjhZs2G/udHfjjaQuGUDL8KVc1ODYPpmcnw== 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=S7uGpkkIsXseSmrqbk/qT7PZ78Wv0vtrAogTHuEPCKAqjqMyVEFjqmMPxqakwlY+2H5P /ERZZstsGDdg8cne8E0frejUx6XhxZnF2RLNdiDEMRl9YsejvmDgrAbveQ7q3sz7a5Fdi 7uCGZGiLa9AVkABcjQYqVUQaBg80PDtEu9nRKr+/3V5XsZspTGzK/X1mVciDErrdtGqCj vUY3daF33ghSmoMOhH3HzmXg2jl9DujwcGN/hOv0CIr5jU45tDHAxW8Zy9W3tbfN6T0dT y1MBz6BiUfjfrxlHEeF+kLquyA7TXb1bmIxhR2ngEu78lM2kEPuBZU5oNn/vmrHgbkA== 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.181 Received: from mail-dy1-f181.google.com (mail-dy1-f181.google.com [74.125.82.181]) (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 D25DE606A6BC for ; Fri, 4 Dec 2026 22:24:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D25DE606A6BC 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-f181.google.com with SMTP id 5a478bee46e88-386c71feeeaso1879203eec.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=KaEqrMpE6J4xgofVKV38Llgn+ldTf0ps5aqOqQfiGFh8lQzYFDvtRNNJ/rgxErY6nl FEelhnsm4SZBK4tZ1cCXBLH5lZ4tAMExYy3ogQQQVRifAfOF/TYKjj1IWTSrVCLEp70C ajkmT+jTS0rrxO2dpQgfyxxLCwu4gUK5iz5vPyw4SQFm9aLVX0wxwBtmeW+eKT+S9NQt B6OI9rKjNMcZapn5I4+tPJNSYiy7PSXdRmZXeOP8qwBXtl66NgNpfKat0jWHjUHDalHO IlVnJJLi74xLPyO4h7kz93mjOMojQqDhtDefGup77JDLyVIVGyrTdQNcJ8wAZtaBq3Un yUFQ== X-Forwarded-Encrypted: i=1; AL+hErEAcCbYy07RtPYSbeI6oXgkmn42+QXWf0YgLESUL2Y33oUPdl74el+gljr69AozyoOKUfI=@vger.kernel.org X-Gm-Message-State: AB4q+uEMBPgkWQJVDJj9HBZsShpJbClqBmCEYuIDO6ZA3xyTvIxnvy0n EHIFmzhlZFEwnU/KsU/04367WtryhwrihIspxvn2yDKAwtmJBGsJKPuT X-Gm-Gg: AZFdTZdCeYlMNiSF1pER3X9URGM7R5d+nQ39s8OVzViQtasG1nP2LmQQfDs4xdV1Wx5 KT3Yz7ekOR0jSqUl24QWN/+FOsOxdBRfLNxu8Qkiz2v13FB2f5m8YkbwU7nXBF3REQ55wvTm/yk myuX5EK8TwpdDMWC5iAlUxoBAzV0S1jxYqbvDJk7sShk4hEmji/zYeK2TQ3R9PiwkRFqCewWv+f iVlkjYTHLxvKYpUMgzbwP0abMXGDsuqUOlVuXyNvjbRsTfW6lCcSVlS+SKpiLDETgK03NUUYyAn KNar6ernTZH4DikTvcCwXB3naNSnggxP6zND9FB9Asu9GgQRMDicejSZtYkOvsAJlCghbnxHizK X5WE8TFpft/NLXtzIxWObDtnfoGf8pPYlFRlzySRqy1Heq6qHHryd4P7gQ+kQEiLY5YtQmJ98d1 9JjJtoN/C4l/W/E2R0xfL3b45A4qxMr2/sHRlR1i7Macw4MdeBpJokMH5ekMCSjXwQ9khxWrh6S 6yDKjVXEZB3qdVZLS80yo4kacmWMZTAZcmqX74dlj0TszAIWDfuBEoZmejdgk9eeV+WLuNQtf4G tvbolvUojDwhdl+TDLyJ51r/Mv/3yf1lY+w4/C0GFv/YcqwPTDhKD5CHw4n0rfXhovki 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: kvm@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