Skip to content

Commit 820a803

Browse files
jtlaytonSteve French
authored andcommitted
cifs: keep BCC in little-endian format
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. Signed-off-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Steve French <sfrench@us.ibm.com>
1 parent 0e6e37a commit 820a803

9 files changed

Lines changed: 41 additions & 82 deletions

File tree

‎fs/cifs/cifs_debug.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void cifs_dump_detail(struct smb_hdr *smb)
6363
cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Flgs2: 0x%x Mid: %d Pid: %d",
6464
smb->Command, smb->Status.CifsError,
6565
smb->Flags, smb->Flags2, smb->Mid, smb->Pid);
66-
cERROR(1, "smb buf %p len %d", smb, smbCalcSize_LE(smb));
66+
cERROR(1, "smb buf %p len %d", smb, smbCalcSize(smb));
6767
}
6868

6969

‎fs/cifs/cifspdu.h‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -435,36 +435,18 @@ struct smb_hdr {
435435
/* given a pointer to an smb_hdr retrieve the pointer to the byte area */
436436
#define pByteArea(smb_var) (BCC(smb_var) + 2)
437437

438-
/* get the converted ByteCount for a SMB packet and return it */
439-
static inline __u16
440-
get_bcc(struct smb_hdr *hdr)
441-
{
442-
__u16 *bc_ptr = (__u16 *)BCC(hdr);
443-
444-
return get_unaligned(bc_ptr);
445-
}
446-
447438
/* get the unconverted ByteCount for a SMB packet and return it */
448439
static inline __u16
449-
get_bcc_le(struct smb_hdr *hdr)
440+
get_bcc(struct smb_hdr *hdr)
450441
{
451442
__le16 *bc_ptr = (__le16 *)BCC(hdr);
452443

453444
return get_unaligned_le16(bc_ptr);
454445
}
455446

456-
/* set the ByteCount for a SMB packet in host-byte order */
457-
static inline void
458-
put_bcc(__u16 count, struct smb_hdr *hdr)
459-
{
460-
__u16 *bc_ptr = (__u16 *)BCC(hdr);
461-
462-
put_unaligned(count, bc_ptr);
463-
}
464-
465447
/* set the ByteCount for a SMB packet in little-endian */
466448
static inline void
467-
put_bcc_le(__u16 count, struct smb_hdr *hdr)
449+
put_bcc(__u16 count, struct smb_hdr *hdr)
468450
{
469451
__le16 *bc_ptr = (__le16 *)BCC(hdr);
470452

‎fs/cifs/cifsproto.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ extern void cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
9393
extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, bool);
9494
extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
9595
extern unsigned int smbCalcSize(struct smb_hdr *ptr);
96-
extern unsigned int smbCalcSize_LE(struct smb_hdr *ptr);
9796
extern int decode_negTokenInit(unsigned char *security_blob, int length,
9897
struct TCP_Server_Info *server);
9998
extern int cifs_convert_address(struct sockaddr *dst, const char *src, int len);

‎fs/cifs/cifssmb.c‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
582582

583583
if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC) &&
584584
(server->capabilities & CAP_EXTENDED_SECURITY)) {
585-
count = pSMBr->ByteCount;
585+
count = get_bcc(&pSMBr->hdr);
586586
if (count < 16) {
587587
rc = -EIO;
588588
goto neg_err_exit;
@@ -736,7 +736,7 @@ CIFSSMBEcho(struct TCP_Server_Info *server)
736736
smb->hdr.Tid = 0xffff;
737737
smb->hdr.WordCount = 1;
738738
put_unaligned_le16(1, &smb->EchoCount);
739-
put_bcc_le(1, &smb->hdr);
739+
put_bcc(1, &smb->hdr);
740740
smb->Data[0] = 'a';
741741
inc_rfc1001_len(smb, 3);
742742

@@ -1079,7 +1079,7 @@ CIFSPOSIXCreate(const int xid, struct cifsTconInfo *tcon, __u32 posix_flags,
10791079
cFYI(1, "copying inode info");
10801080
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
10811081

1082-
if (rc || (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP))) {
1082+
if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
10831083
rc = -EIO; /* bad smb */
10841084
goto psx_create_err;
10851085
}
@@ -1100,7 +1100,7 @@ CIFSPOSIXCreate(const int xid, struct cifsTconInfo *tcon, __u32 posix_flags,
11001100
pRetData->Type = cpu_to_le32(-1); /* unknown */
11011101
cFYI(DBG2, "unknown type");
11021102
} else {
1103-
if (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP)
1103+
if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
11041104
+ sizeof(FILE_UNIX_BASIC_INFO)) {
11051105
cERROR(1, "Open response data too small");
11061106
pRetData->Type = cpu_to_le32(-1);
@@ -1867,7 +1867,7 @@ CIFSSMBPosixLock(const int xid, struct cifsTconInfo *tcon,
18671867
__u16 data_count;
18681868
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
18691869

1870-
if (rc || (pSMBr->ByteCount < sizeof(struct cifs_posix_lock))) {
1870+
if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
18711871
rc = -EIO; /* bad smb */
18721872
goto plk_err_exit;
18731873
}
@@ -2494,7 +2494,7 @@ CIFSSMBUnixQuerySymLink(const int xid, struct cifsTconInfo *tcon,
24942494

24952495
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
24962496
/* BB also check enough total bytes returned */
2497-
if (rc || (pSMBr->ByteCount < 2))
2497+
if (rc || get_bcc(&pSMBr->hdr) < 2)
24982498
rc = -EIO;
24992499
else {
25002500
bool is_unicode;
@@ -2576,14 +2576,14 @@ CIFSSMBQueryReparseLinkInfo(const int xid, struct cifsTconInfo *tcon,
25762576
} else { /* decode response */
25772577
__u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
25782578
__u32 data_count = le32_to_cpu(pSMBr->DataCount);
2579-
if ((pSMBr->ByteCount < 2) || (data_offset > 512)) {
2580-
/* BB also check enough total bytes returned */
2579+
if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
2580+
/* BB also check enough total bytes returned */
25812581
rc = -EIO; /* bad smb */
25822582
goto qreparse_out;
25832583
}
25842584
if (data_count && (data_count < 2048)) {
25852585
char *end_of_smb = 2 /* sizeof byte count */ +
2586-
pSMBr->ByteCount + (char *)&pSMBr->ByteCount;
2586+
get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
25872587

25882588
struct reparse_data *reparse_buf =
25892589
(struct reparse_data *)
@@ -2841,8 +2841,8 @@ CIFSSMBGetPosixACL(const int xid, struct cifsTconInfo *tcon,
28412841
/* decode response */
28422842

28432843
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2844-
if (rc || (pSMBr->ByteCount < 2))
28452844
/* BB also check enough total bytes returned */
2845+
if (rc || get_bcc(&pSMBr->hdr) < 2)
28462846
rc = -EIO; /* bad smb */
28472847
else {
28482848
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
@@ -2991,8 +2991,8 @@ CIFSGetExtAttr(const int xid, struct cifsTconInfo *tcon,
29912991
} else {
29922992
/* decode response */
29932993
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2994-
if (rc || (pSMBr->ByteCount < 2))
29952994
/* BB also check enough total bytes returned */
2995+
if (rc || get_bcc(&pSMBr->hdr) < 2)
29962996
/* If rc should we check for EOPNOSUPP and
29972997
disable the srvino flag? or in caller? */
29982998
rc = -EIO; /* bad smb */
@@ -3067,6 +3067,7 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
30673067
char *end_of_smb;
30683068
__u32 data_count, data_offset, parm_count, parm_offset;
30693069
struct smb_com_ntransact_rsp *pSMBr;
3070+
u16 bcc;
30703071

30713072
*pdatalen = 0;
30723073
*pparmlen = 0;
@@ -3076,8 +3077,8 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
30763077

30773078
pSMBr = (struct smb_com_ntransact_rsp *)buf;
30783079

3079-
/* ByteCount was converted from little endian in SendReceive */
3080-
end_of_smb = 2 /* sizeof byte count */ + pSMBr->ByteCount +
3080+
bcc = get_bcc(&pSMBr->hdr);
3081+
end_of_smb = 2 /* sizeof byte count */ + bcc +
30813082
(char *)&pSMBr->ByteCount;
30823083

30833084
data_offset = le32_to_cpu(pSMBr->DataOffset);
@@ -3103,7 +3104,7 @@ validate_ntransact(char *buf, char **ppparm, char **ppdata,
31033104
*ppdata, data_count, (data_count + *ppdata),
31043105
end_of_smb, pSMBr);
31053106
return -EINVAL;
3106-
} else if (parm_count + data_count > pSMBr->ByteCount) {
3107+
} else if (parm_count + data_count > bcc) {
31073108
cFYI(1, "parm count and data count larger than SMB");
31083109
return -EINVAL;
31093110
}
@@ -3389,7 +3390,7 @@ CIFSSMBQFileInfo(const int xid, struct cifsTconInfo *tcon,
33893390

33903391
if (rc) /* BB add auto retry on EOPNOTSUPP? */
33913392
rc = -EIO;
3392-
else if (pSMBr->ByteCount < 40)
3393+
else if (get_bcc(&pSMBr->hdr) < 40)
33933394
rc = -EIO; /* bad smb */
33943395
else if (pFindData) {
33953396
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
@@ -3477,9 +3478,9 @@ CIFSSMBQPathInfo(const int xid, struct cifsTconInfo *tcon,
34773478

34783479
if (rc) /* BB add auto retry on EOPNOTSUPP? */
34793480
rc = -EIO;
3480-
else if (!legacy && (pSMBr->ByteCount < 40))
3481+
else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
34813482
rc = -EIO; /* bad smb */
3482-
else if (legacy && (pSMBr->ByteCount < 24))
3483+
else if (legacy && get_bcc(&pSMBr->hdr) < 24)
34833484
rc = -EIO; /* 24 or 26 expected but we do not read
34843485
last field */
34853486
else if (pFindData) {
@@ -3555,7 +3556,7 @@ CIFSSMBUnixQFileInfo(const int xid, struct cifsTconInfo *tcon,
35553556
} else { /* decode response */
35563557
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
35573558

3558-
if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
3559+
if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
35593560
cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
35603561
"Unix Extensions can be disabled on mount "
35613562
"by specifying the nosfu mount option.");
@@ -3641,7 +3642,7 @@ CIFSSMBUnixQPathInfo(const int xid, struct cifsTconInfo *tcon,
36413642
} else { /* decode response */
36423643
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
36433644

3644-
if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
3645+
if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
36453646
cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
36463647
"Unix Extensions can be disabled on mount "
36473648
"by specifying the nosfu mount option.");
@@ -4046,8 +4047,8 @@ CIFSGetSrvInodeNumber(const int xid, struct cifsTconInfo *tcon,
40464047
} else {
40474048
/* decode response */
40484049
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4049-
if (rc || (pSMBr->ByteCount < 2))
40504050
/* BB also check enough total bytes returned */
4051+
if (rc || get_bcc(&pSMBr->hdr) < 2)
40514052
/* If rc should we check for EOPNOSUPP and
40524053
disable the srvino flag? or in caller? */
40534054
rc = -EIO; /* bad smb */
@@ -4272,13 +4273,13 @@ CIFSGetDFSRefer(const int xid, struct cifsSesInfo *ses,
42724273
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
42734274

42744275
/* BB Also check if enough total bytes returned? */
4275-
if (rc || (pSMBr->ByteCount < 17)) {
4276+
if (rc || get_bcc(&pSMBr->hdr) < 17) {
42764277
rc = -EIO; /* bad smb */
42774278
goto GetDFSRefExit;
42784279
}
42794280

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

42844285
/* parse returned result into more usable form */
@@ -4344,12 +4345,12 @@ SMBOldQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
43444345
} else { /* decode response */
43454346
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
43464347

4347-
if (rc || (pSMBr->ByteCount < 18))
4348+
if (rc || get_bcc(&pSMBr->hdr) < 18)
43484349
rc = -EIO; /* bad smb */
43494350
else {
43504351
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
43514352
cFYI(1, "qfsinf resp BCC: %d Offset %d",
4352-
pSMBr->ByteCount, data_offset);
4353+
get_bcc(&pSMBr->hdr), data_offset);
43534354

43544355
response_data = (FILE_SYSTEM_ALLOC_INFO *)
43554356
(((char *) &pSMBr->hdr.Protocol) + data_offset);
@@ -4423,7 +4424,7 @@ CIFSSMBQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
44234424
} else { /* decode response */
44244425
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
44254426

4426-
if (rc || (pSMBr->ByteCount < 24))
4427+
if (rc || get_bcc(&pSMBr->hdr) < 24)
44274428
rc = -EIO; /* bad smb */
44284429
else {
44294430
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
@@ -4503,7 +4504,7 @@ CIFSSMBQFSAttributeInfo(const int xid, struct cifsTconInfo *tcon)
45034504
} else { /* decode response */
45044505
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
45054506

4506-
if (rc || (pSMBr->ByteCount < 13)) {
4507+
if (rc || get_bcc(&pSMBr->hdr) < 13) {
45074508
/* BB also check if enough bytes returned */
45084509
rc = -EIO; /* bad smb */
45094510
} else {
@@ -4574,7 +4575,8 @@ CIFSSMBQFSDeviceInfo(const int xid, struct cifsTconInfo *tcon)
45744575
} else { /* decode response */
45754576
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
45764577

4577-
if (rc || (pSMBr->ByteCount < sizeof(FILE_SYSTEM_DEVICE_INFO)))
4578+
if (rc || get_bcc(&pSMBr->hdr) <
4579+
sizeof(FILE_SYSTEM_DEVICE_INFO))
45784580
rc = -EIO; /* bad smb */
45794581
else {
45804582
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
@@ -4643,7 +4645,7 @@ CIFSSMBQFSUnixInfo(const int xid, struct cifsTconInfo *tcon)
46434645
} else { /* decode response */
46444646
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
46454647

4646-
if (rc || (pSMBr->ByteCount < 13)) {
4648+
if (rc || get_bcc(&pSMBr->hdr) < 13) {
46474649
rc = -EIO; /* bad smb */
46484650
} else {
46494651
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
@@ -4788,7 +4790,7 @@ CIFSSMBQFSPosixInfo(const int xid, struct cifsTconInfo *tcon,
47884790
} else { /* decode response */
47894791
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
47904792

4791-
if (rc || (pSMBr->ByteCount < 13)) {
4793+
if (rc || get_bcc(&pSMBr->hdr) < 13) {
47924794
rc = -EIO; /* bad smb */
47934795
} else {
47944796
__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
@@ -5517,7 +5519,7 @@ CIFSSMBQAllEAs(const int xid, struct cifsTconInfo *tcon,
55175519
of these trans2 responses */
55185520

55195521
rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5520-
if (rc || (pSMBr->ByteCount < 4)) {
5522+
if (rc || get_bcc(&pSMBr->hdr) < 4) {
55215523
rc = -EIO; /* bad smb */
55225524
goto QAllEAsOut;
55235525
}

‎fs/cifs/connect.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,12 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
317317
put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
318318

319319
/* fix up the BCC */
320-
byte_count = get_bcc_le(pTargetSMB);
320+
byte_count = get_bcc(pTargetSMB);
321321
byte_count += total_in_buf2;
322322
/* is the result too big for the field? */
323323
if (byte_count > USHRT_MAX)
324324
return -EPROTO;
325-
put_bcc_le(byte_count, pTargetSMB);
325+
put_bcc(byte_count, pTargetSMB);
326326

327327
byte_count = be32_to_cpu(pTargetSMB->smb_buf_length);
328328
byte_count += total_in_buf2;

‎fs/cifs/misc.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length)
462462

463463
if (check_smb_hdr(smb, mid))
464464
return 1;
465-
clc_len = smbCalcSize_LE(smb);
465+
clc_len = smbCalcSize(smb);
466466

467467
if (4 + len != length) {
468468
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)
519519
(struct smb_com_transaction_change_notify_rsp *)buf;
520520
struct file_notify_information *pnotify;
521521
__u32 data_offset = 0;
522-
if (get_bcc_le(buf) > sizeof(struct file_notify_information)) {
522+
if (get_bcc(buf) > sizeof(struct file_notify_information)) {
523523
data_offset = le32_to_cpu(pSMBr->DataOffset);
524524

525525
pnotify = (struct file_notify_information *)

‎fs/cifs/netmisc.c‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -919,13 +919,6 @@ smbCalcSize(struct smb_hdr *ptr)
919919
2 /* size of the bcc field */ + get_bcc(ptr));
920920
}
921921

922-
unsigned int
923-
smbCalcSize_LE(struct smb_hdr *ptr)
924-
{
925-
return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
926-
2 /* size of the bcc field */ + get_bcc_le(ptr));
927-
}
928-
929922
/* The following are taken from fs/ntfs/util.c */
930923

931924
#define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)

‎fs/cifs/sess.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses,
862862
smb_buf->smb_buf_length =
863863
cpu_to_be32(be32_to_cpu(smb_buf->smb_buf_length) + count);
864864

865-
put_bcc_le(count, smb_buf);
865+
put_bcc(count, smb_buf);
866866

867867
rc = SendReceive2(xid, ses, iov, 3 /* num_iovecs */, &resp_buf_type,
868868
CIFS_LOG_ERROR);

0 commit comments

Comments
 (0)