Skip to content

Commit

Permalink
cifs: sanitize length checking in coalesce_t2 (try #3)
Browse files Browse the repository at this point in the history
There are a couple of places in this code where these values can wrap or
go negative, and that could potentially end up overflowing the buffer.
Ensure that that doesn't happen. Do all of the length calculation and
checks first, and only perform the memcpy after they pass.

Also, increase some stack variables to 32 bits to ensure that they don't
wrap without being detected.

Finally, change the error codes to be a bit more descriptive of any
problems detected. -EINVAL isn't very accurate.

Cc: stable@kernel.org
Reported-and-Acked-by: David Howells <dhowells@redhat.com>
Signed-off-by: Jeff Layton <jlayton@redhat.com>
Signed-off-by: Steve French <sfrench@us.ibm.com>
  • Loading branch information
Jeff Layton authored and Steve French committed Apr 29, 2011
1 parent fcda7f4 commit 2a2047b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions fs/cifs/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
char *data_area_of_target;
char *data_area_of_buf2;
int remaining;
__u16 byte_count, total_data_size, total_in_buf, total_in_buf2;
unsigned int byte_count, total_in_buf;
__u16 total_data_size, total_in_buf2;

total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);

Expand All @@ -287,7 +288,7 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
remaining = total_data_size - total_in_buf;

if (remaining < 0)
return -EINVAL;
return -EPROTO;

if (remaining == 0) /* nothing to do, ignore */
return 0;
Expand All @@ -308,20 +309,29 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
data_area_of_target += total_in_buf;

/* copy second buffer into end of first buffer */
memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
total_in_buf += total_in_buf2;
/* is the result too big for the field? */
if (total_in_buf > USHRT_MAX)
return -EPROTO;
put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);

/* fix up the BCC */
byte_count = get_bcc_le(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);

byte_count = pTargetSMB->smb_buf_length;
byte_count += total_in_buf2;

/* BB also add check that we are not beyond maximum buffer size */

/* don't allow buffer to overflow */
if (byte_count > CIFSMaxBufSize)
return -ENOBUFS;
pTargetSMB->smb_buf_length = byte_count;

memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);

if (remaining == total_in_buf2) {
cFYI(1, "found the last secondary response");
return 0; /* we are done */
Expand Down

0 comments on commit 2a2047b

Please sign in to comment.