Skip to contentSkip to conversation

cifs: keep BCC in little-endian format (try #2)

PATCH In review Jeff Layton 2 participants 4 messages Apr 29, 2011 – May 4, 2011 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cifs: keep BCC in little-endian format (try #2)

This is the same patch as originally posted, just with some merge conflicts fixed up for some patches that went late into 2.6.39.

Currently, the ByteCount is usually converted to host-endian on receive. This is confusing however, as we need to keep two sets of routines for accessing it, and keep track of when to use each routine. Munging received packets like this also limits when the signature can be calulated.

Simplify the code by keeping the received ByteCount in little-endian format. This allows us to eliminate a set of routines for accessing it and we can now drop the *_le suffixes from the accessor functions since that's now implied.

While we're at it, switch all of the places that read the ByteCount directly to use the get_bcc inline which should also clean up some unaligned accesses.

---
 fs/cifs/cifs_debug.c |    2 +-
 fs/cifs/cifspdu.h    |   22 +----------------
 fs/cifs/cifsproto.h  |    1 -
 fs/cifs/cifssmb.c    |   61 +++++++++++++++++++++++++------------------------
 fs/cifs/connect.c    |    4 +-
 fs/cifs/misc.c       |    4 +-
 fs/cifs/netmisc.c    |    7 -----
 fs/cifs/sess.c       |    2 +-
 fs/cifs/transport.c  |   19 +--------------
 9 files changed, 40 insertions(+), 82 deletions(-)
fs/cifs/cifs_debug.c +1 -1
index 30d01bc..18f4272 100644
@@ -63,7 +63,7 @@ void cifs_dump_detail(struct smb_hdr *smb)
 	cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Flgs2: 0x%x Mid: %d Pid: %d",
 		  smb->Command, smb->Status.CifsError,
 		  smb->Flags, smb->Flags2, smb->Mid, smb->Pid);
-	cERROR(1, "smb buf %p len %d", smb, smbCalcSize_LE(smb));
+	cERROR(1, "smb buf %p len %d", smb, smbCalcSize(smb));
 }
 
 
fs/cifs/cifspdu.h +2 -20
index b5c8cc5..e53c6b3 100644
@@ -435,36 +435,18 @@ struct smb_hdr {
 /* given a pointer to an smb_hdr retrieve the pointer to the byte area */
 #define pByteArea(smb_var) (BCC(smb_var) + 2)
 
-/* get the converted ByteCount for a SMB packet and return it */
-static inline __u16
-get_bcc(struct smb_hdr *hdr)
-{
-	__u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-	return get_unaligned(bc_ptr);
-}
-
 /* get the unconverted ByteCount for a SMB packet and return it */
 static inline __u16
-get_bcc_le(struct smb_hdr *hdr)
+get_bcc(struct smb_hdr *hdr)
 {
 	__le16 *bc_ptr = (__le16 *)BCC(hdr);
 
 	return get_unaligned_le16(bc_ptr);
 }
 
-/* set the ByteCount for a SMB packet in host-byte order */
-static inline void
-put_bcc(__u16 count, struct smb_hdr *hdr)
-{
-	__u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-	put_unaligned(count, bc_ptr);
-}
-
 /* set the ByteCount for a SMB packet in little-endian */
 static inline void
-put_bcc_le(__u16 count, struct smb_hdr *hdr)
+put_bcc(__u16 count, struct smb_hdr *hdr)
 {
 	__le16 *bc_ptr = (__le16 *)BCC(hdr);
 
fs/cifs/cifsproto.h +0 -1
index 0e4e057..0cdf375 100644
@@ -90,7 +90,6 @@ extern void cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
 extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
 extern unsigned int smbCalcSize(struct smb_hdr *ptr);
-extern unsigned int smbCalcSize_LE(struct smb_hdr *ptr);
 extern int decode_negTokenInit(unsigned char *security_blob, int length,
 			struct TCP_Server_Info *server);
 extern int cifs_convert_address(struct sockaddr *dst, const char *src, int len);
fs/cifs/cifssmb.c +31 -30

226 lines not on this page · raw message

fs/cifs/connect.c +2 -2
index 9d05d8f..68bd894 100644
@@ -316,12 +316,12 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
 	put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
 
 	/* fix up the BCC */
-	byte_count = get_bcc_le(pTargetSMB);
+	byte_count = get_bcc(pTargetSMB);
 	byte_count += total_in_buf2;
 	/* is the result too big for the field? */
 	if (byte_count > USHRT_MAX)
 		return -EPROTO;
-	put_bcc_le(byte_count, pTargetSMB);
+	put_bcc(byte_count, pTargetSMB);
 
 	byte_count = pTargetSMB->smb_buf_length;
 	byte_count += total_in_buf2;
fs/cifs/misc.c +2 -2
index 0c684ae..a0765e6 100644
@@ -464,7 +464,7 @@ checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length)
 
 	if (check_smb_hdr(smb, mid))
 		return 1;
-	clc_len = smbCalcSize_LE(smb);
+	clc_len = smbCalcSize(smb);
 
 	if (4 + len != length) {
 		cERROR(1, "Length read does not match RFC1001 length %d",
@@ -521,7 +521,7 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)
 			(struct smb_com_transaction_change_notify_rsp *)buf;
 		struct file_notify_information *pnotify;
 		__u32 data_offset = 0;
-		if (get_bcc_le(buf) > sizeof(struct file_notify_information)) {
+		if (get_bcc(buf) > sizeof(struct file_notify_information)) {
 			data_offset = le32_to_cpu(pSMBr->DataOffset);
 
 			pnotify = (struct file_notify_information *)
fs/cifs/netmisc.c +0 -7
index 79f641e..79b71c2 100644
@@ -919,13 +919,6 @@ smbCalcSize(struct smb_hdr *ptr)
 		2 /* size of the bcc field */ + get_bcc(ptr));
 }
 
-unsigned int
-smbCalcSize_LE(struct smb_hdr *ptr)
-{
-	return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
-		2 /* size of the bcc field */ + get_bcc_le(ptr));
-}
-
 /* The following are taken from fs/ntfs/util.c */
 
 #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
fs/cifs/sess.c +1 -1
index b6ff84a..74344ef 100644
@@ -861,7 +861,7 @@ ssetup_ntlmssp_authenticate:
 	count = iov[1].iov_len + iov[2].iov_len;
 	smb_buf->smb_buf_length += count;
 
-	put_bcc_le(count, smb_buf);
+	put_bcc(count, smb_buf);
 
 	rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
 			  CIFS_LOG_ERROR);
