Blame SOURCES/openssl-1.0.1e-cve-2016-0799.patch

5fee79
diff -up openssl-1.0.1e/crypto/bio/b_print.c.bio-printf openssl-1.0.1e/crypto/bio/b_print.c
5fee79
--- openssl-1.0.1e/crypto/bio/b_print.c.bio-printf	2013-02-11 16:26:04.000000000 +0100
5fee79
+++ openssl-1.0.1e/crypto/bio/b_print.c	2016-04-07 14:36:55.538117697 +0200
5fee79
@@ -125,14 +125,14 @@
5fee79
 #define LLONG long
5fee79
 #endif
5fee79
 
5fee79
-static void fmtstr     (char **, char **, size_t *, size_t *,
5fee79
+static int fmtstr(char **, char **, size_t *, size_t *,
5fee79
 			const char *, int, int, int);
5fee79
-static void fmtint     (char **, char **, size_t *, size_t *,
5fee79
+static int fmtint(char **, char **, size_t *, size_t *,
5fee79
 			LLONG, int, int, int, int);
5fee79
-static void fmtfp      (char **, char **, size_t *, size_t *,
5fee79
+static int fmtfp(char **, char **, size_t *, size_t *,
5fee79
 			LDOUBLE, int, int, int);
5fee79
-static void doapr_outch (char **, char **, size_t *, size_t *, int);
5fee79
-static void _dopr(char **sbuffer, char **buffer,
5fee79
+static int doapr_outch(char **, char **, size_t *, size_t *, int);
5fee79
+static int _dopr(char **sbuffer, char **buffer,
5fee79
 		  size_t *maxlen, size_t *retlen, int *truncated,
5fee79
 		  const char *format, va_list args);
5fee79
 
5fee79
@@ -165,7 +165,7 @@ static void _dopr(char **sbuffer, char *
5fee79
 #define char_to_int(p) (p - '0')
5fee79
 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
5fee79
 
5fee79
-static void
5fee79
+static int
5fee79
 _dopr(
5fee79
     char **sbuffer,
5fee79
     char **buffer,
5fee79
@@ -200,7 +200,8 @@ _dopr(
5fee79
             if (ch == '%')
5fee79
                 state = DP_S_FLAGS;
5fee79
             else
5fee79
-                doapr_outch(sbuffer,buffer, &currlen, maxlen, ch);
5fee79
+                if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
5fee79
+			return 0;
5fee79
             ch = *format++;
5fee79
             break;
5fee79
         case DP_S_FLAGS:
5fee79
@@ -306,8 +307,9 @@ _dopr(
5fee79
                     value = va_arg(args, int);
5fee79
                     break;
5fee79
                 }
5fee79
-                fmtint(sbuffer, buffer, &currlen, maxlen,
5fee79
-                       value, 10, min, max, flags);
5fee79
+                if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
5fee79
+                            max, flags))
5fee79
+                    return 0;
5fee79
                 break;
5fee79
             case 'X':
5fee79
                 flags |= DP_F_UP;
5fee79
@@ -332,17 +334,19 @@ _dopr(
5fee79
                         unsigned int);
5fee79
                     break;
5fee79
                 }
5fee79
-                fmtint(sbuffer, buffer, &currlen, maxlen, value,
5fee79
+                if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
5fee79
                        ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
5fee79
-                       min, max, flags);
5fee79
+                            min, max, flags))
5fee79
+                    return 0;
5fee79
                 break;
5fee79
             case 'f':
5fee79
                 if (cflags == DP_C_LDOUBLE)
5fee79
                     fvalue = va_arg(args, LDOUBLE);
5fee79
                 else
5fee79
                     fvalue = va_arg(args, double);
5fee79
-                fmtfp(sbuffer, buffer, &currlen, maxlen,
5fee79
-                      fvalue, min, max, flags);
5fee79
+                if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
5fee79
+                           flags))
5fee79
+                    return 0;
5fee79
                 break;
5fee79
             case 'E':
5fee79
                 flags |= DP_F_UP;
5fee79
@@ -361,8 +365,9 @@ _dopr(
5fee79
                     fvalue = va_arg(args, double);
5fee79
                 break;
5fee79
             case 'c':
5fee79
-                doapr_outch(sbuffer, buffer, &currlen, maxlen,
5fee79
-                    va_arg(args, int));
5fee79
+                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,
5fee79
+                            va_arg(args, int)))
5fee79
+                    return 0;
5fee79
                 break;
