cifs: Fix access_flags_to_smbopen_mode
Subsystems (2)
PATCH 1/5 cifs: Fix access_flags_to_smbopen_mode
When converting access_flags to SMBOPEN mode, check for all possible access flags, not only GENERIC_READ and GENERIC_WRITE flags.
--- fs/smb/client/cifssmb.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-)
fs/smb/client/cifssmb.c +24 -8
@@ -1024,15 +1024,31 @@ static __u16 convert_disposition(int disposition)static int access_flags_to_smbopen_mode (const int access_flags) { -int masked_flags = access_flags & (GENERIC_READ |GENERIC_WRITE ); - -if (masked_flags ==GENERIC_READ ) -return SMBOPEN_READ ; -else if (masked_flags ==GENERIC_WRITE ) +/* +* SYSTEM_SECURITY grants both read and write access to SACL, treat is as read/write. +* MAXIMUM_ALLOWED grants as many access as possible, so treat it as read/write too. +* SYNCHRONIZE as is does not grant any specific access, so do not check its mask. +* If only SYNCHRONIZE bit is specified then fallback to read access. +*/ +bool with_write_flags = access_flags & (FILE_WRITE_DATA |FILE_APPEND_DATA |FILE_WRITE_EA | +FILE_DELETE_CHILD |FILE_WRITE_ATTRIBUTES |DELETE | +WRITE_DAC |WRITE_OWNER |SYSTEM_SECURITY | +MAXIMUM_ALLOWED |GENERIC_WRITE |GENERIC_ALL ); +bool with_read_flags = access_flags & (FILE_READ_DATA |FILE_READ_EA |FILE_EXECUTE | +FILE_READ_ATTRIBUTES |READ_CONTROL | +SYSTEM_SECURITY |MAXIMUM_ALLOWED |GENERIC_ALL | +GENERIC_EXECUTE |GENERIC_READ ); +bool with_execute_flags = access_flags & (FILE_EXECUTE |MAXIMUM_ALLOWED |GENERIC_ALL | +GENERIC_EXECUTE ); + +if (with_write_flags && with_read_flags) +return SMBOPEN_READWRITE ; +else if (with_write_flags)return SMBOPEN_WRITE ; - -/* just go for read/write */ -return SMBOPEN_READWRITE ; +else if (with_execute_flags) +return SMBOPEN_EXECUTE ; +else +return SMBOPEN_READ ; }int
Signature · 1 line
2.20.1
PATCH 2/5 cifs: Do not add FILE_READ_ATTRIBUTES when using GENERIC_READ/EXECUTE/ALL
Individual bits GENERIC_READ, GENERIC_EXECUTE and GENERIC_ALL have meaning which includes also access right for FILE_READ_ATTRIBUTES. So specifying FILE_READ_ATTRIBUTES bit together with one of those GENERIC (except GENERIC_WRITE) does not do anything.
This change prevents calling additional (fallback) code and sending more requests without FILE_READ_ATTRIBUTES when the primary request fails on -EACCES, as it is not needed at all.
--- fs/smb/client/smb2file.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/smb/client/smb2file.c +9 -0
@@ -158,7 +158,16 @@ int smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32if (smb2_path ==NULL )return -ENOMEM ; +/* +* GENERIC_READ, GENERIC_EXECUTE, GENERIC_ALL and MAXIMUM_ALLOWED +* contains also FILE_READ_ATTRIBUTES access right. So do not append +* FILE_READ_ATTRIBUTES when not needed and prevent calling code path +* for retry_without_read_attributes. +*/ if (!(oparms->desired_access &FILE_READ_ATTRIBUTES ) && + !(oparms->desired_access &GENERIC_READ ) && + !(oparms->desired_access &GENERIC_EXECUTE ) && + !(oparms->desired_access &GENERIC_ALL ) && !(oparms->desired_access &MAXIMUM_ALLOWED )) { oparms->desired_access |=FILE_READ_ATTRIBUTES ; retry_without_read_attributes = true;
Signature · 1 line
2.20.1
PATCH 3/5 cifs: Improve SMB2+ stat() to work also on non-present name surrogate reparse points
In reply to Pali RohárSMB server returns STATUS_OBJECT_PATH_NOT_FOUND if the path exists, is the reparse point of name surrogate type (e.g. mount point) and surrogate target location does not exist.
So if the server returns STATUS_OBJECT_PATH_NOT_FOUND error then it is required to send request again with OPEN_REPARSE_POINT flag. This is needed to distinguish between path does not exist and path exists and points to non-existent surrogate location.
Before this change, Linux SMB client returned for non-present name surrogate reparse point -ENOENT error. With this change it correctly returns that the entry exits, it is of directory type with filled stat information.
--- fs/smb/client/smb2inode.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
fs/smb/client/smb2inode.c +12 -0
@@ -958,6 +958,18 @@ int smb2_query_path_info(const unsigned int xid,if (!hdr || out_buftype[0 ] ==CIFS_NO_BUFFER ) goto out; +/* +* SMB server returns STATUS_OBJECT_PATH_NOT_FOUND if the path exists, +* is the reparse point of name surrogate type (e.g. mount point) and +* surrogate target location does not exist. +* So if the server returns STATUS_OBJECT_PATH_NOT_FOUND error then it +* is required to send request again with OPEN_REPARSE_POINT flag. +* Re-opening with OPEN_REPARSE_POINT is done in the case when rc is +* -EOPNOTSUPP. +*/ +if (hdr->Status ==STATUS_OBJECT_PATH_NOT_FOUND ) + rc = -EOPNOTSUPP ; +switch (rc) {case 0 : rc =parse_create_response (data, cifs_sb, full_path, &out_iov[0 ]);
Signature · 1 line
2.20.1
PATCH 4/5 cifs: Improve SMB2+ stat() to work also for paths in DELETE_PENDING state
In reply to Pali RohárPaths in DELETE_PENDING state cannot be opened at all. So standard way of querying path attributes for this case is not possible.
There is an alternative way how to query limited information about file over SMB2+ dialects without opening file itself. It is by opening the parent directory, querying specific child with filled search filer and asking for attributes for that child.
Implement this fallback when standard case in smb2_query_path_info fails with STATUS_DELETE_PENDING error and stat was asked for path which is not top level one (because top level does not have parent directory at all).
--- fs/smb/client/cifsglob.h | 1 + fs/smb/client/smb2glob.h | 1 + fs/smb/client/smb2inode.c | 171 +++++++++++++++++++++++++++++++++++++- 3 files changed, 170 insertions(+), 3 deletions(-)
fs/smb/client/cifsglob.h +1 -0
@@ -2329,6 +2329,7 @@ struct smb2_compound_vars {struct smb_rqst rqst[MAX_COMPOUND ];struct kvec open_iov[SMB2_CREATE_IOV_SIZE ];struct kvec qi_iov; +struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE ];struct kvec io_iov[SMB2_IOCTL_IOV_SIZE ];struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE ];struct kvec close_iov;
fs/smb/client/smb2glob.h +1 -0
@@ -39,6 +39,7 @@ enum smb2_compound_ops {SMB2_OP_GET_REPARSE ,SMB2_OP_QUERY_WSL_EA ,SMB2_OP_OPEN_QUERY , + SMB2_OP_QUERY_DIRECTORY, };/* Used when constructing chained read requests. */
fs/smb/client/smb2inode.c +168 -3
207 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 5/5 cifs: Improve detect_directory_symlink_target() function
In reply to Pali RohárFunction detect_directory_symlink_target() is not curruntly able to detect if the target path is directory in case the path is in the DELETE_PENDING state or the user has not granted FILE_READ_ATTRIBUTES permission on the path. This limitation is written in TODO comment.
Resolve this problem by replacing code which determinate path type by the query_path_info() callback, which now is able to handle all these cases.
--- fs/smb/client/reparse.c | 75 ++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 50 deletions(-)
fs/smb/client/reparse.c +25 -50
@@ -248,18 +248,16 @@ static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb, bool *directory) {char sep =CIFS_DIR_SEP (cifs_sb); -struct cifs_open_parms oparms; +struct cifs_open_info_data query_info;struct tcon_link *tlink;struct cifs_tcon *tcon;const char *basename; -struct cifs_fid fid;char *resolved_path;int full_path_len;int basename_len;int symname_len;char *path_sep; -__u32 oplock; -int open_rc; +int query_rc;/* * First do some simple check. If the original Linux symlink target ends
@@ -282,7 +280,8 @@ static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,if (symname[0 ] =='/' ) {cifs_dbg (FYI ,"%s: cannot determinate if the symlink target path '%s' " -"is directory or not, creating '%s' as file symlink\n" , +"is directory or not because path is absolute, " +"creating '%s' as file symlink\n" , __func__, symname, full_path);return 0 ; }
@@ -320,58 +319,34 @@ static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,if (sep =='\\' )convert_delimiter (path_sep, sep); +/* +* Query resolved SMB symlink path and check if it is a directory or not. +* Callback query_path_info() already handles cases when the server does +* not grant FILE_READ_ATTRIBUTES permission for object, or when server +* denies opening the object (e.g. because of DELETE_PENDING state). +*/ tcon =tlink_tcon (tlink); - oparms =CIFS_OPARMS (cifs_sb, tcon, resolved_path, -FILE_READ_ATTRIBUTES ,FILE_OPEN ,0 ,ACL_NO_MODE ); - oparms.fid = &fid; - -/* Try to open as a directory (NOT_FILE) */ - oplock =0 ; - oparms.create_options =cifs_create_options (cifs_sb, -CREATE_NOT_FILE |OPEN_REPARSE_POINT ); - open_rc = tcon->ses->server->ops->open (xid, &oparms, &oplock,NULL ); -if (open_rc ==0 ) { -/* Successful open means that the target path is definitely a directory. */ - *directory = true; - tcon->ses->server->ops->close (xid, tcon, &fid); - }else if (open_rc == -ENOTDIR ) { -/* -ENOTDIR means that the target path is definitely a file. */ - *directory = false; - }else if (open_rc == -ENOENT ) { + query_rc = tcon->ses->server->ops->query_path_info (xid, tcon, cifs_sb, + resolved_path, &query_info); +if (query_rc ==0 ) { +/* Query on path was successful, so just check for directory attr. */ + *directory =le32_to_cpu (query_info.fi.Attributes) &ATTR_DIRECTORY ; + }else if (query_rc == -ENOENT ) {/* -ENOENT means that the target path does not exist. */ cifs_dbg (FYI ,"%s: symlink target path '%s' does not exist, " "creating '%s' as file symlink\n" , __func__, symname, full_path); }else { -/* Try to open as a file (NOT_DIR) */ - oplock =0 ; - oparms.create_options =cifs_create_options (cifs_sb, -CREATE_NOT_DIR |OPEN_REPARSE_POINT ); - open_rc = tcon->ses->server->ops->open (xid, &oparms, &oplock,NULL ); -if (open_rc ==0 ) { -/* Successful open means that the target path is definitely a file. */ - *directory = false; - tcon->ses->server->ops->close (xid, tcon, &fid); - }else if (open_rc == -EISDIR ) { -/* -EISDIR means that the target path is definitely a directory. */ - *directory = true; - }else { -/* -* This code branch is called when we do not have a permission to -* open the resolved_path or some other client/process denied -* opening the resolved_path. -* -* TODO: Try to use ops->query_dir_first on the parent directory -* of resolved_path, search for basename of resolved_path and -* check if the ATTR_DIRECTORY is set in fi.Attributes. In some -* case this could work also when opening of the path is denied. -*/ -cifs_dbg (FYI , -"%s: cannot determinate if the symlink target path '%s' " -"is directory or not, creating '%s' as file symlink\n" , - __func__, symname, full_path); - } +/* +* This code branch is called when we do not have a permission to +* query the resolved_path or some other error occurred during query. +*/ +cifs_dbg (FYI , +"%s: cannot determinate if the symlink target path '%s' " +"is directory or not because query path failed (%d), " +"creating '%s' as file symlink\n" , + __func__, symname, query_rc, full_path); }kfree (resolved_path);
Signature · 1 line
2.20.1