All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
To: smfrench-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Cc: dhowells-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	linux-cifs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: [PATCH 3/5] cifs: sanitize length checking in coalesce_t2
Date: Tue, 26 Apr 2011 08:03:19 -0400	[thread overview]
Message-ID: <1303819401-14789-4-git-send-email-jlayton@redhat.com> (raw)
In-Reply-To: <1303819401-14789-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>

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 of the field sizes to 32 bits to ensure that they
don't wrap without being detected.

Cc: stable-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org
Reported-by: David Howells <dhowells-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
Signed-off-by: Jeff Layton <jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
---
 fs/cifs/connect.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/fs/cifs/connect.c b/fs/cifs/connect.c
index db9d55b..a038f29 100644
--- a/fs/cifs/connect.c
+++ b/fs/cifs/connect.c
@@ -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);
 
@@ -308,20 +309,28 @@ 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;
+	/* did this field "wrap" ? */
+	if (total_in_buf & ~((1<<16)-1))
+		return -EINVAL;
 	put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
+
 	byte_count = get_bcc_le(pTargetSMB);
 	byte_count += total_in_buf2;
+	/* did this field "wrap" ? */
+	if (byte_count & ~((1<<16)-1))
+		return -EINVAL;
 	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 -EINVAL;
 	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 */
-- 
1.7.4.4

  parent reply	other threads:[~2011-04-26 12:03 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-26 12:03 [PATCH 0/5] cifs: fix some bounds checking problems Jeff Layton
     [not found] ` <1303819401-14789-2-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-26 14:10   ` [PATCH 1/5] cifs: change bleft in decode_unicode_ssetup back to signed type David Howells
     [not found] ` <1303819401-14789-3-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-26 14:11   ` [PATCH 2/5] cifs: check for bytes_remaining going to zero in CIFS_SessSetup David Howells
     [not found] ` <1303819401-14789-4-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-26 14:27   ` [PATCH 3/5] cifs: sanitize length checking in coalesce_t2 David Howells
     [not found]     ` <17747.1303828052-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-26 16:00       ` Jeff Layton
2011-04-27 12:03       ` [PATCH 3/5] cifs: sanitize length checking in coalesce_t2 (try #2) Jeff Layton
     [not found]     ` <1303905796-28087-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-27 16:37       ` David Howells
     [not found]         ` <13543.1303922232-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-27 16:43           ` David Howells
2011-04-27 16:43           ` Jeff Layton
2011-04-27 17:31           ` [PATCH] cifs: sanitize length checking in coalesce_t2 (try #3) Jeff Layton
     [not found] ` <1303819401-14789-5-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-26 14:39   ` [PATCH 4/5] cifs: refactor mid finding loop in cifs_demultiplex_thread David Howells
2011-05-03  3:44   ` Steve French
     [not found]     ` <BANLkTim4jXoQm47-ecw8r5ftBVGRbA+mKw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-05-03 12:09       ` Jeff Layton
     [not found]         ` <20110503080953.65db9af6-4QP7MXygkU+dMjc06nkz3ljfA9RmPOcC@public.gmane.org>
2011-05-03 14:50           ` Steve French
     [not found] ` <1303819401-14789-6-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-26 14:40   ` [PATCH 5/5] cifs: handle errors from coalesce_t2 David Howells
     [not found] ` <1303819401-14789-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-04-26 12:03   ` [PATCH 1/5] cifs: change bleft in decode_unicode_ssetup back to signed type Jeff Layton
2011-04-26 12:03   ` [PATCH 2/5] cifs: check for bytes_remaining going to zero in CIFS_SessSetup Jeff Layton
2011-04-26 12:03   ` Jeff Layton [this message]
2011-04-26 12:03   ` [PATCH 4/5] cifs: refactor mid finding loop in cifs_demultiplex_thread Jeff Layton
2011-04-26 12:03   ` [PATCH 5/5] cifs: handle errors from coalesce_t2 Jeff Layton
2011-04-27 14:17   ` [PATCH 0/5] cifs: fix some bounds checking problems Jeff Layton
     [not found]     ` <20110427101740.32be5c28-4QP7MXygkU+dMjc06nkz3ljfA9RmPOcC@public.gmane.org>
2011-04-27 14:59       ` Steve French
     [not found]         ` <BANLkTiko7K1HJFjAKfRokmFiWk=+rHe0DA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-04-27 15:04           ` Jeff Layton
     [not found]             ` <20110427110404.71b80c29-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org>
2011-04-27 15:06               ` Steve French
     [not found]                 ` <BANLkTinzFc5DgUuG9ohRQNGk12PDO7J-uQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-04-27 15:07                   ` Steve French
     [not found]                     ` <BANLkTikLYQg_zEWS5Joz=7eTj1P_YF6ZzQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-04-27 15:14                       ` Jeff Layton
2011-04-29  5:05   ` Steve French
     [not found]     ` <BANLkTimNseyrXc9ZDz0N4tomRx1oy5P60Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-04-29 10:55       ` Jeff Layton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1303819401-14789-4-git-send-email-jlayton@redhat.com \
    --to=jlayton-h+wxahxf7alqt0dzr+alfa@public.gmane.org \
    --cc=dhowells-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=linux-cifs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=smfrench-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.