From nobody Tue Dec 16 04:03:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39E1AC07E9D for ; Mon, 26 Sep 2022 11:34:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235547AbiIZLez (ORCPT ); Mon, 26 Sep 2022 07:34:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238057AbiIZLdG (ORCPT ); Mon, 26 Sep 2022 07:33:06 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5EF56DFBA; Mon, 26 Sep 2022 03:43:17 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 01AF960B6A; Mon, 26 Sep 2022 10:43:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB600C433D6; Mon, 26 Sep 2022 10:43:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664188986; bh=9tDA6zF8149gV29rFUCy+sshSnUxadCtFYcNRA/IQD8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ShrTeC7TnbzxT2CpEu1fm1eubTV/J+duLVzJoCInbmR4vvXMGClFmpHYksZhdpkbd e907zorZ+DMg+ywR1fxJXMK5OrzpJDeUlZI87fnqNO2Qb6QuCRQyLDjHdikXDe4850 CW9DLVGi+PASxBpFCbQX5PvkIW1hm/EEdnhvQKP8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Howells , Ronnie Sahlberg , Steve French , Sasha Levin Subject: [PATCH 5.19 009/207] smb3: fix temporary data corruption in collapse range Date: Mon, 26 Sep 2022 12:09:58 +0200 Message-Id: <20220926100806.904995027@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926100806.522017616@linuxfoundation.org> References: <20220926100806.522017616@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Steve French [ Upstream commit fa30a81f255a56cccd89552cd6ce7ea6e8d8acc4 ] collapse range doesn't discard the affected cached region so can risk temporarily corrupting the file data. This fixes xfstest generic/031 I also decided to merge a minor cleanup to this into the same patch (avoiding rereading inode size repeatedly unnecessarily) to make it clearer. Cc: stable@vger.kernel.org Fixes: 5476b5dd82c8b ("cifs: add support for FALLOC_FL_COLLAPSE_RANGE") Reported-by: David Howells Tested-by: David Howells Reviewed-by: David Howells cc: Ronnie Sahlberg Signed-off-by: Steve French Signed-off-by: Sasha Levin --- fs/cifs/smb2ops.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c index ef8cb7fbabeb..8e55495c4f7e 100644 --- a/fs/cifs/smb2ops.c +++ b/fs/cifs/smb2ops.c @@ -3952,41 +3952,47 @@ static long smb3_collapse_range(struct file *file, = struct cifs_tcon *tcon, { int rc; unsigned int xid; - struct inode *inode; + struct inode *inode =3D file_inode(file); struct cifsFileInfo *cfile =3D file->private_data; - struct cifsInodeInfo *cifsi; + struct cifsInodeInfo *cifsi =3D CIFS_I(inode); __le64 eof; + loff_t old_eof; =20 xid =3D get_xid(); =20 - inode =3D d_inode(cfile->dentry); - cifsi =3D CIFS_I(inode); + inode_lock(inode); =20 - if (off >=3D i_size_read(inode) || - off + len >=3D i_size_read(inode)) { + old_eof =3D i_size_read(inode); + if ((off >=3D old_eof) || + off + len >=3D old_eof) { rc =3D -EINVAL; goto out; } =20 + filemap_invalidate_lock(inode->i_mapping); filemap_write_and_wait(inode->i_mapping); + truncate_pagecache_range(inode, off, old_eof); =20 rc =3D smb2_copychunk_range(xid, cfile, cfile, off + len, - i_size_read(inode) - off - len, off); + old_eof - off - len, off); if (rc < 0) - goto out; + goto out_2; =20 - eof =3D cpu_to_le64(i_size_read(inode) - len); + eof =3D cpu_to_le64(old_eof - len); rc =3D SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid, cfile->fid.volatile_fid, cfile->pid, &eof); if (rc < 0) - goto out; + goto out_2; =20 rc =3D 0; =20 cifsi->server_eof =3D i_size_read(inode) - len; truncate_setsize(inode, cifsi->server_eof); fscache_resize_cookie(cifs_inode_cookie(inode), cifsi->server_eof); +out_2: + filemap_invalidate_unlock(inode->i_mapping); out: + inode_unlock(inode); free_xid(xid); return rc; } --=20 2.35.1