smb: client: fix file compression flag ioctls
Subsystems (2)
v2 cover smb: client: fix file compression flag ioctls
From: Huiwen He <[email protected]>
v1->v2:
- Patch 1 is unchanged.
- Patch 2 now requests FILE_READ_ATTRIBUTES for the temporary by-path handle, only falls back to by-path open for -EBADF from cifs_get_writable_file(), removes the thin set_compression wrapper, and moves the temporary cifsFileInfo off the stack to fix the LKP frame-size warning.
- Patch 3 now returns -EOPNOTSUPP for unsupported FS_IOC_SETFLAGS bits and is adjusted for the helper cleanup in patch 2.
Link to v1:
https://lore.kernel.org/linux-cifs/[email protected]
Thanks,
Huiwen He
Huiwen He (3):
smb/client: always return a value for FS_IOC_GETFLAGS smb/client: use writable handle for FS_IOC_SETFLAGS compression smb/client: allow FS_IOC_SETFLAGS to clear compression
fs/smb/client/cifsglob.h | 2 +- fs/smb/client/cifssmb.c | 4 +- fs/smb/client/ioctl.c | 153 ++++++++++++++++++++++++++++++++++---- fs/smb/client/smb1ops.c | 5 +- fs/smb/client/smb1proto.h | 2 +- fs/smb/client/smb2ops.c | 4 +- fs/smb/client/smb2pdu.c | 6 +- fs/smb/client/smb2proto.h | 3 +- 8 files changed, 153 insertions(+), 26 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 smb/client: always return a value for FS_IOC_GETFLAGS
From: Huiwen He <[email protected]>
Currently, repeated lsattr calls on a regular CIFS file without the compressed attribute may show random flags:
$ touch test.bin $ lsattr test.bin s-S-ia-A-EjI---------m test.bin $ lsattr test.bin ------d-cEjI---------m test.bin
The lsattr reproducer depends on the previous contents of its userspace buffer, so it may not reproduce on every setup. A deterministic reproducer is to initialize the ioctl argument before FS_IOC_GETFLAGS on a file without the compressed attribute:
int flags = 0x7fffffff;
ioctl(fd, FS_IOC_GETFLAGS, &flags);On an affected kernel, flags remains 0x7fffffff. With the fix, it is set to 0.
This happens because when the cached inode does not have the compressed bit set, the CIFS fallback path in FS_IOC_GETFLAGS returns success without calling put_user() to write the zero flags value into the user buffer. As a result, the caller observes stale contents from its own buffer.
Fix this by always writing the visible flags value back to the user buffer before returning success, even when the value is zero.
--- fs/smb/client/ioctl.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
fs/smb/client/ioctl.c +5 -7
@@ -392,13 +392,11 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg) }#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ #endif /* CONFIG_CIFS_POSIX */ - rc =0 ; -if (CIFS_I (inode)->cifsAttrs &ATTR_COMPRESSED ) { -/* add in the compressed bit */ - ExtAttrBits =FS_COMPR_FL ; - rc =put_user (ExtAttrBits &FS_FL_USER_VISIBLE , - (int __user *)arg); - } +if (CIFS_I (inode)->cifsAttrs &FILE_ATTRIBUTE_COMPRESSED ) + ExtAttrBits |=FS_COMPR_FL ; + + rc =put_user (ExtAttrBits &FS_FL_USER_VISIBLE , + (int __user *)arg);break ;case FS_IOC_SETFLAGS :if (pSMBFile ==NULL )
Signature · 1 line
2.43.0
PATCH v2 2/3 smb/client: use writable handle for FS_IOC_SETFLAGS compression
In reply to Huiwen HeFrom: Huiwen He <[email protected]>
Setting the compressed flag on a CIFS mount can fail with -EACCES:
[compress_share]
vfs objects = btrfs
$ touch test.bin
$ chattr +c test.bin
chattr: Permission denied while setting flags on test.bin
This can be reproduced against a Samba share backed by a filesystem that supports compression, such as btrfs.
FS_IOC_SETFLAGS is issued on the file handle opened by userspace. chattr opens the target read-only before setting FS_COMPR_FL, so the SMB client currently sends FSCTL_SET_COMPRESSION on a handle that may not have FILE_WRITE_DATA access. Samba requires FILE_WRITE_DATA for FSCTL_SET_COMPRESSION and rejects the request.
Use the current handle only if it already has FILE_WRITE_DATA. Otherwise try an existing writable handle for the inode. If none is available, open a temporary FILE_WRITE_DATA handle for the compression request.
After FSCTL_SET_COMPRESSION succeeds, update the cached compressed attribute immediately, matching how smb2_set_sparse() updates FILE_ATTRIBUTE_SPARSE_FILE after a successful FSCTL_SET_SPARSE.
--- fs/smb/client/ioctl.c | 116 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 4 deletions(-)
fs/smb/client/ioctl.c +112 -4
@@ -67,6 +67,110 @@ static long cifs_ioctl_query_info(unsigned int xid, struct file *filep,return rc; } +static int cifs_set_compression_by_path (unsigned int xid,struct file *filep, +struct cifs_tcon *tcon) +{ +struct inode *inode =file_inode (filep); +struct cifs_sb_info *cifs_sb =CIFS_SB (inode->i_sb); +struct TCP_Server_Info *server = tcon->ses->server; +struct cifs_open_parms oparms; +struct cifs_open_info_data data = {}; +struct cifsFileInfo *tmp_cfile =NULL ; +struct cifs_fid fid = {}; +const char *full_path; +__u32 oplock =0 ; +u64 uniqueid; +void *page; +int rc; + +if (!server->ops->open || !server->ops->close || + !server->ops->query_file_info) +return -EOPNOTSUPP ; + +if (!(cifs_sb_flags (cifs_sb) &CIFS_MOUNT_SERVER_INUM ) || + cifs_sb->mnt_cifs_serverino_autodisabled) +return -EOPNOTSUPP ; + +if (d_unhashed (filep->f_path.dentry)) +return -ESTALE ; + + page =alloc_dentry_path (); + full_path =build_path_from_dentry (filep->f_path.dentry, page); +if (IS_ERR (full_path)) { +free_dentry_path (page); +return PTR_ERR (full_path); + } + + oparms =CIFS_OPARMS (cifs_sb, tcon, full_path,FILE_WRITE_DATA | +FILE_READ_ATTRIBUTES , +FILE_OPEN ,0 ,ACL_NO_MODE ); + oparms.fid = &fid; + + rc = server->ops->open (xid, &oparms, &oplock,NULL ); +if (rc) + goto out; + + tmp_cfile =kzalloc_obj (*tmp_cfile); +if (!tmp_cfile) { + rc = -ENOMEM ; + goto close; + } + + tmp_cfile->fid = fid; + rc = server->ops->query_file_info (xid, tcon, tmp_cfile, &data); +if (rc) + goto close; + + uniqueid =le64_to_cpu (data.fi.IndexNumber); +if (uniqueid !=CIFS_I (inode)->uniqueid) { + rc = -ESTALE ; + goto close; + } + + rc = server->ops->set_compression (xid, tcon, tmp_cfile); + +close: + server->ops->close (xid, tcon, &fid); +if (tmp_cfile) +kfree (tmp_cfile); +cifs_free_open_info (&data); +out: +free_dentry_path (page); +return rc; +} + +static int cifs_ioctl_set_compression (unsigned int xid,struct file *filep, +struct cifs_tcon *tcon, +struct cifsFileInfo *cfile) +{ +struct cifsFileInfo *wfile; +struct cifs_tcon *wtcon; +struct inode *inode =file_inode (filep); +int rc; + +if (!tcon->ses->server->ops->set_compression) +return -EOPNOTSUPP ; + +if (cfile && (cfile->fid.access &FILE_WRITE_DATA )) { + rc = tcon->ses->server->ops->set_compression (xid, tcon, cfile); +if (rc != -EACCES ) +return rc; + } + + rc =cifs_get_writable_file (CIFS_I (inode),FIND_FSUID_ONLY , &wfile); +if (!rc) { + wtcon =tlink_tcon (wfile->tlink); + rc = wtcon->ses->server->ops->set_compression (xid, wtcon, wfile); +cifsFileInfo_put (wfile); +if (rc != -EACCES ) +return rc; + }else if (rc != -EBADF ) { +return rc; + } + +return cifs_set_compression_by_path (xid, filep, tcon); +} +static long cifs_ioctl_copychunk (unsigned int xid,struct file *dst_file,unsigned long srcfd) {
@@ -424,11 +528,15 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)break ;/* Try to set compress flag */ -if (tcon->ses->server->ops->set_compression) { - rc = tcon->ses->server->ops->set_compression ( - xid, tcon, pSMBFile); -cifs_dbg (FYI ,"set compress flag rc %d\n" , rc); + rc =cifs_ioctl_set_compression (xid, filep, tcon, + pSMBFile); +if (rc ==0 ) { +spin_lock (&inode->i_lock); +CIFS_I (inode)->cifsAttrs |= +FILE_ATTRIBUTE_COMPRESSED ; +spin_unlock (&inode->i_lock); } +cifs_dbg (FYI ,"set compress flag rc %d\n" , rc);break ;case CIFS_IOC_COPYCHUNK_FILE : rc =cifs_ioctl_copychunk (xid, filep, arg);
Signature · 1 line
2.43.0
PATCH v2 3/3 smb/client: allow FS_IOC_SETFLAGS to clear compression
In reply to Huiwen HeFrom: Huiwen He <[email protected]>
The CIFS FS_IOC_SETFLAGS path can set FS_COMPR_FL now, but it cannot clear it again. This can be reproduced on a share backed by a filesystem that supports compression, for example btrfs exported by Samba:
[compress_share]
vfs objects = btrfs
$ touch test.bin
$ chattr +c test.bin
$ lsattr test.bin
$ chattr -c test.bin
The final chattr -c fails with EOPNOTSUPP, and leaves the remote object with the compressed attribute still set, because the client always sends FSCTL_SET_COMPRESSION with COMPRESSION_FORMAT_DEFAULT. That is correct for setting FS_COMPR_FL, but clearing FS_COMPR_FL requires sending COMPRESSION_FORMAT_NONE.
Fix this by passing the requested compression state through the set_compression operation. The SMB1 and SMB2 helpers no longer hard-code COMPRESSION_FORMAT_DEFAULT.
When FS_COMPR_FL is set, send COMPRESSION_FORMAT_DEFAULT. When it is cleared, send COMPRESSION_FORMAT_NONE. If the server accepts the request, update the cached FILE_ATTRIBUTE_COMPRESSED bit under i_lock so FS_IOC_GETFLAGS reports the new state.
--- fs/smb/client/cifsglob.h | 2 +- fs/smb/client/cifssmb.c | 4 ++-- fs/smb/client/ioctl.c | 43 ++++++++++++++++++++++++++++----------- fs/smb/client/smb1ops.c | 5 +++-- fs/smb/client/smb1proto.h | 2 +- fs/smb/client/smb2ops.c | 4 ++-- fs/smb/client/smb2pdu.c | 6 +++--- fs/smb/client/smb2proto.h | 3 ++- 8 files changed, 45 insertions(+), 24 deletions(-)
fs/smb/client/cifsglob.h +1 -1
@@ -425,7 +425,7 @@ struct smb_version_operations {int (*set_file_info)(struct inode *,const char *,FILE_BASIC_INFO *,const unsigned int );int (*set_compression)(const unsigned int ,struct cifs_tcon *, -struct cifsFileInfo *); +struct cifsFileInfo *,__u16 );/* check if we can send an echo or nor */ bool (*can_echo)(struct TCP_Server_Info *);/* send echo request */
fs/smb/client/cifssmb.c +2 -2
@@ -3207,7 +3207,7 @@ struct inode *cifs_create_reparse_inode(struct cifs_open_info_data *data,int CIFSSMB_set_compression (const unsigned int xid,struct cifs_tcon *tcon, -__u16 fid) +__u16 fid,__u16 compression_state) {int rc =0 ;int bytes_returned;
@@ -3222,7 +3222,7 @@ CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,return rc; in_len = rc; - pSMB->compression_state =cpu_to_le16 (COMPRESSION_FORMAT_DEFAULT ); + pSMB->compression_state =cpu_to_le16 (compression_state); pSMB->TotalParameterCount =0 ; pSMB->TotalDataCount =cpu_to_le32 (2 );
fs/smb/client/ioctl.c +31 -12
96 lines not on this page · raw message
fs/smb/client/smb1ops.c +3 -2
@@ -1110,9 +1110,10 @@ smb_set_file_info(struct inode *inode, const char *full_path,static int cifs_set_compression (const unsigned int xid,struct cifs_tcon *tcon, -struct cifsFileInfo *cfile) +struct cifsFileInfo *cfile,__u16 compression_state) { -return CIFSSMB_set_compression (xid, tcon, cfile->fid.netfid); +return CIFSSMB_set_compression (xid, tcon, cfile->fid.netfid, + compression_state); } static int
fs/smb/client/smb1proto.h +1 -1
@@ -117,7 +117,7 @@ struct inode *cifs_create_reparse_inode(struct cifs_open_info_data *data,struct kvec *reparse_iov, struct kvec *xattr_iov);int CIFSSMB_set_compression (const unsigned int xid,struct cifs_tcon *tcon, -__u16 fid); +__u16 fid,__u16 compression_state); int cifs_do_get_acl(const unsigned int xid, struct cifs_tcon *tcon, const unsigned char *searchName, struct posix_acl **acl, const int acl_type, const struct nls_table *nls_codepage,
fs/smb/client/smb2ops.c +2 -2
@@ -2246,10 +2246,10 @@ smb2_duplicate_extents(const unsigned int xid,static int smb2_set_compression (const unsigned int xid,struct cifs_tcon *tcon, -struct cifsFileInfo *cfile) +struct cifsFileInfo *cfile,__u16 compression_state) {return SMB2_set_compression (xid, tcon, cfile->fid.persistent_fid, - cfile->fid.volatile_fid); + cfile->fid.volatile_fid, compression_state); } static int
fs/smb/client/smb2pdu.c +3 -3
@@ -3626,14 +3626,14 @@ SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,int SMB2_set_compression (const unsigned int xid,struct cifs_tcon *tcon, -u64 persistent_fid,u64 volatile_fid) +u64 persistent_fid,u64 volatile_fid, +__u16 compression_state) {int rc;struct compress_ioctl fsctl_input;char *ret_data =NULL ; - fsctl_input.CompressionState = -cpu_to_le16 (COMPRESSION_FORMAT_DEFAULT ); + fsctl_input.CompressionState =cpu_to_le16 (compression_state); rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid, FSCTL_SET_COMPRESSION,
fs/smb/client/smb2proto.h +2 -1
@@ -216,7 +216,8 @@ int SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid, struct smb2_file_full_ea_info *buf, int len);int SMB2_set_compression (const unsigned int xid,struct cifs_tcon *tcon, -u64 persistent_fid,u64 volatile_fid); +u64 persistent_fid,u64 volatile_fid, +__u16 compression_state);int SMB2_oplock_break (const unsigned int xid,struct cifs_tcon *tcon,const u64 persistent_fid,const u64 volatile_fid,__u8 oplock_level);
Signature · 1 line
2.43.0