Check for overflows in i2d_ASN1_SET()
authorDr. Stephen Henson <steve@openssl.org>
Thu, 4 Aug 2016 12:54:51 +0000 (13:54 +0100)
committerDr. Stephen Henson <steve@openssl.org>
Thu, 4 Aug 2016 16:43:57 +0000 (17:43 +0100)
Thanks to Shi Lei for reporting this issue.

Reviewed-by: Rich Salz <rsalz@openssl.org>
(cherry picked from commit af601b83198771a4ad54ac0f415964b90aab4b5f)

crypto/asn1/a_set.c

index bf3f971889264385aa39b815a76fc6247a5fe5d8..5fb58655757dfd0361990032a1c5d9aa1e3a04a4 100644 (file)
@@ -57,6 +57,7 @@
  */
 
 #include <stdio.h>
+#include <limits.h>
 #include "cryptlib.h"
 #include <openssl/asn1_mac.h>
 
@@ -98,10 +99,14 @@ int i2d_ASN1_SET(STACK_OF(OPENSSL_BLOCK) *a, unsigned char **pp,
 
     if (a == NULL)
         return (0);
-    for (i = sk_OPENSSL_BLOCK_num(a) - 1; i >= 0; i--)
+    for (i = sk_OPENSSL_BLOCK_num(a) - 1; i >= 0; i--) {
+        int tmplen = i2d(sk_OPENSSL_BLOCK_value(a, i), NULL);
+        if (tmplen > INT_MAX - ret)
+            return -1;
         ret += i2d(sk_OPENSSL_BLOCK_value(a, i), NULL);
+    }
     r = ASN1_object_size(1, ret, ex_tag);
-    if (pp == NULL)
+    if (pp == NULL || r == -1)
         return (r);
 
     p = *pp;