5fee79
             case 's':
5fee79
                 strvalue = va_arg(args, char *);
5fee79
@@ -372,13 +377,15 @@ _dopr(
5fee79
 		    else
5fee79
 			max = *maxlen;
5fee79
 		}
5fee79
-                fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
5fee79
-                       flags, min, max);
5fee79
+                if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
5fee79
+                            flags, min, max))
5fee79
+                    return 0;
5fee79
                 break;
5fee79
             case 'p':
5fee79
                 value = (long)va_arg(args, void *);
5fee79
-                fmtint(sbuffer, buffer, &currlen, maxlen,
5fee79
-                    value, 16, min, max, flags|DP_F_NUM);
5fee79
+                if (!fmtint(sbuffer, buffer, &currlen, maxlen,
5fee79
+                            value, 16, min, max, flags | DP_F_NUM))
5fee79
+                    return 0;
5fee79
                 break;
5fee79
             case 'n': /* XXX */
5fee79
                 if (cflags == DP_C_SHORT) {
5fee79
@@ -400,7 +407,8 @@ _dopr(
5fee79
                 }
5fee79
                 break;
5fee79
             case '%':
5fee79
-                doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
5fee79
+                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
5fee79
+                    return 0;
5fee79
                 break;
5fee79
             case 'w':
5fee79
                 /* not supported yet, treat as next char */
5fee79
@@ -424,12 +432,13 @@ _dopr(
5fee79
     *truncated = (currlen > *maxlen - 1);
5fee79
     if (*truncated)
5fee79
         currlen = *maxlen - 1;
5fee79
-    doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
5fee79
+    if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
5fee79
+        return 0;
5fee79
     *retlen = currlen - 1;
5fee79
-    return;
5fee79
+    return 1;
5fee79
 }
5fee79
 
5fee79
-static void
5fee79
+static int
5fee79
 fmtstr(
5fee79
     char **sbuffer,
5fee79
     char **buffer,
5fee79
@@ -440,36 +449,44 @@ fmtstr(
5fee79
     int min,
5fee79
     int max)
5fee79
 {
5fee79
-    int padlen, strln;
5fee79
+    int padlen;
5fee79
+    size_t strln;
5fee79
     int cnt = 0;
5fee79
 
5fee79
     if (value == 0)
5fee79
         value = "<NULL>";
5fee79
-    for (strln = 0; value[strln]; ++strln)
5fee79
-        ;
5fee79
+
5fee79
+    strln = strlen(value);
5fee79
+    if (strln > INT_MAX)
5fee79
+        strln = INT_MAX;
5fee79
+
5fee79
     padlen = min - strln;
5fee79
-    if (padlen < 0)
5fee79
+    if (min < 0 || padlen < 0)
5fee79
         padlen = 0;
5fee79
     if (flags & DP_F_MINUS)
5fee79
         padlen = -padlen;
5fee79
 
5fee79
     while ((padlen > 0) && (cnt < max)) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5fee79
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
5fee79
+            return 0;
5fee79
         --padlen;
5fee79
         ++cnt;
5fee79
     }
5fee79
     while (*value && (cnt < max)) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
5fee79
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
5fee79
+            return 0;
5fee79
         ++cnt;
5fee79
     }
5fee79
     while ((padlen < 0) && (cnt < max)) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5fee79
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
5fee79
+            return 0;
5fee79
         ++padlen;
5fee79
         ++cnt;
5fee79
     }
5fee79
+    return 1;
5fee79
 }
5fee79
 
