Skip to content

Commit b07687e

Browse files
paliSteve French
authored andcommitted
cifs: Improve SMB2+ stat() to work also without FILE_READ_ATTRIBUTES
If SMB2_OP_QUERY_INFO (called when POSIX extensions are not used) failed with STATUS_ACCESS_DENIED then it means that caller does not have permission to open the path with FILE_READ_ATTRIBUTES access and therefore cannot issue SMB2_OP_QUERY_INFO command. This will result in the -EACCES error from stat() sycall. There is an alternative way how to query limited information about path but still suitable for stat() syscall. SMB2 OPEN/CREATE operation returns in its successful response subset of query information. So try to open the path without FILE_READ_ATTRIBUTES but with MAXIMUM_ALLOWED access which will grant the maximum possible access to the file and the response will contain required query information for stat() syscall. This will improve smb2_query_path_info() to query also files which do not grant FILE_READ_ATTRIBUTES access to caller. Signed-off-by: Pali Rohár <pali@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent e255612 commit b07687e

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

fs/smb/client/smb2file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ int smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32
158158
if (smb2_path == NULL)
159159
return -ENOMEM;
160160

161-
if (!(oparms->desired_access & FILE_READ_ATTRIBUTES)) {
161+
if (!(oparms->desired_access & FILE_READ_ATTRIBUTES) &&
162+
!(oparms->desired_access & MAXIMUM_ALLOWED)) {
162163
oparms->desired_access |= FILE_READ_ATTRIBUTES;
163164
retry_without_read_attributes = true;
164165
}

fs/smb/client/smb2glob.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum smb2_compound_ops {
3838
SMB2_OP_SET_REPARSE,
3939
SMB2_OP_GET_REPARSE,
4040
SMB2_OP_QUERY_WSL_EA,
41+
SMB2_OP_OPEN_QUERY,
4142
};
4243

4344
/* Used when constructing chained read requests. */

fs/smb/client/smb2inode.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
176176
struct kvec *out_iov, int *out_buftype, struct dentry *dentry)
177177
{
178178

179+
struct smb2_create_rsp *create_rsp = NULL;
179180
struct smb2_query_info_rsp *qi_rsp = NULL;
180181
struct smb2_compound_vars *vars = NULL;
181182
__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
@@ -265,7 +266,13 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
265266
num_rqst++;
266267
rc = 0;
267268

268-
for (i = 0; i < num_cmds; i++) {
269+
i = 0;
270+
271+
/* Skip the leading explicit OPEN operation */
272+
if (num_cmds > 0 && cmds[0] == SMB2_OP_OPEN_QUERY)
273+
i++;
274+
275+
for (; i < num_cmds; i++) {
269276
/* Operation */
270277
switch (cmds[i]) {
271278
case SMB2_OP_QUERY_INFO:
@@ -640,6 +647,27 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
640647
}
641648

642649
tmp_rc = rc;
650+
651+
if (rc == 0 && num_cmds > 0 && cmds[0] == SMB2_OP_OPEN_QUERY) {
652+
create_rsp = rsp_iov[0].iov_base;
653+
idata = in_iov[0].iov_base;
654+
idata->fi.CreationTime = create_rsp->CreationTime;
655+
idata->fi.LastAccessTime = create_rsp->LastAccessTime;
656+
idata->fi.LastWriteTime = create_rsp->LastWriteTime;
657+
idata->fi.ChangeTime = create_rsp->ChangeTime;
658+
idata->fi.Attributes = create_rsp->FileAttributes;
659+
idata->fi.AllocationSize = create_rsp->AllocationSize;
660+
idata->fi.EndOfFile = create_rsp->EndofFile;
661+
if (le32_to_cpu(idata->fi.NumberOfLinks) == 0)
662+
idata->fi.NumberOfLinks = cpu_to_le32(1); /* dummy value */
663+
idata->fi.DeletePending = 0;
664+
idata->fi.Directory = !!(le32_to_cpu(create_rsp->FileAttributes) & ATTR_DIRECTORY);
665+
666+
/* smb2_parse_contexts() fills idata->fi.IndexNumber */
667+
rc = smb2_parse_contexts(server, &rsp_iov[0], &oparms->fid->epoch,
668+
oparms->fid->lease_key, &oplock, &idata->fi, NULL);
669+
}
670+
643671
for (i = 0; i < num_cmds; i++) {
644672
char *buf = rsp_iov[i + i].iov_base;
645673

@@ -978,6 +1006,43 @@ int smb2_query_path_info(const unsigned int xid,
9781006
case 0:
9791007
rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]);
9801008
break;
1009+
case -EACCES:
1010+
/*
1011+
* If SMB2_OP_QUERY_INFO (called when POSIX extensions are not used) failed with
1012+
* STATUS_ACCESS_DENIED then it means that caller does not have permission to
1013+
* open the path with FILE_READ_ATTRIBUTES access and therefore cannot issue
1014+
* SMB2_OP_QUERY_INFO command.
1015+
*
1016+
* There is an alternative way how to query limited information about path but still
1017+
* suitable for stat() syscall. SMB2 OPEN/CREATE operation returns in its successful
1018+
* response subset of query information.
1019+
*
1020+
* So try to open the path without FILE_READ_ATTRIBUTES but with MAXIMUM_ALLOWED
1021+
* access which will grant the maximum possible access to the file and the response
1022+
* will contain required query information for stat() syscall.
1023+
*/
1024+
1025+
if (tcon->posix_extensions)
1026+
break;
1027+
1028+
num_cmds = 1;
1029+
cmds[0] = SMB2_OP_OPEN_QUERY;
1030+
in_iov[0].iov_base = data;
1031+
in_iov[0].iov_len = sizeof(*data);
1032+
oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, MAXIMUM_ALLOWED,
1033+
FILE_OPEN, create_options, ACL_NO_MODE);
1034+
free_rsp_iov(out_iov, out_buftype, ARRAY_SIZE(out_iov));
1035+
rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
1036+
&oparms, in_iov, cmds, num_cmds,
1037+
cfile, out_iov, out_buftype, NULL);
1038+
1039+
hdr = out_iov[0].iov_base;
1040+
if (!hdr || out_buftype[0] == CIFS_NO_BUFFER)
1041+
goto out;
1042+
1043+
if (!rc)
1044+
rc = parse_create_response(data, cifs_sb, full_path, &out_iov[0]);
1045+
break;
9811046
case -EOPNOTSUPP:
9821047
/*
9831048
* BB TODO: When support for special files added to Samba

0 commit comments

Comments
 (0)