3 <https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-2177>
6 <https://git.openssl.org/?p=openssl.git;a=commit;h=a004e72b95835136d3f1ea90517f706c24c03da7>
8 From a004e72b95835136d3f1ea90517f706c24c03da7 Mon Sep 17 00:00:00 2001
9 From: Matt Caswell <matt@openssl.org>
10 Date: Thu, 5 May 2016 11:10:26 +0100
11 Subject: [PATCH] Avoid some undefined pointer arithmetic
13 A common idiom in the codebase is:
17 return; /* Too long */
20 Where "p" points to some malloc'd data of SIZE bytes and
23 "len" here could be from some externally supplied data (e.g. from a TLS
26 The rules of C pointer arithmetic are such that "p + len" is only well
27 defined where len <= SIZE. Therefore the above idiom is actually
30 For example this could cause problems if some malloc implementation
31 provides an address for "p" such that "p + len" actually overflows for
32 values of len that are too big and therefore p + len < limit!
34 Issue reported by Guido Vranken.
38 Reviewed-by: Rich Salz <rsalz@openssl.org>
40 ssl/s3_srvr.c | 14 +++++++-------
42 ssl/t1_lib.c | 56 ++++++++++++++++++++++++++++++--------------------------
43 3 files changed, 38 insertions(+), 34 deletions(-)
45 diff --git a/ssl/s3_srvr.c b/ssl/s3_srvr.c
46 index ab28702..ab7f690 100644
49 @@ -980,7 +980,7 @@ int ssl3_get_client_hello(SSL *s)
51 session_length = *(p + SSL3_RANDOM_SIZE);
53 - if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) {
54 + if (SSL3_RANDOM_SIZE + session_length + 1 >= (d + n) - p) {
55 al = SSL_AD_DECODE_ERROR;
56 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
58 @@ -998,7 +998,7 @@ int ssl3_get_client_hello(SSL *s)
59 /* get the session-id */
62 - if (p + j > d + n) {
63 + if ((d + n) - p < j) {
64 al = SSL_AD_DECODE_ERROR;
65 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
67 @@ -1054,14 +1054,14 @@ int ssl3_get_client_hello(SSL *s)
71 - if (p + 1 > d + n) {
72 + if ((d + n) - p < 1) {
73 al = SSL_AD_DECODE_ERROR;
74 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
79 - if (p + cookie_len > d + n) {
80 + if ((d + n ) - p < cookie_len) {
81 al = SSL_AD_DECODE_ERROR;
82 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
84 @@ -1131,7 +1131,7 @@ int ssl3_get_client_hello(SSL *s)
88 - if (p + 2 > d + n) {
89 + if ((d + n ) - p < 2) {
90 al = SSL_AD_DECODE_ERROR;
91 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
93 @@ -1145,7 +1145,7 @@ int ssl3_get_client_hello(SSL *s)
96 /* i bytes of cipher data + 1 byte for compression length later */
97 - if ((p + i + 1) > (d + n)) {
98 + if ((d + n) - p < i + 1) {
100 al = SSL_AD_DECODE_ERROR;
101 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
102 @@ -1211,7 +1211,7 @@ int ssl3_get_client_hello(SSL *s)
106 - if ((p + i) > (d + n)) {
107 + if ((d + n) - p < i) {
108 /* not enough data */
109 al = SSL_AD_DECODE_ERROR;
110 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
111 diff --git a/ssl/ssl_sess.c b/ssl/ssl_sess.c
112 index b182998..54ee783 100644
115 @@ -573,7 +573,7 @@ int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
119 - if (session_id + len > limit) {
120 + if (limit - session_id < len) {
124 diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c
125 index fb64607..cdac011 100644
128 @@ -1867,11 +1867,11 @@ static void ssl_check_for_safari(SSL *s, const unsigned char *data,
129 0x02, 0x03, /* SHA-1/ECDSA */
132 - if (data >= (limit - 2))
133 + if (limit - data <= 2)
137 - if (data > (limit - 4))
138 + if (limit - data < 4)
142 @@ -1879,7 +1879,7 @@ static void ssl_check_for_safari(SSL *s, const unsigned char *data,
143 if (type != TLSEXT_TYPE_server_name)
146 - if (data + size > limit)
147 + if (limit - data < size)
151 @@ -1887,7 +1887,7 @@ static void ssl_check_for_safari(SSL *s, const unsigned char *data,
152 const size_t len1 = sizeof(kSafariExtensionsBlock);
153 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
155 - if (data + len1 + len2 != limit)
156 + if (limit - data != (int)(len1 + len2))
158 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
160 @@ -1896,7 +1896,7 @@ static void ssl_check_for_safari(SSL *s, const unsigned char *data,
162 const size_t len = sizeof(kSafariExtensionsBlock);
164 - if (data + len != limit)
165 + if (limit - data != (int)(len))
167 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
169 @@ -2053,19 +2053,19 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p,
173 - if (data > (limit - 2))
174 + if (limit - data < 2)
179 - if (data + len != limit)
180 + if (limit - data != len)
183 - while (data <= (limit - 4)) {
184 + while (limit - data >= 4) {
188 - if (data + size > (limit))
189 + if (limit - data < size)
192 fprintf(stderr, "Received extension type %d size %d\n", type, size);
193 @@ -2472,18 +2472,18 @@ static int ssl_scan_clienthello_custom_tlsext(SSL *s,
194 if (s->hit || s->cert->srv_ext.meths_count == 0)
197 - if (data >= limit - 2)
198 + if (limit - data <= 2)
202 - if (data > limit - len)
203 + if (limit - data < len)
206 - while (data <= limit - 4) {
207 + while (limit - data >= 4) {
211 - if (data + size > limit)
212 + if (limit - data < size)
214 if (custom_ext_parse(s, 1 /* server */ , type, data, size, al) <= 0)
216 @@ -2569,20 +2569,20 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p,
217 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
220 - if (data >= (d + n - 2))
221 + if ((d + n) - data <= 2)
225 - if (data + length != d + n) {
226 + if ((d + n) - data != length) {
227 *al = SSL_AD_DECODE_ERROR;
231 - while (data <= (d + n - 4)) {
232 + while ((d + n) - data >= 4) {
236 - if (data + size > (d + n))
237 + if ((d + n) - data < size)
240 if (s->tlsext_debug_cb)
241 @@ -3307,29 +3307,33 @@ int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
242 /* Skip past DTLS cookie */
243 if (SSL_IS_DTLS(s)) {
248 + if (limit - p <= i)
253 /* Skip past cipher list */
257 + if (limit - p <= i)
261 /* Skip past compression algorithm list */
269 /* Now at start of extensions */
270 - if ((p + 2) >= limit)
271 + if (limit - p <= 2)
274 - while ((p + 4) <= limit) {
275 + while (limit - p >= 4) {
276 unsigned short type, size;
279 - if (p + size > limit)
280 + if (limit - p < size)
282 if (type == TLSEXT_TYPE_session_ticket) {