ffdfc30
From fc4f4cdb8bf9981904e652abf69b892a45bddacf Mon Sep 17 00:00:00 2001
ffdfc30
From: David Benjamin <davidben@google.com>
ffdfc30
Date: Wed, 23 Jul 2014 22:32:21 +0200
ffdfc30
Subject: [PATCH] Fix protocol downgrade bug in case of fragmented packets
ffdfc30
MIME-Version: 1.0
ffdfc30
Content-Type: text/plain; charset=UTF-8
ffdfc30
Content-Transfer-Encoding: 8bit
ffdfc30
ffdfc30
CVE-2014-3511
ffdfc30
ffdfc30
Reviewed-by: Emilia Käsper <emilia@openssl.org>
ffdfc30
Reviewed-by: Bodo Möller <bodo@openssl.org>
ffdfc30
---
ffdfc30
 ssl/s23_srvr.c | 30 +++++++++++++++++++++++-------
ffdfc30
 1 file changed, 23 insertions(+), 7 deletions(-)
ffdfc30
ffdfc30
diff --git a/ssl/s23_srvr.c b/ssl/s23_srvr.c
ffdfc30
index 4877849..2901a6b 100644
ffdfc30
--- a/ssl/s23_srvr.c
ffdfc30
+++ b/ssl/s23_srvr.c
ffdfc30
@@ -348,23 +348,19 @@ int ssl23_get_client_hello(SSL *s)
ffdfc30
 			 * Client Hello message, this would be difficult, and we'd have
ffdfc30
 			 * to read more records to find out.
ffdfc30
 			 * No known SSL 3.0 client fragments ClientHello like this,
ffdfc30
-			 * so we simply assume TLS 1.0 to avoid protocol version downgrade
ffdfc30
-			 * attacks. */
ffdfc30
+			 * so we simply reject such connections to avoid
ffdfc30
+			 * protocol version downgrade attacks. */
ffdfc30
 			if (p[3] == 0 && p[4] < 6)
ffdfc30
 				{
ffdfc30
-#if 0
ffdfc30
 				SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_SMALL);
ffdfc30
 				goto err;
ffdfc30
-#else
ffdfc30
-				v[1] = TLS1_VERSION_MINOR;
ffdfc30
-#endif
ffdfc30
 				}
ffdfc30
 			/* if major version number > 3 set minor to a value
ffdfc30
 			 * which will use the highest version 3 we support.
ffdfc30
 			 * If TLS 2.0 ever appears we will need to revise
ffdfc30
 			 * this....
ffdfc30
 			 */
ffdfc30
-			else if (p[9] > SSL3_VERSION_MAJOR)
ffdfc30
+			if (p[9] > SSL3_VERSION_MAJOR)
ffdfc30
 				v[1]=0xff;
ffdfc30
 			else
ffdfc30
 				v[1]=p[10]; /* minor version according to client_version */
ffdfc30
@@ -444,14 +440,34 @@ int ssl23_get_client_hello(SSL *s)
ffdfc30
 		v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
ffdfc30
 		v[1] = p[4];
ffdfc30
 
ffdfc30
+		/* An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
ffdfc30
+		 * header is sent directly on the wire, not wrapped as a TLS
ffdfc30
+		 * record. It's format is:
ffdfc30
+		 * Byte  Content
ffdfc30
+		 * 0-1   msg_length
ffdfc30
+		 * 2     msg_type
ffdfc30
+		 * 3-4   version
ffdfc30
+		 * 5-6   cipher_spec_length
ffdfc30
+		 * 7-8   session_id_length
ffdfc30
+		 * 9-10  challenge_length
ffdfc30
+		 * ...   ...
ffdfc30
+		 */
ffdfc30
 		n=((p[0]&0x7f)<<8)|p[1];
ffdfc30
 		if (n > (1024*4))
ffdfc30
 			{
ffdfc30
 			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
ffdfc30
 			goto err;
ffdfc30
 			}
ffdfc30
+		if (n < 9)
ffdfc30
+			{
ffdfc30
+			SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
ffdfc30
+			goto err;
ffdfc30
+			}
ffdfc30
 
ffdfc30
 		j=ssl23_read_bytes(s,n+2);
ffdfc30
+		/* We previously read 11 bytes, so if j > 0, we must have
ffdfc30
+		 * j == n+2 == s->packet_length. We have at least 11 valid
ffdfc30
+		 * packet bytes. */
ffdfc30
 		if (j <= 0) return(j);
ffdfc30
 
ffdfc30
 		ssl3_finish_mac(s, s->packet+2, s->packet_length-2);
ffdfc30
-- 
ffdfc30
1.8.3.1
ffdfc30