fs/cifs/transport.c +1 -18
index 46d8756..44f4f49 100644
@@ -491,7 +491,7 @@ send_nt_cancel(struct TCP_Server_Info *server, struct smb_hdr *in_buf,
 	in_buf->smb_buf_length = sizeof(struct smb_hdr) - 4  + 2;
 	in_buf->Command = SMB_COM_NT_CANCEL;
 	in_buf->WordCount = 0;
-	put_bcc_le(0, in_buf);
+	put_bcc(0, in_buf);
 
 	mutex_lock(&server->srv_mutex);
 	rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
@@ -651,11 +651,6 @@ SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
 		rc = map_smb_to_linux_error(midQ->resp_buf,
 					    flags & CIFS_LOG_ERROR);
 
-		/* convert ByteCount if necessary */
-		if (receive_len >= sizeof(struct smb_hdr) - 4
-		    /* do not count RFC1001 header */  +
-		    (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
-			put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
 		if ((flags & CIFS_NO_RESP) == 0)
 			midQ->resp_buf = NULL;  /* mark it so buf will
 						   not be freed by
@@ -804,12 +799,6 @@ SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
 
 		/* BB special case reconnect tid and uid here? */
 		rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
-
-		/* convert ByteCount if necessary */
-		if (receive_len >= sizeof(struct smb_hdr) - 4
-		    /* do not count RFC1001 header */  +
-		    (2 * out_buf->WordCount) + 2 /* bcc */ )
-			put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
 	} else {
 		rc = -EIO;
 		cERROR(1, "Bad MID state?");
@@ -1017,12 +1006,6 @@ SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
 	/* BB special case reconnect tid and uid here? */
 	rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
 
-	/* convert ByteCount if necessary */
-	if (receive_len >= sizeof(struct smb_hdr) - 4
-	    /* do not count RFC1001 header */  +
-	    (2 * out_buf->WordCount) + 2 /* bcc */ )
-		put_bcc(get_bcc_le(out_buf), out_buf);
-
 out:
 	delete_mid(midQ);
 	if (rstart && rc == -EACCES)
Signature · 1 line
1.7.4.4

A message that is not in the archive (sent off-list or not yet received): 1304087165-11427-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org

#2 lore raw

2011/4/29 Jeff Layton <[email protected]>:

Quoted 475 lines

This is the same patch as originally posted, just with some merge conflicts fixed up for some patches that went late into 2.6.39.

Currently, the ByteCount is usually converted to host-endian on receive. This is confusing however, as we need to keep two sets of routines for accessing it, and keep track of when to use each routine. Munging received packets like this also limits when the signature can be calulated.

Simplify the code by keeping the received ByteCount in little-endian format. This allows us to eliminate a set of routines for accessing it and we can now drop the *_le suffixes from the accessor functions since that's now implied.

While we're at it, switch all of the places that read the ByteCount directly to use the get_bcc inline which should also clean up some unaligned accesses.

---
 fs/cifs/cifs_debug.c |    2 +-
 fs/cifs/cifspdu.h    |   22 +---------------- fs/cifs/cifsproto.h  |    1 -
 fs/cifs/cifssmb.c    |   61 +++++++++++++++++++++++++------------------------ fs/cifs/connect.c    |    4 +-
 fs/cifs/misc.c       |    4 +-
 fs/cifs/netmisc.c    |    7 -----
 fs/cifs/sess.c       |    2 +-
 fs/cifs/transport.c  |   19 +--------------

 9 files changed, 40 insertions(+), 82 deletions(-)
fs/cifs/cifs_debug.c +1 -1
index 30d01bc..18f4272 100644
@@ -63,7 +63,7 @@ void cifs_dump_detail(struct smb_hdr *smb)
        cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Flgs2: 0x%x Mid: %d Pid: %d",
                  smb->Command, smb->Status.CifsError,
                  smb->Flags, smb->Flags2, smb->Mid, smb->Pid);
-       cERROR(1, "smb buf %p len %d", smb, smbCalcSize_LE(smb));
+       cERROR(1, "smb buf %p len %d", smb, smbCalcSize(smb));
  }
 
 
fs/cifs/cifspdu.h +0 -9
index b5c8cc5..e53c6b3 100644
@@ -435,36 +435,18 @@ struct smb_hdr {
  /* given a pointer to an smb_hdr retrieve the pointer to the byte area */
  #define pByteArea(smb_var) (BCC(smb_var) + 2)
 
-/* get the converted ByteCount for a SMB packet and return it */
-static inline __u16
-get_bcc(struct smb_hdr *hdr)
-{
-       __u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-       return get_unaligned(bc_ptr);
-}
-
  /* get the unconverted ByteCount for a SMB packet and return it */

 static inline __u16

fs/cifs/cifspdu.h +1 -1
-get_bcc_le(struct smb_hdr *hdr)
+get_bcc(struct smb_hdr *hdr)

 {
       __le16 *bc_ptr = (__le16 *)BCC(hdr);

       return get_unaligned_le16(bc_ptr);
 }

fs/cifs/cifspdu.h +0 -9
-/* set the ByteCount for a SMB packet in host-byte order */
-static inline void
-put_bcc(__u16 count, struct smb_hdr *hdr)
-{
-       __u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-       put_unaligned(count, bc_ptr);
-}
-

 /* set the ByteCount for a SMB packet in little-endian */ static inline void

fs/cifs/cifspdu.h +1 -1
-put_bcc_le(__u16 count, struct smb_hdr *hdr)
+put_bcc(__u16 count, struct smb_hdr *hdr)

 {
       __le16 *bc_ptr = (__le16 *)BCC(hdr);

fs/cifs/cifsproto.h +0 -1
index 0e4e057..0cdf375 100644
@@ -90,7 +90,6 @@ extern void cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
  extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
  extern unsigned int smbCalcSize(struct smb_hdr *ptr);
-extern unsigned int smbCalcSize_LE(struct smb_hdr *ptr);
  extern int decode_negTokenInit(unsigned char *security_blob, int length,
                        struct TCP_Server_Info *server);
  extern int cifs_convert_address(struct sockaddr *dst, const char *src, int len);
fs/cifs/cifssmb.c +0 -0
index 2710f00..5ae00d5 100644
@@ -575,7 +575,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
 

       if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC) && (server->capabilities & CAP_EXTENDED_SECURITY)) {
-               count = pSMBr->ByteCount;
+               count = get_bcc(&pSMBr->hdr);
               if (count < 16) {
                       rc = -EIO;
                       goto neg_err_exit;

fs/cifs/cifssmb.c +2 -2
@@ -729,7 +729,7 @@ CIFSSMBEcho(struct TCP_Server_Info *server)
        smb->hdr.Tid = 0xffff;
        smb->hdr.WordCount = 1;
        put_unaligned_le16(1, &smb->EchoCount);
-       put_bcc_le(1, &smb->hdr);
+       put_bcc(1, &smb->hdr);
        smb->Data[0] = 'a';
        smb->hdr.smb_buf_length += 3;
 
@@ -1072,7 +1072,7 @@ PsxCreat:
        cFYI(1, "copying inode info");
        rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-       if (rc || (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP))) {
+       if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {

               rc = -EIO;      /* bad smb */
               goto psx_create_err;
       }

fs/cifs/cifssmb.c +0 -0
@@ -1093,7 +1093,7 @@ PsxCreat:

               pRetData->Type = cpu_to_le32(-1); /* unknown */ cFYI(DBG2, "unknown type");
       } else {
-               if (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP)
+               if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
                                       + sizeof(FILE_UNIX_BASIC_INFO)) { cERROR(1, "Open response data too small"); pRetData->Type = cpu_to_le32(-1);

fs/cifs/cifssmb.c +1 -1
@@ -1859,7 +1859,7 @@ CIFSSMBPosixLock(const int xid, struct cifsTconInfo *tcon,
                __u16 data_count;
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(struct cifs_posix_lock))) {
+               if (rc || (get_bcc(&pSMBr->hdr) < sizeof(struct cifs_posix_lock))) {

                       rc = -EIO;      /* bad smb */ goto plk_err_exit;
               }

fs/cifs/cifssmb.c +3 -3
@@ -2486,7 +2486,7 @@ querySymLinkRetry:
 
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
                /* BB also check enough total bytes returned */
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                        rc = -EIO;
                else {
                        bool is_unicode;
@@ -2568,14 +2568,14 @@ CIFSSMBQueryReparseLinkInfo(const int xid, struct cifsTconInfo *tcon,
        } else {                /* decode response */
                __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
                __u32 data_count = le32_to_cpu(pSMBr->DataCount);
-               if ((pSMBr->ByteCount < 2) || (data_offset > 512)) {
-               /* BB also check enough total bytes returned */
+               if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
+                       /* BB also check enough total bytes returned */

                       rc = -EIO;      /* bad smb */
                       goto qreparse_out;
               }
               if (data_count && (data_count < 2048)) {
                       char *end_of_smb = 2 /* sizeof byte count */ +
-                               pSMBr->ByteCount + (char *)&pSMBr->ByteCount;
+                               get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;

                       struct reparse_data *reparse_buf = (struct reparse_data *)

fs/cifs/cifssmb.c +1 -1
@@ -2833,7 +2833,7 @@ queryAclRetry:
                /* decode response */
 
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                /* BB also check enough total bytes returned */

                       rc = -EIO;      /* bad smb */ else {

fs/cifs/cifssmb.c +1 -1
@@ -2983,7 +2983,7 @@ GetExtAttrRetry:
        } else {
                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                /* BB also check enough total bytes returned */
                        /* If rc should we check for EOPNOSUPP and

                          disable the srvino flag? or in caller? */

fs/cifs/cifssmb.c +4 -3
@@ -3059,6 +3059,7 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
        char *end_of_smb;
        __u32 data_count, data_offset, parm_count, parm_offset;
        struct smb_com_ntransact_rsp *pSMBr;
+       u16 bcc;
 
        *pdatalen = 0;
        *pparmlen = 0;
@@ -3068,8 +3069,8 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
 
        pSMBr = (struct smb_com_ntransact_rsp *)buf;
 
-       /* ByteCount was converted from little endian in SendReceive */
-       end_of_smb = 2 /* sizeof byte count */ + pSMBr->ByteCount +
+       bcc = get_bcc(&pSMBr->hdr);
+       end_of_smb = 2 /* sizeof byte count */ + bcc +
                        (char *)&pSMBr->ByteCount;
 
        data_offset = le32_to_cpu(pSMBr->DataOffset);
@@ -3095,7 +3096,7 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
                        *ppdata, data_count, (data_count + *ppdata),
                        end_of_smb, pSMBr);
                return -EINVAL;
-       } else if (parm_count + data_count > pSMBr->ByteCount) {
+       } else if (parm_count + data_count > bcc) {
                cFYI(1, "parm count and data count larger than SMB");
                return -EINVAL;
        }
@@ -3382,7 +3383,7 @@ QFileInfoRetry:
 

               if (rc) /* BB add auto retry on EOPNOTSUPP? */
                       rc = -EIO;
-               else if (pSMBr->ByteCount < 40)
+               else if (get_bcc(&pSMBr->hdr) < 40)
                       rc = -EIO;      /* bad smb */
               else if (pFindData) {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +0 -0
@@ -3470,9 +3471,9 @@ QPathInfoRetry:
 

               if (rc) /* BB add auto retry on EOPNOTSUPP? */ rc = -EIO;
-               else if (!legacy && (pSMBr->ByteCount < 40))
+               else if (!legacy && get_bcc(&pSMBr->hdr) < 40) rc = -EIO;      /* bad smb */
-               else if (legacy && (pSMBr->ByteCount < 24))
+               else if (legacy && get_bcc(&pSMBr->hdr) < 24) rc = -EIO;  /* 24 or 26 expected but we do not read last field */
               else if (pFindData) {

fs/cifs/cifssmb.c +1 -1
@@ -3548,7 +3549,7 @@ UnixQFileInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
+               if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {

                       cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n" "Unix Extensions can be disabled on mount " "by specifying the nosfu mount option.");

fs/cifs/cifssmb.c +1 -1
@@ -3634,7 +3635,7 @@ UnixQPathInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
+               if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {

                       cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n" "Unix Extensions can be disabled on mount " "by specifying the nosfu mount option.");

fs/cifs/cifssmb.c +1 -1
@@ -4039,7 +4040,7 @@ GetInodeNumberRetry:
        } else {
                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                /* BB also check enough total bytes returned */
                        /* If rc should we check for EOPNOSUPP and

                       disable the srvino flag? or in caller? */

fs/cifs/cifssmb.c +1 -1
@@ -4265,13 +4266,13 @@ getDFSRetry:
        rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
        /* BB Also check if enough total bytes returned? */
-       if (rc || (pSMBr->ByteCount < 17)) {
+       if (rc || get_bcc(&pSMBr->hdr) < 17) {

               rc = -EIO;      /* bad smb */
               goto GetDFSRefExit;
       }

       cFYI(1, "Decoding GetDFSRefer response BCC: %d  Offset %d",
-                               pSMBr->ByteCount,
+                               get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));

       /* parse returned result into more usable form */

fs/cifs/cifssmb.c +1 -1
@@ -4337,12 +4338,12 @@ oldQFSInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 18))
+               if (rc || get_bcc(&pSMBr->hdr) < 18)

                       rc = -EIO;      /* bad smb */
               else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); cFYI(1, "qfsinf resp BCC: %d  Offset %d",
-                                pSMBr->ByteCount, data_offset);
+                                get_bcc(&pSMBr->hdr), data_offset);

                       response_data = (FILE_SYSTEM_ALLOC_INFO *) (((char *) &pSMBr->hdr.Protocol) + data_offset);

fs/cifs/cifssmb.c +1 -1
@@ -4416,7 +4417,7 @@ QFSInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 24))
+               if (rc || get_bcc(&pSMBr->hdr) < 24)

                       rc = -EIO;      /* bad smb */
               else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +1 -1
@@ -4496,7 +4497,7 @@ QFSAttributeRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 13)) {
+               if (rc || get_bcc(&pSMBr->hdr) < 13) {
                        /* BB also check if enough bytes returned */

                       rc = -EIO;      /* bad smb */ } else {

fs/cifs/cifssmb.c +1 -1
@@ -4567,7 +4568,7 @@ QFSDeviceRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(FILE_SYSTEM_DEVICE_INFO)))
+               if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_SYSTEM_DEVICE_INFO))

                       rc = -EIO;      /* bad smb */
               else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +1 -1
