From 42fd9a15e38f093a1cc3a20f1a10b7944489a5b1 Mon Sep 17 00:00:00 2001 From: Steve French Date: Thu, 15 Aug 2024 14:03:43 -0500 Subject: [PATCH] smb3: fix lock breakage for cached writes BugLink: https://bugs.launchpad.net/bugs/2084005 commit 836bb3268db405cf9021496ac4dbc26d3e4758fe upstream. Mandatory locking is enforced for cached writes, which violates default posix semantics, and also it is enforced inconsistently. This apparently breaks recent versions of libreoffice, but can also be demonstrated by opening a file twice from the same client, locking it from handle one and writing to it from handle two (which fails, returning EACCES). Since there was already a mount option "forcemandatorylock" (which defaults to off), with this change only when the user intentionally specifies "forcemandatorylock" on mount will we break posix semantics on write to a locked range (ie we will only fail the write in this case, if the user mounts with "forcemandatorylock"). Fixes: 85160e03a79e ("CIFS: Implement caching mechanism for mandatory brlocks") Cc: stable@vger.kernel.org Cc: Pavel Shilovsky Reported-by: abartlet@samba.org Reported-by: Kevin Ottens Reviewed-by: David Howells Signed-off-by: Steve French Signed-off-by: Greg Kroah-Hartman [koichiroden: adjusted context due to missing commit 3ee1a1fc3981 ("cifs: Cut over to using netfslib")] Signed-off-by: Koichiro Den Signed-off-by: Stefan Bader --- fs/smb/client/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c index 438d68d681b1..1c7c6a4347d3 100644 --- a/fs/smb/client/file.c +++ b/fs/smb/client/file.c @@ -3821,6 +3821,7 @@ cifs_writev(struct kiocb *iocb, struct iov_iter *from) struct inode *inode = file->f_mapping->host; struct cifsInodeInfo *cinode = CIFS_I(inode); struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; + struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); ssize_t rc; inode_lock(inode); @@ -3834,12 +3835,16 @@ cifs_writev(struct kiocb *iocb, struct iov_iter *from) if (rc <= 0) goto out; - if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), + if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) && + (cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), server->vals->exclusive_lock_type, 0, - NULL, CIFS_WRITE_OP)) - rc = __generic_file_write_iter(iocb, from); - else + NULL, CIFS_WRITE_OP))) { rc = -EACCES; + goto out; + } + + rc = __generic_file_write_iter(iocb, from); + out: up_read(&cinode->lock_sem); inode_unlock(inode);