Skip to content

Commit b3efed6

Browse files
paliSasha Levin
authored andcommitted
cifs: Fix changing times and read-only attr over SMB1 smb_set_file_info() function
[ Upstream commit f122121 ] Function CIFSSMBSetPathInfo() is not supported by non-NT servers and returns error. Fallback code via open filehandle and CIFSSMBSetFileInfo() does not work neither because CIFS_open() works also only on NT server. Therefore currently the whole smb_set_file_info() function as a SMB1 callback for the ->set_file_info() does not work with older non-NT SMB servers, like Win9x and others. This change implements fallback code in smb_set_file_info() which will works with any server and allows to change time values and also to set or clear read-only attributes. To make existing fallback code via CIFSSMBSetFileInfo() working with also non-NT servers, it is needed to change open function from CIFS_open() (which is NT specific) to cifs_open_file() which works with any server (this is just a open wrapper function which choose the correct open function supported by the server). CIFSSMBSetFileInfo() is working also on non-NT servers, but zero time values are not treated specially. So first it is needed to fill all time values if some of them are missing, via cifs_query_path_info() call. There is another issue, opening file in write-mode (needed for changing attributes) is not possible when the file has read-only attribute set. The only option how to clear read-only attribute is via SMB_COM_SETATTR command. And opening directory is not possible neither and here the SMB_COM_SETATTR command is the only option how to change attributes. And CIFSSMBSetFileInfo() does not honor setting read-only attribute, so for setting is also needed to use SMB_COM_SETATTR command. Existing code in cifs_query_path_info() is already using SMB_COM_GETATTR as a fallback code path (function SMBQueryInformation()), so introduce a new function SMBSetInformation which will implement SMB_COM_SETATTR command. My testing showed that Windows XP SMB1 client is also using SMB_COM_SETATTR command for setting or clearing read-only attribute against non-NT server. So this can prove that this is the correct way how to do it. With this change it is possible set all 4 time values and all attributes, including clearing and setting read-only bit on non-NT SMB servers. Tested against Win98 SMB1 server. This change fixes "touch" command which was failing when called on existing file. And fixes also "chmod +w" and "chmod -w" commands which were also failing (as they are changing read-only attribute). Note that this change depends on following change "cifs: Improve cifs_query_path_info() and cifs_query_file_info()" as it require to query all 4 time attribute values. Signed-off-by: Pali Rohár <pali@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 41ba6e9 commit b3efed6

File tree

4 files changed

+166
-12
lines changed

4 files changed

+166
-12
lines changed

fs/smb/client/cifspdu.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,9 @@ typedef struct smb_com_query_information_rsp {
12661266
typedef struct smb_com_setattr_req {
12671267
struct smb_hdr hdr; /* wct = 8 */
12681268
__le16 attr;
1269-
__le16 time_low;
1270-
__le16 time_high;
1269+
__le32 last_write_time;
12711270
__le16 reserved[5]; /* must be zero */
1272-
__u16 ByteCount;
1271+
__le16 ByteCount;
12731272
__u8 BufferFormat; /* 4 = ASCII */
12741273
unsigned char fileName[];
12751274
} __attribute__((packed)) SETATTR_REQ;

fs/smb/client/cifsproto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ extern int CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon);
395395
extern int CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
396396
struct kstatfs *FSData);
397397

398+
extern int SMBSetInformation(const unsigned int xid, struct cifs_tcon *tcon,
399+
const char *fileName, __le32 attributes, __le64 write_time,
400+
const struct nls_table *nls_codepage,
401+
struct cifs_sb_info *cifs_sb);
398402
extern int CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
399403
const char *fileName, const FILE_BASIC_INFO *data,
400404
const struct nls_table *nls_codepage,

fs/smb/client/cifssmb.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5168,6 +5168,63 @@ CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
51685168
return rc;
51695169
}
51705170