5fee79
-static void
5fee79
+static int
5fee79
 fmtint(
5fee79
     char **sbuffer,
5fee79
     char **buffer,
5fee79
@@ -533,37 +550,44 @@ fmtint(
5fee79
 
5fee79
     /* spaces */
5fee79
     while (spadlen > 0) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5fee79
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
5fee79
+            return 0;
5fee79
         --spadlen;
5fee79
     }
5fee79
 
5fee79
     /* sign */
5fee79
     if (signvalue)
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
5fee79
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
5fee79
+            return 0;
5fee79
 
5fee79
     /* prefix */
5fee79
     while (*prefix) {
5fee79
-	doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
5fee79
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
5fee79
+            return 0;
5fee79
 	prefix++;
5fee79
     }
5fee79
 
5fee79
     /* zeros */
5fee79
     if (zpadlen > 0) {
5fee79
         while (zpadlen > 0) {
5fee79
-            doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
5fee79
+            if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
5fee79
+                return 0;
5fee79
             --zpadlen;
5fee79
         }
5fee79
     }
5fee79
     /* digits */
5fee79
-    while (place > 0)
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
5fee79
+    while (place > 0) {
5fee79
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
5fee79
+            return 0;
5fee79
+    }
5fee79
 
5fee79
     /* left justified spaces */
5fee79
     while (spadlen < 0) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5fee79
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
5fee79
+            return 0;
5fee79
         ++spadlen;
5fee79
     }
5fee79
-    return;
5fee79
+    return 1;
5fee79
 }
5fee79
 
5fee79
 static LDOUBLE
5fee79
@@ -597,7 +621,7 @@ roundv(LDOUBLE value)
5fee79
     return intpart;
5fee79
 }
5fee79
 
5fee79
-static void
5fee79
+static int
5fee79
 fmtfp(
5fee79
     char **sbuffer,
5fee79
     char **buffer,
5fee79
@@ -616,7 +640,6 @@ fmtfp(
5fee79
     int fplace = 0;
5fee79
     int padlen = 0;
5fee79
     int zpadlen = 0;
5fee79
-    int caps = 0;
5fee79
     long intpart;
5fee79
     long fracpart;
5fee79
     long max10;
5fee79
@@ -650,9 +673,7 @@ fmtfp(
5fee79
 
5fee79
     /* convert integer part */
5fee79
     do {
5fee79
-        iconvert[iplace++] =
5fee79
-            (caps ? "0123456789ABCDEF"
5fee79
-              : "0123456789abcdef")[intpart % 10];
5fee79
+        iconvert[iplace++] = "0123456789"[intpart % 10];
5fee79
         intpart = (intpart / 10);
5fee79
     } while (intpart && (iplace < (int)sizeof(iconvert)));
5fee79
     if (iplace == sizeof iconvert)
5fee79
@@ -661,9 +682,7 @@ fmtfp(
5fee79
 
5fee79
     /* convert fractional part */
5fee79
     do {
5fee79
-        fconvert[fplace++] =
5fee79
-            (caps ? "0123456789ABCDEF"
5fee79
-              : "0123456789abcdef")[fracpart % 10];
5fee79
+        fconvert[fplace++] = "0123456789"[fracpart % 10];
5fee79
         fracpart = (fracpart / 10);
5fee79
     } while (fplace < max);
5fee79
     if (fplace == sizeof fconvert)
5fee79
@@ -682,47 +701,61 @@ fmtfp(
5fee79
 
5fee79
     if ((flags & DP_F_ZERO) && (padlen > 0)) {
5fee79
         if (signvalue) {
5fee79
-            doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
5fee79
+            if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
5fee79
+                return 0;
5fee79
             --padlen;
5fee79
             signvalue = 0;
5fee79
         }
5fee79
         while (padlen > 0) {
5fee79
-            doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
5fee79
+            if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
5fee79
+                return 0;
5fee79
             --padlen;
5fee79
         }
5fee79
     }
5fee79
     while (padlen > 0) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5fee79
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
5fee79
+            return 0;
5fee79
         --padlen;
5fee79
     }
5fee79
-    if (signvalue)
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
5fee79
+    if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
5fee79
+        return 0;
5fee79
 
5fee79
-    while (iplace > 0)
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
5fee79
+    while (iplace > 0) {
5fee79
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
5fee79
+            return 0;
5fee79
+    }
5fee79
 
5fee79
     /*
5fee79
      * Decimal point. This should probably use locale to find the correct
5fee79
      * char to print out.
5fee79
      */
5fee79
     if (max > 0 || (flags & DP_F_NUM)) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
5fee79
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
5fee79
+            return 0;
5fee79
 
5fee79
-        while (fplace > 0)
5fee79
-            doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
5fee79
+        while (fplace > 0) {
5fee79
+            if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
5fee79
+                            fconvert[--fplace]))
5fee79
+                return 0;
5fee79
+        }
5fee79
     }
5fee79
     while (zpadlen > 0) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
5fee79
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
5fee79
+            return 0;
5fee79
         --zpadlen;
5fee79
     }