@@ -4636,7 +4637,7 @@ QFSUnixRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 13)) {
+               if (rc || get_bcc(&pSMBr->hdr) < 13) {

                       rc = -EIO;      /* bad smb */
               } else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +1 -1
@@ -4781,7 +4782,7 @@ QFSPosixRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 13)) {
+               if (rc || get_bcc(&pSMBr->hdr) < 13) {

                       rc = -EIO;      /* bad smb */
               } else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +0 -0
@@ -5510,7 +5511,7 @@ QAllEAsRetry:

       of these trans2 responses */

       rc = validate_t2((struct smb_t2_rsp *)pSMBr);

-       if (rc || (pSMBr->ByteCount < 4)) {
+       if (rc || get_bcc(&pSMBr->hdr) < 4) {

               rc = -EIO;      /* bad smb */
               goto QAllEAsOut;
       }

fs/cifs/connect.c +2 -2
index 9d05d8f..68bd894 100644
@@ -316,12 +316,12 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
        put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
 
        /* fix up the BCC */
-       byte_count = get_bcc_le(pTargetSMB);
+       byte_count = get_bcc(pTargetSMB);
        byte_count += total_in_buf2;
        /* is the result too big for the field? */
        if (byte_count > USHRT_MAX)
                return -EPROTO;
-       put_bcc_le(byte_count, pTargetSMB);
+       put_bcc(byte_count, pTargetSMB);
 
        byte_count = pTargetSMB->smb_buf_length;
        byte_count += total_in_buf2;
fs/cifs/misc.c +2 -2
index 0c684ae..a0765e6 100644
@@ -464,7 +464,7 @@ checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length)
 
        if (check_smb_hdr(smb, mid))
                return 1;
-       clc_len = smbCalcSize_LE(smb);
+       clc_len = smbCalcSize(smb);
 
        if (4 + len != length) {
                cERROR(1, "Length read does not match RFC1001 length %d",
@@ -521,7 +521,7 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)
                        (struct smb_com_transaction_change_notify_rsp *)buf;
                struct file_notify_information *pnotify;
                __u32 data_offset = 0;
-               if (get_bcc_le(buf) > sizeof(struct file_notify_information)) {
+               if (get_bcc(buf) > sizeof(struct file_notify_information)) {
                        data_offset = le32_to_cpu(pSMBr->DataOffset);
 
                        pnotify = (struct file_notify_information *)
fs/cifs/netmisc.c +0 -7
index 79f641e..79b71c2 100644
@@ -919,13 +919,6 @@ smbCalcSize(struct smb_hdr *ptr)
                2 /* size of the bcc field */ + get_bcc(ptr));
  }
 
-unsigned int
-smbCalcSize_LE(struct smb_hdr *ptr)
-{
-       return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
-               2 /* size of the bcc field */ + get_bcc_le(ptr));
-}
-
  /* The following are taken from fs/ntfs/util.c */
 
  #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
fs/cifs/sess.c +1 -1
index b6ff84a..74344ef 100644
@@ -861,7 +861,7 @@ ssetup_ntlmssp_authenticate:
        count = iov[1].iov_len + iov[2].iov_len;
        smb_buf->smb_buf_length += count;
 
-       put_bcc_le(count, smb_buf);
+       put_bcc(count, smb_buf);
 
        rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
                          CIFS_LOG_ERROR);
