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=1796387452; b=biCPRvf1w4HM5cs31K78IVCJg8Q8kYlM3mRZT/SJLbmPSJWwRYdlcXAO96ftk5zW2G3W 7+v10lmKuhfLAYZVkSfo8OhtlSyRW5vuLUf18Z/MIN/SYS0vDKQ35uUsXSh1a47H0ORz7 JeC5U9KcYREVLEVnkQbSYrw25yd1CX0Yxp9gGW7jHroCFlNqSYmeB54JtpEF6EasEqnjv VWsjBiRNZE5rM9ejN/cVbzxHPi4zj3j09tHZCM30XOf9BwXGTazMpCrukcKLQP3H/+O7p iI0mxLnK4/GUQI58JKQRS9pyuEYiSKON5mtf2Q3jNQOGv61NDSd9axd8G9Z5p65kkOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387452; 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=ls/LKlOyy2aH7Fp8GOiAzjIVoj0QkbBNuGjDYXN9Dqw=; b=gbR/MZvp55OPczLQ+Te7mc6Dc3qXCNLWUbyDqhfI1SnNrxVcxzVskBzdomsGYLTsQs4r V5uwvbgCuQce/W8q/xmIEG60gbQUvBSw4mxIdhtdIKfkyykU9JmCH7pP36kvOzWeFkH5y l4H8ZpAdk5+m6GukWPMEQFEgOwC0Yh8w0vlrFhtExRwGL5CzeB3I2gZszR7Sfpv5N4RR1 MwBVKnhpsbggiyxlKN83IDpRvZ1/4Ham0lWjzjXibUz3yHQhPJawPcWNGuQ2eEyJ/qhe0 +nUa4Xa/xFwWQK4uz1NpXHfOXkvE4zW0gbqOJ0kEnPapooocSGz+Wzo7cJbaswuRAWg== 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=YA9ZuTt2; 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 0F90A606A334; Fri, 4 Dec 2026 12:30:52 +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=YA9ZuTt2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7EFE01F000CE; Fri, 4 Dec 2026 12:30:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387451; bh=ls/LKlOyy2aH7Fp8GOiAzjIVoj0QkbBNuGjDYXN9Dqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YA9ZuTt23zedT7xQEkHvg0MChZ39ZqWFpU2ut0+VDIJq8N2FBGc1YKdK6d4euSt2v DMnyPNZ2Ua+qSkM+inIO8x5/H7Ix50W+YTJmtkLnQpXDOzplKSpsyWHctzXk/6mcGI TR4JjwfJhX+7Q5y2gaKTA8Fb+p5BNqATxJJeTk90= 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 6.1 0232/1059] f2fs: read i_size once in f2fs_vm_page_mkwrite Date: Fri, 4 Dec 2026 18:56:03 +0900 Message-ID: <20261204100650.283450463@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-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 [ sashal: Adapted the fix to the page-based f2fs_vm_page_mkwrite() (lock_page, page_offset, zero_user_segment) instead of the folio API, with the need_alloc context line differing. ] 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 e94d30e397e2c..f1b1754fb4de4 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -58,6 +58,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 = true; + loff_t isize; int err = 0; if (unlikely(IS_IMMUTABLE(inode))) @@ -115,8 +116,14 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) } lock_page(page); + /* + * 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(page->mapping != inode->i_mapping || - page_offset(page) > i_size_read(inode) || + page_offset(page) > isize || !PageUptodate(page))) { unlock_page(page); err = -EFAULT; @@ -147,11 +154,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)(page->index + 1) << PAGE_SHIFT) > - i_size_read(inode)) { + if (((loff_t)(page->index + 1) << PAGE_SHIFT) > isize) { loff_t offset; - offset = i_size_read(inode) & ~PAGE_MASK; + offset = isize & ~PAGE_MASK; zero_user_segment(page, offset, PAGE_SIZE); } set_page_dirty(page); -- 2.53.0