5fee79
 
5fee79
     while (padlen < 0) {
5fee79
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5fee79
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
5fee79
+            return 0;
5fee79
         ++padlen;
5fee79
     }
5fee79
+    return 1;
5fee79
 }
5fee79
 
5fee79
-static void
5fee79
+#define BUFFER_INC  1024
5fee79
+
5fee79
+static int
5fee79
 doapr_outch(
5fee79
     char **sbuffer,
5fee79
     char **buffer,
5fee79
@@ -733,24 +766,30 @@ doapr_outch(
5fee79
     /* If we haven't at least one buffer, someone has doe a big booboo */
5fee79
     assert(*sbuffer != NULL || buffer != NULL);
5fee79
 
5fee79
-    if (buffer) {
5fee79
-	while (*currlen >= *maxlen) {
5fee79
-	    if (*buffer == NULL) {
5fee79
-		if (*maxlen == 0)
5fee79
-		    *maxlen = 1024;
5fee79
+    /* |currlen| must always be <= |*maxlen| */
5fee79
+    assert(*currlen <= *maxlen);
5fee79
+
5fee79
+    if (buffer && *currlen == *maxlen) {
5fee79
+        if (*maxlen > INT_MAX - BUFFER_INC)
5fee79
+            return 0;
5fee79
+
5fee79
+        *maxlen += BUFFER_INC;
5fee79
+	if (*buffer == NULL) {
5fee79
 		*buffer = OPENSSL_malloc(*maxlen);
5fee79
+		if (*buffer == NULL)
5fee79
+	        	return 0;
5fee79
 		if (*currlen > 0) {
5fee79
 		    assert(*sbuffer != NULL);
5fee79
 		    memcpy(*buffer, *sbuffer, *currlen);
5fee79
 		}
5fee79
 		*sbuffer = NULL;
5fee79
-	    } else {
5fee79
-		*maxlen += 1024;
5fee79
-		*buffer = OPENSSL_realloc(*buffer, *maxlen);
5fee79
-	    }
5fee79
+	} else {
5fee79
+            char *tmpbuf;
5fee79
+            tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
5fee79
+            if (tmpbuf == NULL)
5fee79
+                return 0;
5fee79
+            *buffer = tmpbuf;
5fee79
 	}
5fee79
-	/* What to do if *buffer is NULL? */
5fee79
-	assert(*sbuffer != NULL || *buffer != NULL);
5fee79
     }
5fee79
 
5fee79
     if (*currlen < *maxlen) {
5fee79
@@ -760,7 +799,7 @@ doapr_outch(
5fee79
 	    (*buffer)[(*currlen)++] = (char)c;
5fee79
     }
5fee79
 
5fee79
-    return;
5fee79
+    return 1;
5fee79
 }
5fee79
 
5fee79
 /***************************************************************************/
5fee79
@@ -792,11 +831,15 @@ int BIO_vprintf (BIO *bio, const char *f
5fee79
 
5fee79
 	dynbuf = NULL;
5fee79
 	CRYPTO_push_info("doapr()");
5fee79
-	_dopr(&hugebufp, &dynbuf, &hugebufsize,
5fee79
-		&retlen, &ignored, format, args);
5fee79
+	if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
5fee79
+                args))
5fee79
+		{
5fee79
+		OPENSSL_free(dynbuf);
5fee79
+        	return -1;
5fee79
+		}
5fee79
 	if (dynbuf)
5fee79
 		{
5fee79
-		ret=BIO_write(bio, dynbuf, (int)retlen);
5fee79
+		ret = BIO_write(bio, dynbuf, (int)retlen);
5fee79
 		OPENSSL_free(dynbuf);
5fee79
 		}
5fee79
 	else
5fee79
@@ -829,7 +872,8 @@ int BIO_vsnprintf(char *buf, size_t n, c
5fee79
 	size_t retlen;
5fee79
 	int truncated;
5fee79
 
5fee79
-	_dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
5fee79
+	if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
5fee79
+		return -1;
5fee79
 
5fee79
 	if (truncated)
5fee79
 		/* In case of truncation, return -1 like traditional snprintf.