fs/cifs/transport.c +1 -6
index 46d8756..44f4f49 100644
@@ -491,7 +491,7 @@ send_nt_cancel(struct TCP_Server_Info *server, struct smb_hdr *in_buf,
        in_buf->smb_buf_length = sizeof(struct smb_hdr) - 4  + 2;
        in_buf->Command = SMB_COM_NT_CANCEL;
        in_buf->WordCount = 0;
-       put_bcc_le(0, in_buf);
+       put_bcc(0, in_buf);
 
        mutex_lock(&server->srv_mutex);
        rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
@@ -651,11 +651,6 @@ SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
                rc = map_smb_to_linux_error(midQ->resp_buf,
                                            flags & CIFS_LOG_ERROR);
 
-               /* convert ByteCount if necessary */
-               if (receive_len >= sizeof(struct smb_hdr) - 4
-                   /* do not count RFC1001 header */  +
-                   (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
-                       put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
                if ((flags & CIFS_NO_RESP) == 0)

                       midQ->resp_buf = NULL;  /* mark it so buf will not be freed by

fs/cifs/transport.c +0 -12
@@ -804,12 +799,6 @@ SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
 
                /* BB special case reconnect tid and uid here? */
                rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
-
-               /* convert ByteCount if necessary */
-               if (receive_len >= sizeof(struct smb_hdr) - 4
-                   /* do not count RFC1001 header */  +
-                   (2 * out_buf->WordCount) + 2 /* bcc */ )
-                       put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
        } else {
                rc = -EIO;
                cERROR(1, "Bad MID state?");
@@ -1017,12 +1006,6 @@ SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
        /* BB special case reconnect tid and uid here? */
        rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
 
-       /* convert ByteCount if necessary */
-       if (receive_len >= sizeof(struct smb_hdr) - 4
-           /* do not count RFC1001 header */  +
-           (2 * out_buf->WordCount) + 2 /* bcc */ )
-               put_bcc(get_bcc_le(out_buf), out_buf);
-
Quoted 5 lines

 out:
       delete_mid(midQ);
       if (rstart && rc == -EACCES)
--
1.7.4.4

This patch has checkpatch warnings:
WARNING: line over 80 characters
#152: FILE: fs/cifs/cifssmb.c:1870:
+ if (rc || (get_bcc(&pSMBr->hdr) < sizeof(struct cifs_posix_lock))) {

WARNING: line over 80 characters
#179: FILE: fs/cifs/cifssmb.c:2586:
+ get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;

WARNING: suspect code indent for conditional statements (16, 16) #188: FILE: fs/cifs/cifssmb.c:2844:
+ if (rc || get_bcc(&pSMBr->hdr) < 2)
/* BB also check enough total bytes returned */

WARNING: suspect code indent for conditional statements (16, 16) #197: FILE: fs/cifs/cifssmb.c:2994:
+ if (rc || get_bcc(&pSMBr->hdr) < 2)
/* BB also check enough total bytes returned */

WARNING: suspect code indent for conditional statements (16, 16) #273: FILE: fs/cifs/cifssmb.c:4050:
+ if (rc || get_bcc(&pSMBr->hdr) < 2)
/* BB also check enough total bytes returned */

WARNING: line over 80 characters
#331: FILE: fs/cifs/cifssmb.c:4578:
+ if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_SYSTEM_DEVICE_INFO))

total: 0 errors, 6 warnings, 373 lines checked

Signature · 2 lines
Best regards,
Pavel Shilovsky.

A message that is not in the archive (sent off-list or not yet received): BANLkTimzhtC9ZPbXS3tpdF380V0M0+YHxw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#4 lore raw
Quoted 510 lines · On Wed, 4 May 2011 09:11:15 +0400 Pavel Shilovsky…

2011/4/29 Jeff Layton <[email protected]>:

Quoted 475 lines

This is the same patch as originally posted, just with some merge conflicts fixed up for some patches that went late into 2.6.39.

Currently, the ByteCount is usually converted to host-endian on receive. This is confusing however, as we need to keep two sets of routines for accessing it, and keep track of when to use each routine. Munging received packets like this also limits when the signature can be calulated.

Simplify the code by keeping the received ByteCount in little-endian format. This allows us to eliminate a set of routines for accessing it and we can now drop the *_le suffixes from the accessor functions since that's now implied.

While we're at it, switch all of the places that read the ByteCount directly to use the get_bcc inline which should also clean up some unaligned accesses.

---
 fs/cifs/cifs_debug.c |    2 +-
 fs/cifs/cifspdu.h    |   22 +---------------- fs/cifs/cifsproto.h  |    1 -
 fs/cifs/cifssmb.c    |   61 +++++++++++++++++++++++++------------------------ fs/cifs/connect.c    |    4 +-
 fs/cifs/misc.c       |    4 +-
 fs/cifs/netmisc.c    |    7 -----
 fs/cifs/sess.c       |    2 +-
 fs/cifs/transport.c  |   19 +--------------

 9 files changed, 40 insertions(+), 82 deletions(-)
fs/cifs/cifs_debug.c +1 -1
index 30d01bc..18f4272 100644
@@ -63,7 +63,7 @@ void cifs_dump_detail(struct smb_hdr *smb)
        cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Flgs2: 0x%x Mid: %d Pid: %d",
                  smb->Command, smb->Status.CifsError,
                  smb->Flags, smb->Flags2, smb->Mid, smb->Pid);
-       cERROR(1, "smb buf %p len %d", smb, smbCalcSize_LE(smb));
+       cERROR(1, "smb buf %p len %d", smb, smbCalcSize(smb));
  }
 
 
