From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796309710; b=F6itsAsUz78jwpGBsDQafVMqrXHRhwMPb6F4HkrzAm/J/cHvhoDrLwjSekPE62PaVWca rxl/EYV2nJm+M/Rh69L6iD9hVWkufLMf8AkyHvI2SiMCCs9z2/BzMTO8RZoadwwjeMPLD R1zfwfG2YiMMsg6mnwyBXZ2JaT1GrDRaxxaufWm4L9YgpD9mZkeXjRAx3Uw8gHcDP8ySF P+9cl1alNLT+YbNqvtPGrZsV1oZEAizpYDiKo3Wzv+jMJNop/Q9h1m9l8tUo+QN09A/eg P8oom7cdZ20dvq4GYQIDNJu+I/Lx1FAf8tQzy3wJFrzzuJLUb7DgEHFq6AoWeZph80A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796309710; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=FUO+eCD1y7YmDfTZkKx4UOFOeb3cw69TqWBNjFkF8Fw=; b=y0wrzml5tA7w7JlHWHMDM/cTF4Qlkt2ZVnOX5HHwUAIaGXAHZ30SwVrwwywt1q46lbKC cWVYjXYTV110muYm7x+uhDsPwEpzrKFYmQFQIMquZT7XynzQ6xfQ8W4Lx5x8KFpc9ktHi pkhFEcpOqIRNRLM/90cbWBg7buwfxNRXq6vAdxGvMDMj0ffC83qz4UogVq5YSeg2Ffpj7 FvPpZEEymTNnSf8TuES0Zr5c2KepEWCxiNSdikMGvr/GGbw2WNGmWWsZcAn6nLdhwR+46 b7WhpTUFcSevsdr4Kx1DByDjZfa3391TbKHydguLv6tyGq/EHnhcuYhjfW3U0bropQw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VzXjXj71; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81F37606A683; Thu, 3 Dec 2026 14:55:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=VzXjXj71 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796309710; b=jtF2K2ghfq0AaK5Q6EGkGlbjxxV4cuRJqJKV85Z2HAEgiLRKBWqL8bujwixgpf5RmOF2 NgC4mfotgvwNG0aWsIVBQ6EvHdy8bOg2z5sepLRAyoF9zQ693gnCQ3IgKBvGq8dzZDxkc /gZjzpPBNOcLjbMf5raqz+ElTRi1oplltaSxNLd+mkCRotC2FHAJc1CftiItPrROyDeU1 pMzxPPBo8QCFyKMRlVMzD042r7afCJlV0xHim4E7QZEVIPQghDqPJWCf1PYC6Zb9AaQoB 5Wy/EQQBT+aGCVCZWHJd1iIQBqB+2XeRJKbDmj4dj1K1fN4f9abneLWlPC8gG9lYdmw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796309710; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:To:Cc:X-Mailer:X-Developer-Signature: X-Developer-Key:X-Endpoint-Received:X-Original-From:Reply-To: Content-Transfer-Encoding; bh=FUO+eCD1y7YmDfTZkKx4UOFOeb3cw69TqWBNjFkF8Fw=; b=hj98u/UEsj7g0UXUGuzINN+4ki6WOUvC1853bR3DlApcKKuGlQJfXVpph/y6dmkqUomz rKC/OVL4AC/GYLgrd0JCDV7y0Tt7PgYADwdS0BaQLg6uqA3y5r+kv4fRC+QIBPxJTssoU l1hqzQGxDuuNt5Q54sIhhxdWn2agJxSrE9rgVi/rDfVJEDWW8vOi3tPiQSWd/1KbguXTs ZSHax5Ug0qfU6hPm4PbXEeRcaEEBDUTTMFb5rsegj/2ckRo/NngvsqNeyLHlXCsMcX+Mt Au2nwqGevf6ASyH1u0oVq2MfC08dGTofKxOY1kJop5O6+HA0AMy9+kHcgU662uXmFvw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 3A9EE1F00893; Thu, 3 Dec 2026 14:55:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796309710; bh=FUO+eCD1y7YmDfTZkKx4UOFOeb3cw69TqWBNjFkF8Fw=; h=From:Date:Subject:To:Cc:Reply-To; b=VzXjXj718a+vQZ9JpGapno8NlZ2JUp4uw+RQXg4S/vEOUpiXHTzPC2WvrsaFZOFWC FxffoEWOzlNzEszdcvwa8FiBU1B0LO+6R7kNI3ukouIBSwUBb55q73/q0Y+ViiobPR SO09C7k+j6HURlEhCzxQInyNzFyaUUurWnMYmI5KrXuwhHXBArD0A6T8mwmUir/w4i A97vqRjvHoDuCUosdBeruCbVVGZoKK74d6BZ7pgR3KsGheNYg9zCei8h7WjCkYGVd8 MMHCsysMuk6fklr8FglN711oEk87VtWoCkoNfRz7VsKbzWQI81gjjOI1O5jvH88ymG WqoXhN/4Py9TQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id AB96D4F9EB; Thu, 3 Dec 2026 14:55:05 +0000 (UTC) From: Mostafa Amin via B4 Relay Date: Thu, 03 Dec 2026 09:55:03 -0500 Subject: [PATCH v2] fs/ntfs3: serialize size changes with mapping->invalidate_lock 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-Type: text/plain; charset="utf-8" Message-Id: <20261203-fix-krn-ntfs3-inline-read-v2-1-01ffbc2999b5@windriver.com> X-B4-Tracking: v=1; b=H4sIAMaCEWsC/42NSw7CMAxEr1J5jVE+bSRYcY+qiypxqAWkyKkCq OrdCT0ByzeaebNCJmHKcG5WECqceU4VzKEBP43pSsihMhhlnNbGYeQ33iRhWmK2yOnOiVBoDHi yLnbKeN/qDur+KVTLu7sfKk+cl1k++1XRv/Qfa9Go0apgDTk1trG7vDgF4UJy9PMDhm3bvgWd7 qzGAAAA To: Konstantin Komarov Cc: ntfs3@lists.linux.dev, linux-kernel@vger.kernel.org, syzbot+079e16bc964fb91acb4a@syzkaller.appspotmail.com, linux-fsdevel@vger.kernel.org, syzbot@syzkaller.appspotmail.com, stable@vger.kernel.org, Mostafa Amin X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796309705; l=6972; i=mostafa.amin@windriver.com; s=20261126; h=from:subject:message-id; bh=qrvm4MGzbYv1iOcge5IuuLvXxAcmfo73l1tWB4/dbYA=; b=tvur42F7S5U50Tf57k7U14vUg8y8sxTl+5cuhg+o5Q5R1rRY6lI5cBbp5S/x+CaeUVDm7Yvmz LyV6Vhm5tpFCLkufPSHpuTbbOFszYKU+6IyIfc+HKD7Rhq65/oD7b6B X-Developer-Key: i=mostafa.amin@windriver.com; a=ed25519; pk=miyC6GYBn9SqytJ6Cgwe1MLT98rhnwuFEIO8ErmAetY= X-Endpoint-Received: by B4 Relay for mostafa.amin@windriver.com/20261126 with auth_id=1656 X-Original-From: Mostafa Amin Reply-To: mostafa.amin@windriver.com Content-Transfer-Encoding: 7bit From: Mostafa Amin syzbot reported a WARNING in iomap_read_inline_data() ("size > iomap->length") triggered by concurrent ftruncate() and mmap() page faults on a resident NTFS3 file: CPU0 (filemap_fault) CPU1 (ntfs_setattr / write_iter) -------------------- -------------------------------- filemap_invalidate_lock_shared() ntfs_read_folio() ntfs_iomap_begin() attr_data_get_block() iomap->length = 5 truncate_setsize(inode, 20) i_size_write(inode, 20) ntfs_set_size(inode, 20) iomap_read_inline_data() size = i_size_read(inode) = 20 WARN_ON_ONCE(20 > 5) -> -EIO iomap_read_inline_data() copies i_size_read(inode) - iomap->offset bytes out of iomap->inline_data and warns with -EIO if that exceeds iomap->length. In ntfs3, attr_data_get_block_locked() snapshots a resident $DATA attribute into an allocated page under ni_lock and sets iomap->length to res.data_size, dropping ni_lock before returning from ntfs_iomap_begin(). Meanwhile, ntfs_setattr() updates inode->i_size via truncate_setsize() before ntfs_set_size() grows res.data_size and without holding mapping->invalidate_lock. Extending writes in ntfs_file_write_iter() and preallocation in ntfs_fallocate() likewise resize the attribute and inode->i_size via ntfs_set_size() without mapping->invalidate_lock, allowing a concurrent page fault or buffered read to observe the new inode->i_size against an inline iomap built from the old resident size. Serialize regular-file size and resident/non-resident layout changes against pagecache readers and faults with filemap_invalidate_lock(): - In ntfs_setattr(), hold mapping->invalidate_lock across ATTR_SIZE updates; when extending, truncate any stray pagecache beyond oldsize and run ntfs_set_size() before pagecache_isize_extended(), passing i_size_read(inode) in case ntfs_set_size() clamps the extended size (e.g. on $MFT), so i_size never leads res.data_size or transiently changes on error. - In ntfs_file_write_iter(), reject extending IOCB_NOWAIT writes with -EAGAIN and hold mapping->invalidate_lock around ntfs_set_size(). - In ntfs_fallocate(), hold mapping->invalidate_lock across all modes, including default preallocation which converts resident files to non-resident and extends i_size. Fixes: 099ef9ab9203 ("fs/ntfs3: implement iomap-based file operations") Reported-by: syzbot+079e16bc964fb91acb4a@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=079e16bc964fb91acb4a Tested-by: syzbot@syzkaller.appspotmail.com Cc: stable@vger.kernel.org Signed-off-by: Mostafa Amin --- Developed with assistance from an AI coding agent (used to trace the iomap_read_inline_data() race and syzbot CI $MFT clamping report, audit ntfs_set_size() callsites in fs/ntfs3/file.c, and run QEMU/KVM reproducer tests). Every line of the change was reviewed and verified by the human author. Tested on v7.4-rc4 (x86_64 QEMU/KVM, 4 vCPUs, CONFIG_KASAN=y, CONFIG_PROVE_LOCKING=y, CONFIG_UBSAN=y, CONFIG_DEBUG_ATOMIC_SLEEP=y, CONFIG_DEBUG_KMEMLEAK=y, panic_on_warn=1): - syzbot CI v1 reproducer ($MFT truncate clamping in ntfs_setattr): 3/3 crashes on v1, 0/10 crashes on v2, 3/3 crashes with v2->v1 reverted, plus 0/3 crashes on extended $MFT truncate/mmap/write stress. - syzbot C reproducer (ftruncate vs mmap + MADV_DONTNEED): 3/3 crashes before fix, 0/10 crashes on v2. - Extending pwrite + fsync + POSIX_FADV_DONTNEED race and resident-to- nonresident data-integrity suite: 1/1 crash before fix, 0/3 on v2. - W=1 build of fs/ntfs3/ on debug (CONFIG_NTFS3_FS=y) and allmodconfig (CONFIG_NTFS3_FS=m): 0 warnings. Not tested: sparse/smatch/coccinelle, non-x86_64 runtime, full xfstests. --- Changes in v2: - Pass i_size_read(inode) instead of newsize to pagecache_isize_extended() in ntfs_setattr() in case ntfs_set_size() clamps the extended size (e.g. on $MFT), fixing a WARNING in pagecache_isize_extended() reported by syzbot CI. - Add Tested-by: syzbot@syzkaller.appspotmail.com. - Link to v1: https://lore.kernel.org/r/20261126-fix-krn-ntfs3-inline-read-v1-1-30d32e60a4f5@windriver.com --- fs/ntfs3/file.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/fs/ntfs3/file.c b/fs/ntfs3/file.c index 6c84a3e1c7fe..5f0fdd6d6d34 100644 --- a/fs/ntfs3/file.c +++ b/fs/ntfs3/file.c @@ -518,12 +518,9 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len) goto out; } - if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | - FALLOC_FL_INSERT_RANGE)) { - inode_dio_wait(inode); - filemap_invalidate_lock(mapping); - map_locked = true; - } + inode_dio_wait(inode); + filemap_invalidate_lock(mapping); + map_locked = true; if (mode & FALLOC_FL_PUNCH_HOLE) { u32 frame_size; @@ -736,15 +733,25 @@ int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, goto out; } inode_dio_wait(inode); + filemap_invalidate_lock(inode->i_mapping); oldsize = i_size_read(inode); newsize = attr->ia_size; if (newsize != oldsize) { - truncate_setsize(inode, newsize); - - err = ntfs_set_size(inode, newsize); + if (newsize > oldsize) { + truncate_pagecache(inode, oldsize); + err = ntfs_set_size(inode, newsize); + if (!err) + pagecache_isize_extended(inode, oldsize, + i_size_read(inode)); + } else { + truncate_setsize(inode, newsize); + err = ntfs_set_size(inode, newsize); + if (err) + i_size_write(inode, oldsize); + } if (err) { - i_size_write(inode, oldsize); + filemap_invalidate_unlock(inode->i_mapping); goto out; } @@ -753,6 +760,7 @@ int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); } + filemap_invalidate_unlock(inode->i_mapping); } setattr_copy(idmap, inode, attr); @@ -1288,11 +1296,17 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) endbyte = vbo + ret; if (endbyte > inode->i_size) { + if (iocb->ki_flags & IOCB_NOWAIT) { + ret = -EAGAIN; + goto out; + } /* * Normal files: increase file size, allocate space. * Sparse/Compressed: increase file size. No space allocated. */ + filemap_invalidate_lock(inode->i_mapping); ret = ntfs_set_size(inode, endbyte); + filemap_invalidate_unlock(inode->i_mapping); if (ret) goto out; } --- base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f change-id: 20261126-fix-krn-ntfs3-inline-read-936f502cc415 Best regards, -- Mostafa Amin