From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794064748; b=oQWRPzVBnSeIJelUeOtpG5isvV/qD+PJEt4VAxAeZ/3lFNxnU6wLdLReTb+TONGsPL+4 ZiadacmMDc68naZ32oyzpOgHaqG9q3TMpwrE9hzYqhh2adBwmOw4M2YZlaJEWWHCBFb3L wlwK85s2iNggX9ZU53k6bgOADHDfijci5jyFwpeRu+FCmeN833JGGX1QHIqKqYOD1nR79 1l1cfx8p/ZBl6vmbVuTGXd12/9LW+IKNGEBZVxQbF8r0mnIBjSRUdKJGEvYs/X1RYXTlV hWPiicZ5nt0FJU2tN3J9jzRjMOCJfQR+Du0tlV+bdqgPZbs0mbM3Re0BqGhXpMaNgsQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794064748; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=ZcggKSe1vu0rrSjVxaVQ1RkcR3UZfcprGtETrjlHV0c=; b=h4PH2fD+xoCYNDRun6mwO4ELAf69qBP7DvhlkOuBeRlru5Sk71KbX1CK6/xyWH3kJBb1 myeLcUM+PfLZL4suXIXs0BRi9NosfeQMgRrv4t6h9VUojCluKah2oiNM7n9aulnk79qeu ng1EeeClxXOtBRhoRY111uH2osi4Q/mENSbkrLE51JVqrJSKgpkkssuFWxvr2w2wZdUrs lzGqyg0eEbcvzciEGo1vqQjiTrnaeVhTuyH+z18JHxxRrkpNted5roH5t5aExn+1fTuDA Zl5P0jl4sDb41qbEyQluCktJJAyQrqrGsBwpGNCMP4PF62LQua1GiquNYKy6396C3rw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=iU2Dk7AY; arc=none 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 C495E606A334; Sat, 7 Nov 2026 15:19:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=iU2Dk7AY Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72F321F008A3; Sat, 7 Nov 2026 15:19:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794064747; bh=ZcggKSe1vu0rrSjVxaVQ1RkcR3UZfcprGtETrjlHV0c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=iU2Dk7AYU23kKl/OcseqLW5KWeZ81IGx/+f2Boi3f4KYD9+/jrjz+Mrvw4ofAmRPA Og3R+SQVzdBg4Wu3DISc0qxGCr2rIWsEuF+sSyLHXzqi2+UskNuxsyQXiIoUtxqJko drqAFEsJ8QnEVDBo8+PDaXdbPowXehOCUFhiYuFY= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sashiko , Nanzhe Zhao , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 7.3 0722/1257] f2fs: read i_size once in f2fs_vm_page_mkwrite Date: Sat, 7 Nov 2026 08:50:18 -0500 Message-ID: <20261107134959.202457186@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261107134939.434104957@linuxfoundation.org> References: <20261107134939.434104957@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Nanzhe Zhao [ Upstream commit c04f209773ecb31e592cababce0d6658ee0b8d58 ] f2fs_vm_page_mkwrite() reads i_size twice: once to check whether the folio is partially beyond EOF, and once to calculate the offset for folio_zero_segment(). But a concurrent buffered write can extend isize and cause the following race condition to corrupt data: buffered write writes [0,3500) CPU0 (write fault) CPU1 (buffered write) ---- ---- f2fs_vm_page_mkwrite() folio_lock(folio) if (((loff_t)(folio->index + 1) << PAGE_SHIFT) > i_size_read(inode)) /* i_size_read() = 3500 */ write(fd, buf, 200, 4096) /* writes into page1, does not wait for folio lock */ i_size_write(inode, 4296) offset = i_size_read(inode) & ~PAGE_MASK /* i_size_read() = 4296, offset = 200 */ folio_zero_segment(folio, offset, folio_size(folio)) /* valid data [200, 3500) is zeroed */ Fix this by reading i_size once and using the same value for both the EOF check and the zeroing offset. Reported-by: Sashiko Closes: https://sashiko.dev/#/patchset/20261012030315.978375-1-zhaonanzhe%40xiaomi.com Fixes: fbfa2cc58d53 ("f2fs: add file operations") Cc: stable@vger.kernel.org Signed-off-by: Nanzhe Zhao Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/file.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index b391ced41edda..6843c2aa67444 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -124,6 +124,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) struct f2fs_sb_info *sbi = F2FS_I_SB(inode); struct dnode_of_data dn; bool need_alloc = !f2fs_is_pinned_file(inode); + loff_t isize; int err = 0; vm_fault_t ret; @@ -187,8 +188,14 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) filemap_invalidate_lock_shared(inode->i_mapping); folio_lock(folio); + /* + * A concurrent buffered write can extend isize, so we store isize in a + * local variable to let the zeroing offset match the EOF check below. + */ + isize = i_size_read(inode); + if (unlikely(folio->mapping != inode->i_mapping || - folio_pos(folio) > i_size_read(inode) || + folio_pos(folio) > isize || !folio_test_uptodate(folio))) { folio_unlock(folio); err = -EFAULT; @@ -218,11 +225,10 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) f2fs_wait_on_block_writeback(inode, dn.data_blkaddr); /* page is wholly or partially inside EOF */ - if (((loff_t)(folio->index + 1) << PAGE_SHIFT) > - i_size_read(inode)) { + if (((loff_t)(folio->index + 1) << PAGE_SHIFT) > isize) { loff_t offset; - offset = i_size_read(inode) & ~PAGE_MASK; + offset = isize & ~PAGE_MASK; folio_zero_segment(folio, offset, folio_size(folio)); } folio_mark_dirty(folio); -- 2.53.0