fs/cifs/cifspdu.h +0 -9
index b5c8cc5..e53c6b3 100644
@@ -435,36 +435,18 @@ struct smb_hdr {
  /* given a pointer to an smb_hdr retrieve the pointer to the byte area */
  #define pByteArea(smb_var) (BCC(smb_var) + 2)
 
-/* get the converted ByteCount for a SMB packet and return it */
-static inline __u16
-get_bcc(struct smb_hdr *hdr)
-{
-       __u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-       return get_unaligned(bc_ptr);
-}
-
  /* get the unconverted ByteCount for a SMB packet and return it */

 static inline __u16

fs/cifs/cifspdu.h +1 -1
-get_bcc_le(struct smb_hdr *hdr)
+get_bcc(struct smb_hdr *hdr)

 {
       __le16 *bc_ptr = (__le16 *)BCC(hdr);

       return get_unaligned_le16(bc_ptr);
 }

fs/cifs/cifspdu.h +0 -9
-/* set the ByteCount for a SMB packet in host-byte order */
-static inline void
-put_bcc(__u16 count, struct smb_hdr *hdr)
-{
-       __u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-       put_unaligned(count, bc_ptr);
-}
-

 /* set the ByteCount for a SMB packet in little-endian */ static inline void

fs/cifs/cifspdu.h +1 -1
-put_bcc_le(__u16 count, struct smb_hdr *hdr)
+put_bcc(__u16 count, struct smb_hdr *hdr)

 {
       __le16 *bc_ptr = (__le16 *)BCC(hdr);

fs/cifs/cifsproto.h +0 -1
index 0e4e057..0cdf375 100644
@@ -90,7 +90,6 @@ extern void cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
  extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
  extern unsigned int smbCalcSize(struct smb_hdr *ptr);
-extern unsigned int smbCalcSize_LE(struct smb_hdr *ptr);
  extern int decode_negTokenInit(unsigned char *security_blob, int length,
                        struct TCP_Server_Info *server);
  extern int cifs_convert_address(struct sockaddr *dst, const char *src, int len);
fs/cifs/cifssmb.c +0 -0
index 2710f00..5ae00d5 100644
@@ -575,7 +575,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
 

       if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC) && (server->capabilities & CAP_EXTENDED_SECURITY)) {
-               count = pSMBr->ByteCount;
+               count = get_bcc(&pSMBr->hdr);
               if (count < 16) {
                       rc = -EIO;
                       goto neg_err_exit;

fs/cifs/cifssmb.c +2 -2
@@ -729,7 +729,7 @@ CIFSSMBEcho(struct TCP_Server_Info *server)
        smb->hdr.Tid = 0xffff;
        smb->hdr.WordCount = 1;
        put_unaligned_le16(1, &smb->EchoCount);
-       put_bcc_le(1, &smb->hdr);
+       put_bcc(1, &smb->hdr);
        smb->Data[0] = 'a';
        smb->hdr.smb_buf_length += 3;
 
@@ -1072,7 +1072,7 @@ PsxCreat:
        cFYI(1, "copying inode info");
        rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-       if (rc || (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP))) {
+       if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {

               rc = -EIO;      /* bad smb */
               goto psx_create_err;
       }

fs/cifs/cifssmb.c +0 -0
@@ -1093,7 +1093,7 @@ PsxCreat:

               pRetData->Type = cpu_to_le32(-1); /* unknown */ cFYI(DBG2, "unknown type");
       } else {
-               if (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP)
+               if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
                                       + sizeof(FILE_UNIX_BASIC_INFO)) { cERROR(1, "Open response data too small"); pRetData->Type = cpu_to_le32(-1);

fs/cifs/cifssmb.c +1 -1
@@ -1859,7 +1859,7 @@ CIFSSMBPosixLock(const int xid, struct cifsTconInfo *tcon,
                __u16 data_count;
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(struct cifs_posix_lock))) {
+               if (rc || (get_bcc(&pSMBr->hdr) < sizeof(struct cifs_posix_lock))) {

                       rc = -EIO;      /* bad smb */ goto plk_err_exit;
               }

fs/cifs/cifssmb.c +3 -3
@@ -2486,7 +2486,7 @@ querySymLinkRetry:
 
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
                /* BB also check enough total bytes returned */
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                        rc = -EIO;
                else {
                        bool is_unicode;
@@ -2568,14 +2568,14 @@ CIFSSMBQueryReparseLinkInfo(const int xid, struct cifsTconInfo *tcon,
        } else {                /* decode response */
                __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
                __u32 data_count = le32_to_cpu(pSMBr->DataCount);
-               if ((pSMBr->ByteCount < 2) || (data_offset > 512)) {
-               /* BB also check enough total bytes returned */
+               if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
+                       /* BB also check enough total bytes returned */

                       rc = -EIO;      /* bad smb */
                       goto qreparse_out;
               }
               if (data_count && (data_count < 2048)) {
                       char *end_of_smb = 2 /* sizeof byte count */ +
-                               pSMBr->ByteCount + (char *)&pSMBr->ByteCount;
+                               get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;

                       struct reparse_data *reparse_buf = (struct reparse_data *)

fs/cifs/cifssmb.c +1 -1
@@ -2833,7 +2833,7 @@ queryAclRetry:
                /* decode response */
 
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                /* BB also check enough total bytes returned */

                       rc = -EIO;      /* bad smb */ else {

fs/cifs/cifssmb.c +1 -1
@@ -2983,7 +2983,7 @@ GetExtAttrRetry:
        } else {
                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                /* BB also check enough total bytes returned */
                        /* If rc should we check for EOPNOSUPP and

                          disable the srvino flag? or in caller? */

fs/cifs/cifssmb.c +4 -3
@@ -3059,6 +3059,7 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
        char *end_of_smb;
        __u32 data_count, data_offset, parm_count, parm_offset;
        struct smb_com_ntransact_rsp *pSMBr;
+       u16 bcc;
 
        *pdatalen = 0;
        *pparmlen = 0;
@@ -3068,8 +3069,8 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
 
        pSMBr = (struct smb_com_ntransact_rsp *)buf;
 
-       /* ByteCount was converted from little endian in SendReceive */
-       end_of_smb = 2 /* sizeof byte count */ + pSMBr->ByteCount +
+       bcc = get_bcc(&pSMBr->hdr);
+       end_of_smb = 2 /* sizeof byte count */ + bcc +
                        (char *)&pSMBr->ByteCount;
 
        data_offset = le32_to_cpu(pSMBr->DataOffset);
@@ -3095,7 +3096,7 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
                        *ppdata, data_count, (data_count + *ppdata),
                        end_of_smb, pSMBr);
                return -EINVAL;
-       } else if (parm_count + data_count > pSMBr->ByteCount) {
+       } else if (parm_count + data_count > bcc) {
                cFYI(1, "parm count and data count larger than SMB");
                return -EINVAL;
        }
@@ -3382,7 +3383,7 @@ QFileInfoRetry:
 

               if (rc) /* BB add auto retry on EOPNOTSUPP? */
                       rc = -EIO;
-               else if (pSMBr->ByteCount < 40)
+               else if (get_bcc(&pSMBr->hdr) < 40)
                       rc = -EIO;      /* bad smb */
               else if (pFindData) {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +0 -0
@@ -3470,9 +3471,9 @@ QPathInfoRetry:
 

               if (rc) /* BB add auto retry on EOPNOTSUPP? */ rc = -EIO;
-               else if (!legacy && (pSMBr->ByteCount < 40))
+               else if (!legacy && get_bcc(&pSMBr->hdr) < 40) rc = -EIO;      /* bad smb */
-               else if (legacy && (pSMBr->ByteCount < 24))
+               else if (legacy && get_bcc(&pSMBr->hdr) < 24) rc = -EIO;  /* 24 or 26 expected but we do not read last field */
               else if (pFindData) {

fs/cifs/cifssmb.c +1 -1
@@ -3548,7 +3549,7 @@ UnixQFileInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
+               if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {

                       cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n" "Unix Extensions can be disabled on mount " "by specifying the nosfu mount option.");

fs/cifs/cifssmb.c +1 -1
@@ -3634,7 +3635,7 @@ UnixQPathInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
+               if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {

                       cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n" "Unix Extensions can be disabled on mount " "by specifying the nosfu mount option.");

fs/cifs/cifssmb.c +1 -1
@@ -4039,7 +4040,7 @@ GetInodeNumberRetry:
        } else {
                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
-               if (rc || (pSMBr->ByteCount < 2))
+               if (rc || get_bcc(&pSMBr->hdr) < 2)
                /* BB also check enough total bytes returned */
                        /* If rc should we check for EOPNOSUPP and

                       disable the srvino flag? or in caller? */

fs/cifs/cifssmb.c +1 -1
@@ -4265,13 +4266,13 @@ getDFSRetry:
        rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
        /* BB Also check if enough total bytes returned? */
-       if (rc || (pSMBr->ByteCount < 17)) {
+       if (rc || get_bcc(&pSMBr->hdr) < 17) {

               rc = -EIO;      /* bad smb */
               goto GetDFSRefExit;
       }

       cFYI(1, "Decoding GetDFSRefer response BCC: %d  Offset %d",
-                               pSMBr->ByteCount,
+                               get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));

       /* parse returned result into more usable form */

fs/cifs/cifssmb.c +1 -1
@@ -4337,12 +4338,12 @@ oldQFSInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 18))
+               if (rc || get_bcc(&pSMBr->hdr) < 18)

                       rc = -EIO;      /* bad smb */
               else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset); cFYI(1, "qfsinf resp BCC: %d  Offset %d",
-                                pSMBr->ByteCount, data_offset);
+                                get_bcc(&pSMBr->hdr), data_offset);

                       response_data = (FILE_SYSTEM_ALLOC_INFO *) (((char *) &pSMBr->hdr.Protocol) + data_offset);

fs/cifs/cifssmb.c +1 -1
@@ -4416,7 +4417,7 @@ QFSInfoRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 24))
+               if (rc || get_bcc(&pSMBr->hdr) < 24)

                       rc = -EIO;      /* bad smb */
               else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +1 -1
@@ -4496,7 +4497,7 @@ QFSAttributeRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 13)) {
+               if (rc || get_bcc(&pSMBr->hdr) < 13) {
                        /* BB also check if enough bytes returned */

                       rc = -EIO;      /* bad smb */ } else {

fs/cifs/cifssmb.c +1 -1
@@ -4567,7 +4568,7 @@ QFSDeviceRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < sizeof(FILE_SYSTEM_DEVICE_INFO)))
+               if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_SYSTEM_DEVICE_INFO))

                       rc = -EIO;      /* bad smb */
               else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +1 -1