5171+
int
5172+
SMBSetInformation(const unsigned int xid, struct cifs_tcon *tcon,
5173+
const char *fileName, __le32 attributes, __le64 write_time,
5174+
const struct nls_table *nls_codepage,
5175+
struct cifs_sb_info *cifs_sb)
5176+
{
5177+
SETATTR_REQ *pSMB;
5178+
SETATTR_RSP *pSMBr;
5179+
struct timespec64 ts;
5180+
int bytes_returned;
5181+
int name_len;
5182+
int rc;
5183+
5184+
cifs_dbg(FYI, "In %s path %s\n", __func__, fileName);
5185+
5186+
retry:
5187+
rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5188+
(void **) &pSMBr);
5189+
if (rc)
5190+
return rc;
5191+
5192+
if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5193+
name_len =
5194+
cifsConvertToUTF16((__le16 *) pSMB->fileName,
5195+
fileName, PATH_MAX, nls_codepage,
5196+
cifs_remap(cifs_sb));
5197+
name_len++; /* trailing null */
5198+
name_len *= 2;
5199+
} else {
5200+
name_len = copy_path_name(pSMB->fileName, fileName);
5201+
}
5202+
/* Only few attributes can be set by this command, others are not accepted by Win9x. */
5203+
pSMB->attr = cpu_to_le16(le32_to_cpu(attributes) &
5204+
(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_ARCHIVE));
5205+
/* Zero write time value (in both NT and SETATTR formats) means to not change it. */
5206+
if (le64_to_cpu(write_time) != 0) {
5207+
ts = cifs_NTtimeToUnix(write_time);
5208+
pSMB->last_write_time = cpu_to_le32(ts.tv_sec);
5209+
}
5210+
pSMB->BufferFormat = 0x04;
5211+
name_len++; /* account for buffer type byte */
5212+
inc_rfc1001_len(pSMB, (__u16)name_len);
5213+
pSMB->ByteCount = cpu_to_le16(name_len);
5214+
5215+
rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5216+
(struct smb_hdr *) pSMBr, &bytes_returned, 0);
5217+
if (rc)
5218+
cifs_dbg(FYI, "Send error in %s = %d\n", __func__, rc);
5219+
5220+
cifs_buf_release(pSMB);
5221+
5222+
if (rc == -EAGAIN)
5223+
goto retry;
5224+
5225+
return rc;
5226+
}
5227+
51715228
/* Some legacy servers such as NT4 require that the file times be set on
51725229
an open handle, rather than by pathname - this is awkward due to
51735230
potential access conflicts on the open, but it is unavoidable for these

fs/smb/client/smb1ops.c

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -894,27 +894,68 @@ smb_set_file_info(struct inode *inode, const char *full_path,
894894
struct cifs_fid fid;
895895
struct cifs_open_parms oparms;
896896
struct cifsFileInfo *open_file;
897+
FILE_BASIC_INFO new_buf;
898+
struct cifs_open_info_data query_data;
899+
__le64 write_time = buf->LastWriteTime;
897900
struct cifsInodeInfo *cinode = CIFS_I(inode);
898901
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
899902
struct tcon_link *tlink = NULL;
900903
struct cifs_tcon *tcon;
901904

902905
/* if the file is already open for write, just use that fileid */
903906
open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
907+
904908
if (open_file) {
905909
fid.netfid = open_file->fid.netfid;
906910
netpid = open_file->pid;
907911
tcon = tlink_tcon(open_file->tlink);
908-
goto set_via_filehandle;
912+
} else {
913+
tlink = cifs_sb_tlink(cifs_sb);
914+
if (IS_ERR(tlink)) {
915+
rc = PTR_ERR(tlink);
916+
tlink = NULL;
917+
goto out;
918+
}
919+
tcon = tlink_tcon(tlink);
909920
}
910921

911-
tlink = cifs_sb_tlink(cifs_sb);
912-
if (IS_ERR(tlink)) {
913-
rc = PTR_ERR(tlink);
914-
tlink = NULL;
915-
goto out;
922+
/*
923+
* Non-NT servers interprets zero time value in SMB_SET_FILE_BASIC_INFO
924+
* over TRANS2_SET_FILE_INFORMATION as a valid time value. NT servers
925+
* interprets zero time value as do not change existing value on server.
926+
* API of ->set_file_info() callback expects that zero time value has
927+
* the NT meaning - do not change. Therefore if server is non-NT and
928+
* some time values in "buf" are zero, then fetch missing time values.
929+
*/
930+
if (!(tcon->ses->capabilities & CAP_NT_SMBS) &&
931+
(!buf->CreationTime || !buf->LastAccessTime ||
932+
!buf->LastWriteTime || !buf->ChangeTime)) {
933+
rc = cifs_query_path_info(xid, tcon, cifs_sb, full_path, &query_data);
934+
if (rc) {
935+
if (open_file) {
936+
cifsFileInfo_put(open_file);
937+
open_file = NULL;
938+
}
939+
goto out;
940+
}
941+
/*
942+
* Original write_time from buf->LastWriteTime is preserved
943+
* as SMBSetInformation() interprets zero as do not change.
944+
*/
945+
new_buf = *buf;
946+
buf = &new_buf;
947+
if (!buf->CreationTime)
948+
buf->CreationTime = query_data.fi.CreationTime;
949+
if (!buf->LastAccessTime)
950+
buf->LastAccessTime = query_data.fi.LastAccessTime;
951+
if (!buf->LastWriteTime)
952+
buf->LastWriteTime = query_data.fi.LastWriteTime;
953+
if (!buf->ChangeTime)
954+
buf->ChangeTime = query_data.fi.ChangeTime;
916955
}
917-
tcon = tlink_tcon(tlink);
956+
957+
if (open_file)
958+
goto set_via_filehandle;
918959

919960
rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
920961
cifs_sb);
@@ -935,15 +976,53 @@ smb_set_file_info(struct inode *inode, const char *full_path,
935976
.fid = &fid,
936977
};
937978

