From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796129493; b=BtoRSjCOPHbNDUA/+5T5GayaePHZ85L9lEjErHdaQ0SK9XFv3Bzva75Ja2+meorQMlLm 98ll9fB3gRqJOXc6gvEAsi2WGgiIhm+pj+aY+HVjbMEc549HRB4cDx3rs5bMhvEgBdSKr mkEn2uvbs582X8IV209Ri4/4PIV2WSRLxX52q6aVOKuRlWmPZnC/t7x7y4g8AK8f7psXH ovKSBT66pYwCIRlhl0hUFYzTH2DR5ZTkCfMV6fc3HqEUiAj1veWe4JvOkdT/eFRhCd1FX 0vFc1Zi6JmMzVhzq9PQXLH78YrrK7mw0bPHVenWTslno13SGJU9aWaQd8eYQFFX1uqA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796129493; 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=uwg5MwPgOHBaoSES/43VLhxSGVlP67fjU9v2QI6wJuw=; b=HwAsYngGydkUcNItv2oMONkGl8nADQ/7SG6/aJAckmBYJ8UpaHtxU4bMP7qEQfp1b58k hJXCNqw88j5ABC8lY6dzAKx+pMsuw92homcCAU/PB1SxLovIA5h+r5kRbCKP1CUlEEN56 DEavf4Pwf2uZLwR1TPg5L078fuJmn87dPtC+4gNysUdKkHQYDmCUOOjFpw7Z2KlCGRe56 MhaQ3cADszNDW2+JVCx5hF9ShO/jdLxU0dWs7ZIgFhSQX+xUwvFi2MhHEE5/YVugW0iqv rmutCnDSGYlnPE1wkETvFHznjh1keJ6AUQhhRtfDEE9mWQogz6DUdaUqGcRugSWm0Cg== 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=Gnd+sBmN; arc=none smtp.remote-ip=209.85.210.181 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.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 0C4B1606A325 for ; Tue, 1 Dec 2026 12:51:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0C4B1606A325 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=Gnd+sBmN Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-8a070755c8cso5541594b3a.0 for ; Tue, 01 Dec 2026 04:51:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796129492; x=1796734292; 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=uwg5MwPgOHBaoSES/43VLhxSGVlP67fjU9v2QI6wJuw=; b=Gnd+sBmNebIOIRiaPuw89w+Pq9yYEtdGXUlwzRy4eOOjP+20AhwchYSeXZ20tesJYO qUVxcocpOsZShYZzJv3F0qgqbFqHvDdWU/wdPjrU/SfontGQJpd86kBEoG452jCqewZ+ DhDqKfJ0/R75VY2He/UKL1+HivGPrGycZDpGpgR2p4CwUJMXaK1qhSHLJpAOTrB+Ta89 yZJPkZuKwFY9vyIfMki64L6FurcsWIE5VPqEQMIWv218pxLVD4AIpEEYu5qDgKNQuNCB 1j6UBmttpe1GP2oRI8Um9CZnlpGydG4ezdfUke3Y2ZNnKvKqtascKiy56uUdxubU+IKI Xq3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796129492; x=1796734292; 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=uwg5MwPgOHBaoSES/43VLhxSGVlP67fjU9v2QI6wJuw=; b=CbHJL8UyALyT622xtvEiOiv72wXrxdo9f3nIgyfeblH4CFnSnFJutLfzMBVL5ugwW0 yA5kh9Js/6LlzLpseoIPrWCZjF41Cymf9K6+jcLaxU9qHR5yP2ikal2plhRguPWeT/3x GUJ/bf2qvLFjk78y2bqxj9mkcDquGBTLyPFvSbVOjrQo8HJb3Zs+AfrLk1g7/RzPpxEL 2kkiiZzWXsu5hYIem6PKvCShwC6HlfMKSkxBrQYxIV/kqjSbh6S5fYZAiRkuMC6NdPL0 KtkjAACaPM5Vf4LKYLpCnJ+B13oQA6hos1oSwWM4+vUJlCVycBpZSBqfZvyX4szeAXl3 0Osg== X-Forwarded-Encrypted: i=1; AL+hErFhb/UEozbJCLUm7H7PLXAFjnPL1tskEbmBeBMKPnfiHxA53YOdwZAS7hFYrSpy9/sHn8s90n51qgHKVE4=@vger.kernel.org X-Gm-Message-State: APuU4Z/Q3nA4xlGYS2k3k3beH+7vrk24QeiUOwTklLXQdFcWcy7XFsls TomUoCO8E4z6bEauJDHllVOq3nxofwNYdcgMfX2rpZGzmSip33VYoBcV X-Gm-Gg: AZFdTZfiCyf2VHKjXgLzGgP00QUTB/jeIqlDzSKN15lUeqkdq+0ABG4KVt0uFPULtiR +8lVTeL+iEBR3uS9PZEgejl0mxsJy7Da8bp7wkfn+96ES8cvdC1jtzVQf+at7tpNH6xzMBMpMfI n6RWQKlZzUkwhdfZ8yincOT+vQfCzeN/zyq0//tA0U38sWUofYyy+rA7gmVUX5/kyDBMlIk+Smj CTmxzl8RABAY85r8sYOzjyQ6ZDFoUqInvuAUfb9Oiu339Ewuar1XSZ5FGFnNnydy9BOOgmpmQPy 9f2NVnBlF6ucDvh2gZcpl1KEnt6eVI1COaWOV6+Q0Qg2axYUX1MRc5M6Xr4LNRMyQknhffZf2LR Tec+NW6oROWaip9LQYz9U9FKCatqD0MRd1DNAgQ7N4Ga06oRsPRcLOAaPCVkOwP6ASEBNZdEtGY 6d931DPi3YR2VYdVFGHuQBR2qfpmNyrjb9z/twr7Hq5RrRw322igpwnLlE4guc7xe4gh9Piavdb zQt7mPCUBv+iT4dQPO6byYw/QDaqKssNaJmvCHF3Q1vVeiqNeScBeO/kgun9AfFKhrXdw80QrEN HwNmV9Ry+ZkLs7Bj2e4tbqfY9mtkDcXwbFxPmqlgOV3XjH7U3b08N3UiNSs4okNZwHw= X-Received: by 2002:a05:6a20:db97:b0:3e2:b942:ef96 with SMTP id adf61e73a8af0-4014bc4e18bmr40660827637.35.1796129492096; Tue, 01 Dec 2026 04:51:32 -0800 (PST) Received: from osman.mioffice.cn ([43.224.245.178]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde6cfcadffsm19859992a12.9.2026.12.01.04.51.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 04:51:31 -0800 (PST) From: Zhan Xusheng To: linkinjeon@kernel.org, hyc.lee@gmail.com Cc: liubaolin12138@163.com, ernest.borowski.git@proton.me, ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, zhanxusheng@xiaomi.com, stable@vger.kernel.org Subject: [PATCH v2] ntfs: zero up to a page that a shared mapping makes writable Date: Tue, 1 Dec 2026 20:51:26 +0800 Message-ID: <20261201125126.1651071-1-zhanxusheng1024@gmail.com> X-Mailer: git-send-email 2.43.0 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 From: Zhan Xusheng A page fault that makes a page of a shared mapping writable moves the initialized size to the end of that page. If the page starts past the initialized size, nothing zeroes the range in between, and the allocated tail of the cluster holding the old initialized size becomes readable with whatever the disk held before. This needs clusters larger than a page. For example, with 64K clusters, write 4K to a new file, map it 4M MAP_SHARED, grow it to 4M with ftruncate() and store 8 bytes at 2M through the mapping. After msync() and a remount, [4K, 64K) reads back the old contents of the cluster. Zero the gap with ntfs_extend_initialized_size() before the fault takes the invalidate lock, as a write past the initialized size does. A truncate can shrink the file between the fault and the zeroing, so have ntfs_extend_initialized_size() check the file size under that lock, or the initialized size ends up past the end of the file. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org --- v2: check the file size under the invalidate lock, as a truncate between the fault and the zeroing left the initialized size past the end of the file (Baolin). The check is in ntfs_extend_initialized_size(), so it also covers ntfs_file_mmap_prepare(), which reads the file size without the lock as well; that path is not tested. v1: https://lore.kernel.org/all/20261130030220.1100209-1-zhanxusheng1024@gmail.com/ Tested in a VM on ntfs-next 329758f9d73b, with all free space filled with a known pattern first. The example above, bytes of old data in [0, 64K) after remount: ntfs-next v2 4K clusters 0 0 64K clusters 61440 0 With a test-only msleep() before the zeroing, the store at 2M waiting there and the file shrunk to 64K meanwhile, the store gets SIGBUS in both versions. After remount, v1 leaves "initialized size 2097152, data size 65536" and the inode cannot be read; v2 leaves 65536 for both. Shared mappings made writable with mprotect(), the killed and stalled writer cases, fsx (buffered and O_DIRECT), insert/collapse range and the partial O_DIRECT cases give the same results with and without the patch. No lockdep or KASAN reports. fs/ntfs/file.c | 15 +++++++++++++++ fs/ntfs/inode.c | 9 +++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index 31ff35176e05..213a867dd2a4 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -1177,6 +1177,7 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf) struct inode *inode = file_inode(vmf->vma->vm_file); struct address_space *mapping = inode->i_mapping; bool stream = ntfs_inode_is_named_stream(NTFS_I(inode)); + loff_t pos; int err; vm_fault_t ret; @@ -1195,6 +1196,20 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf) file_update_time(vmf->vma->vm_file); } + /* + * Making the page writable moves the initialized size up to its end. + * If the page starts past the initialized size, zero what lies between + * first, as a write there would, or the tail of the last initialized + * cluster becomes readable with whatever the disk held. + */ + pos = (loff_t)vmf->pgoff << PAGE_SHIFT; + if (NInoNonResident(NTFS_I(inode)) && + pos > NTFS_I(inode)->initialized_size) { + err = ntfs_extend_initialized_size(inode, pos, pos); + if (err) + goto out_error; + } + /* * Serialize against truncate/fallocate which hold the lock * exclusively while invalidating pagecache and changing extents. diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c index 3aac1d7d310f..84762691046e 100644 --- a/fs/ntfs/inode.c +++ b/fs/ntfs/inode.c @@ -2647,10 +2647,15 @@ static int __ntfs_extend_initialized_size(struct inode *vi, int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, const loff_t new_size) { - int err; + int err = 0; filemap_invalidate_lock(vi->i_mapping); - err = __ntfs_extend_initialized_size(vi, offset, new_size); + /* + * A page fault or mmap() looks at the file size without the lock, so + * a truncate may have shrunk the file since: don't initialize past it. + */ + if (new_size <= i_size_read(vi)) + err = __ntfs_extend_initialized_size(vi, offset, new_size); filemap_invalidate_unlock(vi->i_mapping); return err; } -- 2.43.0