@@ -4636,7 +4637,7 @@ QFSUnixRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 13)) {
+               if (rc || get_bcc(&pSMBr->hdr) < 13) {

                       rc = -EIO;      /* bad smb */
               } else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +1 -1
@@ -4781,7 +4782,7 @@ QFSPosixRetry:
        } else {                /* decode response */
                rc = validate_t2((struct smb_t2_rsp *)pSMBr);
 
-               if (rc || (pSMBr->ByteCount < 13)) {
+               if (rc || get_bcc(&pSMBr->hdr) < 13) {

                       rc = -EIO;      /* bad smb */
               } else {
                       __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

fs/cifs/cifssmb.c +0 -0
@@ -5510,7 +5511,7 @@ QAllEAsRetry:

       of these trans2 responses */

       rc = validate_t2((struct smb_t2_rsp *)pSMBr);

-       if (rc || (pSMBr->ByteCount < 4)) {
+       if (rc || get_bcc(&pSMBr->hdr) < 4) {

               rc = -EIO;      /* bad smb */
               goto QAllEAsOut;
       }

fs/cifs/connect.c +2 -2
index 9d05d8f..68bd894 100644
@@ -316,12 +316,12 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
        put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
 
        /* fix up the BCC */
-       byte_count = get_bcc_le(pTargetSMB);
+       byte_count = get_bcc(pTargetSMB);
        byte_count += total_in_buf2;
        /* is the result too big for the field? */
        if (byte_count > USHRT_MAX)
                return -EPROTO;
-       put_bcc_le(byte_count, pTargetSMB);
+       put_bcc(byte_count, pTargetSMB);
 
        byte_count = pTargetSMB->smb_buf_length;
        byte_count += total_in_buf2;
fs/cifs/misc.c +2 -2
index 0c684ae..a0765e6 100644
@@ -464,7 +464,7 @@ checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length)
 
        if (check_smb_hdr(smb, mid))
                return 1;
-       clc_len = smbCalcSize_LE(smb);
+       clc_len = smbCalcSize(smb);
 
        if (4 + len != length) {
                cERROR(1, "Length read does not match RFC1001 length %d",
@@ -521,7 +521,7 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)
                        (struct smb_com_transaction_change_notify_rsp *)buf;
                struct file_notify_information *pnotify;
                __u32 data_offset = 0;
-               if (get_bcc_le(buf) > sizeof(struct file_notify_information)) {
+               if (get_bcc(buf) > sizeof(struct file_notify_information)) {
                        data_offset = le32_to_cpu(pSMBr->DataOffset);
 
                        pnotify = (struct file_notify_information *)
fs/cifs/netmisc.c +0 -7
index 79f641e..79b71c2 100644
@@ -919,13 +919,6 @@ smbCalcSize(struct smb_hdr *ptr)
                2 /* size of the bcc field */ + get_bcc(ptr));
  }
 
-unsigned int
-smbCalcSize_LE(struct smb_hdr *ptr)
-{
-       return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
-               2 /* size of the bcc field */ + get_bcc_le(ptr));
-}
-
  /* The following are taken from fs/ntfs/util.c */
 
  #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
fs/cifs/sess.c +1 -1
index b6ff84a..74344ef 100644
@@ -861,7 +861,7 @@ ssetup_ntlmssp_authenticate:
        count = iov[1].iov_len + iov[2].iov_len;
        smb_buf->smb_buf_length += count;
 
-       put_bcc_le(count, smb_buf);
+       put_bcc(count, smb_buf);
 
        rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
                          CIFS_LOG_ERROR);
fs/cifs/transport.c +1 -6
index 46d8756..44f4f49 100644
@@ -491,7 +491,7 @@ send_nt_cancel(struct TCP_Server_Info *server, struct smb_hdr *in_buf,
        in_buf->smb_buf_length = sizeof(struct smb_hdr) - 4  + 2;
        in_buf->Command = SMB_COM_NT_CANCEL;
        in_buf->WordCount = 0;
-       put_bcc_le(0, in_buf);
+       put_bcc(0, in_buf);
 
        mutex_lock(&server->srv_mutex);
        rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
@@ -651,11 +651,6 @@ SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
                rc = map_smb_to_linux_error(midQ->resp_buf,
                                            flags & CIFS_LOG_ERROR);
 
-               /* convert ByteCount if necessary */
-               if (receive_len >= sizeof(struct smb_hdr) - 4
-                   /* do not count RFC1001 header */  +
-                   (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
-                       put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
                if ((flags & CIFS_NO_RESP) == 0)

                       midQ->resp_buf = NULL;  /* mark it so buf will not be freed by

fs/cifs/transport.c +0 -12
@@ -804,12 +799,6 @@ SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
 
                /* BB special case reconnect tid and uid here? */
                rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
-
-               /* convert ByteCount if necessary */
-               if (receive_len >= sizeof(struct smb_hdr) - 4
-                   /* do not count RFC1001 header */  +
-                   (2 * out_buf->WordCount) + 2 /* bcc */ )
-                       put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
        } else {
                rc = -EIO;
                cERROR(1, "Bad MID state?");
@@ -1017,12 +1006,6 @@ SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
        /* BB special case reconnect tid and uid here? */
        rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
 
-       /* convert ByteCount if necessary */
-       if (receive_len >= sizeof(struct smb_hdr) - 4
-           /* do not count RFC1001 header */  +
-           (2 * out_buf->WordCount) + 2 /* bcc */ )
-               put_bcc(get_bcc_le(out_buf), out_buf);
-
Quoted 5 lines

 out:
       delete_mid(midQ);
       if (rstart && rc == -EACCES)
--
1.7.4.4

This patch has checkpatch warnings:
WARNING: line over 80 characters
#152: FILE: fs/cifs/cifssmb.c:1870:

fs/cifs/cifssmb.c +1 -0
+		if (rc || (get_bcc(&pSMBr->hdr) < sizeof(struct cifs_posix_lock))) {

WARNING: line over 80 characters
#179: FILE: fs/cifs/cifssmb.c:2586:

fs/cifs/cifssmb.c +1 -0
+				get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;

WARNING: suspect code indent for conditional statements (16, 16) #188: FILE: fs/cifs/cifssmb.c:2844:

(quoted diff) +1 -0
+		if (rc || get_bcc(&pSMBr->hdr) < 2)
 		/* BB also check enough total bytes returned */

WARNING: suspect code indent for conditional statements (16, 16) #197: FILE: fs/cifs/cifssmb.c:2994:

(quoted diff) +1 -0
+		if (rc || get_bcc(&pSMBr->hdr) < 2)
 		/* BB also check enough total bytes returned */

WARNING: suspect code indent for conditional statements (16, 16) #273: FILE: fs/cifs/cifssmb.c:4050:

(quoted diff) +1 -0
+		if (rc || get_bcc(&pSMBr->hdr) < 2)
 		/* BB also check enough total bytes returned */

WARNING: line over 80 characters
#331: FILE: fs/cifs/cifssmb.c:4578:

fs/cifs/cifssmb.c +1 -0
+		if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_SYSTEM_DEVICE_INFO))
Quoted 2 lines

total: 0 errors, 6 warnings, 373 lines checked

Bleh -- ok. Many of those problems preexist these changes but it's probably worth fixing them. I'll respin the patch.

Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw

PATCH 1/1 cifs: keep BCC in little-endian format

In reply to a message not in the archive

This is the same patch as originally posted, just with some merge conflicts fixed up...

Currently, the ByteCount is usually converted to host-endian on receive. This is confusing however, as we need to keep two sets of routines for accessing it, and keep track of when to use each routine. Munging received packets like this also limits when the signature can be calulated.

Simplify the code by keeping the received ByteCount in little-endian format. This allows us to eliminate a set of routines for accessing it and we can now drop the *_le suffixes from the accessor functions since that's now implied.

While we're at it, switch all of the places that read the ByteCount directly to use the get_bcc inline which should also clean up some unaligned accesses.

---
 fs/cifs/cifs_debug.c |    2 +-
 fs/cifs/cifspdu.h    |   22 +----------------
 fs/cifs/cifsproto.h  |    1 -
 fs/cifs/cifssmb.c    |   62 +++++++++++++++++++++++++------------------------
 fs/cifs/connect.c    |    4 +-
 fs/cifs/misc.c       |    4 +-
 fs/cifs/netmisc.c    |    7 -----
 fs/cifs/sess.c       |    2 +-
 fs/cifs/transport.c  |   19 +--------------
 9 files changed, 41 insertions(+), 82 deletions(-)
fs/cifs/cifs_debug.c +1 -1
index 30d01bc..18f4272 100644
@@ -63,7 +63,7 @@ void cifs_dump_detail(struct smb_hdr *smb)
 	cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Flgs2: 0x%x Mid: %d Pid: %d",
 		  smb->Command, smb->Status.CifsError,
 		  smb->Flags, smb->Flags2, smb->Mid, smb->Pid);
-	cERROR(1, "smb buf %p len %d", smb, smbCalcSize_LE(smb));
+	cERROR(1, "smb buf %p len %d", smb, smbCalcSize(smb));
 }
 
 
fs/cifs/cifspdu.h +2 -20
index eac95e2..291d735 100644
@@ -435,36 +435,18 @@ struct smb_hdr {
 /* given a pointer to an smb_hdr retrieve the pointer to the byte area */
 #define pByteArea(smb_var) (BCC(smb_var) + 2)
 
-/* get the converted ByteCount for a SMB packet and return it */
-static inline __u16
-get_bcc(struct smb_hdr *hdr)
-{
-	__u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-	return get_unaligned(bc_ptr);
-}
-
 /* get the unconverted ByteCount for a SMB packet and return it */
 static inline __u16
-get_bcc_le(struct smb_hdr *hdr)
+get_bcc(struct smb_hdr *hdr)
 {
 	__le16 *bc_ptr = (__le16 *)BCC(hdr);
 
 	return get_unaligned_le16(bc_ptr);
 }
 
-/* set the ByteCount for a SMB packet in host-byte order */
-static inline void
-put_bcc(__u16 count, struct smb_hdr *hdr)
-{
-	__u16 *bc_ptr = (__u16 *)BCC(hdr);
-
-	put_unaligned(count, bc_ptr);
-}
-
 /* set the ByteCount for a SMB packet in little-endian */
 static inline void
-put_bcc_le(__u16 count, struct smb_hdr *hdr)
+put_bcc(__u16 count, struct smb_hdr *hdr)
 {
 	__le16 *bc_ptr = (__le16 *)BCC(hdr);
 
fs/cifs/cifsproto.h +0 -1
index 7c1ed01..136d2f2 100644
@@ -93,7 +93,6 @@ extern void cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
 extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
 extern unsigned int smbCalcSize(struct smb_hdr *ptr);
-extern unsigned int smbCalcSize_LE(struct smb_hdr *ptr);
 extern int decode_negTokenInit(unsigned char *security_blob, int length,
 			struct TCP_Server_Info *server);
 extern int cifs_convert_address(struct sockaddr *dst, const char *src, int len);
fs/cifs/cifssmb.c +32 -30

230 lines not on this page · raw message

fs/cifs/connect.c +2 -2
index 760f9ed..814d5d2 100644
@@ -317,12 +317,12 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
 	put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
 
 	/* fix up the BCC */
-	byte_count = get_bcc_le(pTargetSMB);
+	byte_count = get_bcc(pTargetSMB);
 	byte_count += total_in_buf2;
 	/* is the result too big for the field? */
 	if (byte_count > USHRT_MAX)
 		return -EPROTO;
-	put_bcc_le(byte_count, pTargetSMB);
+	put_bcc(byte_count, pTargetSMB);
 
 	byte_count = be32_to_cpu(pTargetSMB->smb_buf_length);
 	byte_count += total_in_buf2;
fs/cifs/misc.c +2 -2
index 533f863..907531a 100644
@@ -462,7 +462,7 @@ checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length)
 
 	if (check_smb_hdr(smb, mid))
 		return 1;
-	clc_len = smbCalcSize_LE(smb);
+	clc_len = smbCalcSize(smb);
 
 	if (4 + len != length) {
 		cERROR(1, "Length read does not match RFC1001 length %d",
@@ -519,7 +519,7 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)
 			(struct smb_com_transaction_change_notify_rsp *)buf;
 		struct file_notify_information *pnotify;
 		__u32 data_offset = 0;
-		if (get_bcc_le(buf) > sizeof(struct file_notify_information)) {
+		if (get_bcc(buf) > sizeof(struct file_notify_information)) {
 			data_offset = le32_to_cpu(pSMBr->DataOffset);
 
 			pnotify = (struct file_notify_information *)
fs/cifs/netmisc.c +0 -7
index 79f641e..79b71c2 100644
@@ -919,13 +919,6 @@ smbCalcSize(struct smb_hdr *ptr)
 		2 /* size of the bcc field */ + get_bcc(ptr));
 }
 
-unsigned int
-smbCalcSize_LE(struct smb_hdr *ptr)
-{
-	return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
-		2 /* size of the bcc field */ + get_bcc_le(ptr));
-}
-
 /* The following are taken from fs/ntfs/util.c */
 
 #define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
fs/cifs/sess.c +1 -1
index 1daadad..7dd4621 100644
@@ -862,7 +862,7 @@ ssetup_ntlmssp_authenticate:
 	smb_buf->smb_buf_length =
 		cpu_to_be32(be32_to_cpu(smb_buf->smb_buf_length) + count);
 
-	put_bcc_le(count, smb_buf);
+	put_bcc(count, smb_buf);
 
 	rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
 			  CIFS_LOG_ERROR);
fs/cifs/transport.c +1 -18
index 19df0e5..f2513fb 100644
@@ -484,7 +484,7 @@ send_nt_cancel(struct TCP_Server_Info *server, struct smb_hdr *in_buf,
 	in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4  + 2);
 	in_buf->Command = SMB_COM_NT_CANCEL;
 	in_buf->WordCount = 0;
-	put_bcc_le(0, in_buf);
+	put_bcc(0, in_buf);
 
 	mutex_lock(&server->srv_mutex);
 	rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
@@ -644,11 +644,6 @@ SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
 		rc = map_smb_to_linux_error(midQ->resp_buf,
 					    flags & CIFS_LOG_ERROR);
 
-		/* convert ByteCount if necessary */
-		if (receive_len >= sizeof(struct smb_hdr) - 4
-		    /* do not count RFC1001 header */  +
-		    (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
-			put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
 		if ((flags & CIFS_NO_RESP) == 0)
 			midQ->resp_buf = NULL;  /* mark it so buf will
 						   not be freed by
@@ -798,12 +793,6 @@ SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
 
 		/* BB special case reconnect tid and uid here? */
 		rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
-
-		/* convert ByteCount if necessary */
-		if (receive_len >= sizeof(struct smb_hdr) - 4
-		    /* do not count RFC1001 header */  +
-		    (2 * out_buf->WordCount) + 2 /* bcc */ )
-			put_bcc(get_bcc_le(midQ->resp_buf), midQ->resp_buf);
 	} else {
 		rc = -EIO;
 		cERROR(1, "Bad MID state?");
@@ -1012,12 +1001,6 @@ SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
 	/* BB special case reconnect tid and uid here? */
 	rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
 
-	/* convert ByteCount if necessary */
-	if (receive_len >= sizeof(struct smb_hdr) - 4
-	    /* do not count RFC1001 header */  +
-	    (2 * out_buf->WordCount) + 2 /* bcc */ )
-		put_bcc(get_bcc_le(out_buf), out_buf);
-
 out:
 	delete_mid(midQ);
 	if (rstart && rc == -EACCES)
Signature · 1 line
1.7.4.4