]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
cifs: prevent updating file size from server if we have a read/write lease
authorBharath SM <bharathsm@microsoft.com>
Thu, 29 Feb 2024 17:39:52 +0000 (23:09 +0530)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 3 Apr 2024 13:32:18 +0000 (15:32 +0200)
[ Upstream commit e4b61f3b1c67f5068590965f64ea6e8d5d5bd961 ]

In cases of large directories, the readdir operation may span multiple
round trips to retrieve contents. This introduces a potential race
condition in case of concurrent write and readdir operations. If the
readdir operation initiates before a write has been processed by the
server, it may update the file size attribute to an older value.
Address this issue by avoiding file size updates from readdir when we
have read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

Cc: stable@vger.kernel.org
Reviewed-by: Shyam Prasad N <sprasad@microsoft.com>
Signed-off-by: Bharath SM <bharathsm@microsoft.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/smb/client/cifsproto.h
fs/smb/client/file.c
fs/smb/client/inode.c
fs/smb/client/readdir.c

index a841bf4967fa4d159704a6bc7b6d190be4eedf04..58cfbd450a55e5050631f196b23105f08b860ad3 100644 (file)
@@ -144,7 +144,8 @@ extern int cifs_reconnect(struct TCP_Server_Info *server,
 extern int checkSMB(char *buf, unsigned int len, struct TCP_Server_Info *srvr);
 extern bool is_valid_oplock_break(char *, struct TCP_Server_Info *);
 extern bool backup_cred(struct cifs_sb_info *);
-extern bool is_size_safe_to_change(struct cifsInodeInfo *, __u64 eof);
+extern bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 eof,
+                                  bool from_readdir);
 extern void cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
                            unsigned int bytes_written);
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, int);
@@ -201,7 +202,8 @@ extern void cifs_unix_basic_to_fattr(struct cifs_fattr *fattr,
                                     struct cifs_sb_info *cifs_sb);
 extern void cifs_dir_info_to_fattr(struct cifs_fattr *, FILE_DIRECTORY_INFO *,
                                        struct cifs_sb_info *);
-extern int cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr);
+extern int cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
+                              bool from_readdir);
 extern struct inode *cifs_iget(struct super_block *sb,
                               struct cifs_fattr *fattr);
 
index 98514f2f2d7b15add3583264e6ec25c45e7a0a21..9d42a390090765743d190f4ea64f9c34fbc19423 100644 (file)
@@ -329,7 +329,7 @@ int cifs_posix_open(const char *full_path, struct inode **pinode,
                }
        } else {
                cifs_revalidate_mapping(*pinode);
-               rc = cifs_fattr_to_inode(*pinode, &fattr);
+               rc = cifs_fattr_to_inode(*pinode, &fattr, false);
        }
 
 posix_open_ret:
@@ -4769,12 +4769,14 @@ static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
    refreshing the inode only on increases in the file size
    but this is tricky to do without racing with writebehind
    page caching in the current Linux kernel design */
-bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
+bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
+                           bool from_readdir)
 {
        if (!cifsInode)
                return true;
 
-       if (is_inode_writable(cifsInode)) {
+       if (is_inode_writable(cifsInode) ||
+               ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
                /* This inode is open for write at least once */
                struct cifs_sb_info *cifs_sb;
 
index d02f8ba29cb5bf22f1dcdcc3932f20afc3094f22..7f28edf4b20f39b394af2a8015e1470f78765256 100644 (file)
@@ -147,7 +147,8 @@ cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 
 /* populate an inode with info from a cifs_fattr struct */
 int
-cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
+cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
+                   bool from_readdir)
 {
        struct cifsInodeInfo *cifs_i = CIFS_I(inode);
        struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
@@ -199,7 +200,7 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
         * Can't safely change the file size here if the client is writing to
         * it due to potential races.
         */
-       if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
+       if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) {
                i_size_write(inode, fattr->cf_eof);
 
                /*
@@ -368,7 +369,7 @@ static int update_inode_info(struct super_block *sb,
                CIFS_I(*inode)->time = 0; /* force reval */
                return -ESTALE;
        }
-       return cifs_fattr_to_inode(*inode, fattr);
+       return cifs_fattr_to_inode(*inode, fattr, false);
 }
 
 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
@@ -403,7 +404,7 @@ cifs_get_file_info_unix(struct file *filp)
        } else
                goto cifs_gfiunix_out;
 
-       rc = cifs_fattr_to_inode(inode, &fattr);
+       rc = cifs_fattr_to_inode(inode, &fattr, false);
 
 cifs_gfiunix_out:
        free_xid(xid);
@@ -934,7 +935,7 @@ cifs_get_file_info(struct file *filp)
        fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
        fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
        /* if filetype is different, return error */
-       rc = cifs_fattr_to_inode(inode, &fattr);
+       rc = cifs_fattr_to_inode(inode, &fattr, false);
 cgfi_exit:
        cifs_free_open_info(&data);
        free_xid(xid);
@@ -1491,7 +1492,7 @@ retry_iget5_locked:
                }
 
                /* can't fail - see cifs_find_inode() */
-               cifs_fattr_to_inode(inode, fattr);
+               cifs_fattr_to_inode(inode, fattr, false);
                if (sb->s_flags & SB_NOATIME)
                        inode->i_flags |= S_NOATIME | S_NOCMTIME;
                if (inode->i_state & I_NEW) {
index b520eea7bfce83b2fd8e9a1d9e1685c39516840e..132ae7d884a97c69c8895cb58ac21f4972140895 100644 (file)
@@ -148,7 +148,7 @@ retry:
                                                rc = -ESTALE;
                                        }
                                }
-                               if (!rc && !cifs_fattr_to_inode(inode, fattr)) {
+                               if (!rc && !cifs_fattr_to_inode(inode, fattr, true)) {
                                        dput(dentry);
                                        return;
                                }