938-
cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for times not supported by this server\n");
939-
rc = CIFS_open(xid, &oparms, &oplock, NULL);
979+
if (S_ISDIR(inode->i_mode) && !(tcon->ses->capabilities & CAP_NT_SMBS)) {
980+
/* Opening directory path is not possible on non-NT servers. */
981+
rc = -EOPNOTSUPP;
982+
} else {
983+
/*
984+
* Use cifs_open_file() instead of CIFS_open() as the
985+
* cifs_open_file() selects the correct function which
986+
* works also on non-NT servers.
987+
*/
988+
rc = cifs_open_file(xid, &oparms, &oplock, NULL);
989+
/*
990+
* Opening path for writing on non-NT servers is not
991+
* possible when the read-only attribute is already set.
992+
* Non-NT server in this case returns -EACCES. For those
993+
* servers the only possible way how to clear the read-only
994+
* bit is via SMB_COM_SETATTR command.
995+
*/
996+
if (rc == -EACCES &&
997+
(cinode->cifsAttrs & ATTR_READONLY) &&
998+
le32_to_cpu(buf->Attributes) != 0 && /* 0 = do not change attrs */
999+
!(le32_to_cpu(buf->Attributes) & ATTR_READONLY) &&
1000+
!(tcon->ses->capabilities & CAP_NT_SMBS))
1001+
rc = -EOPNOTSUPP;
1002+
}
1003+
1004+
/* Fallback to SMB_COM_SETATTR command when absolutelty needed. */
1005+
if (rc == -EOPNOTSUPP) {
1006+
cifs_dbg(FYI, "calling SetInformation since SetPathInfo for attrs/times not supported by this server\n");
1007+
rc = SMBSetInformation(xid, tcon, full_path,
1008+
buf->Attributes != 0 ? buf->Attributes : cpu_to_le32(cinode->cifsAttrs),
1009+
write_time,
1010+
cifs_sb->local_nls, cifs_sb);
1011+
if (rc == 0)
1012+
cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1013+
else
1014+
rc = -EACCES;
1015+
goto out;
1016+
}
1017+
9401018
if (rc != 0) {
9411019
if (rc == -EIO)
9421020
rc = -EINVAL;
9431021
goto out;
9441022
}
9451023

9461024
netpid = current->tgid;
1025+
cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for attrs/times not supported by this server\n");
9471026

9481027
set_via_filehandle:
9491028
rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
@@ -954,6 +1033,21 @@ smb_set_file_info(struct inode *inode, const char *full_path,
9541033
CIFSSMBClose(xid, tcon, fid.netfid);
9551034
else
9561035
cifsFileInfo_put(open_file);
1036+
1037+
/*
1038+
* Setting the read-only bit is not honered on non-NT servers when done
1039+
* via open-semantics. So for setting it, use SMB_COM_SETATTR command.
1040+
* This command works only after the file is closed, so use it only when
1041+
* operation was called without the filehandle.
1042+
*/
1043+
if (open_file == NULL &&
1044+
!(tcon->ses->capabilities & CAP_NT_SMBS) &&
1045+
le32_to_cpu(buf->Attributes) & ATTR_READONLY) {
1046+
SMBSetInformation(xid, tcon, full_path,
1047+
buf->Attributes,
1048+
0 /* do not change write time */,
1049+
cifs_sb->local_nls, cifs_sb);
1050+
}
9571051
out:
9581052
if (tlink != NULL)
9591053
cifs_put_tlink(tlink);

0 commit comments

Comments
 (0)