Blame SOURCES/openssl-1.0.2k-cve-2018-0739.patch

aa8173
diff -up openssl-1.0.2k/crypto/asn1/asn1_err.c.asn1-recursive openssl-1.0.2k/crypto/asn1/asn1_err.c
aa8173
--- openssl-1.0.2k/crypto/asn1/asn1_err.c.asn1-recursive	2017-01-26 14:22:03.000000000 +0100
aa8173
+++ openssl-1.0.2k/crypto/asn1/asn1_err.c	2018-06-18 15:08:18.333412753 +0200
aa8173
@@ -279,6 +279,7 @@ static ERR_STRING_DATA ASN1_str_reasons[
aa8173
     {ERR_REASON(ASN1_R_MSTRING_NOT_UNIVERSAL), "mstring not universal"},
aa8173
     {ERR_REASON(ASN1_R_MSTRING_WRONG_TAG), "mstring wrong tag"},
aa8173
     {ERR_REASON(ASN1_R_NESTED_ASN1_STRING), "nested asn1 string"},
aa8173
+    {ERR_REASON(ASN1_R_NESTED_TOO_DEEP), "nested too deep"},
aa8173
     {ERR_REASON(ASN1_R_NON_HEX_CHARACTERS), "non hex characters"},
aa8173
     {ERR_REASON(ASN1_R_NOT_ASCII_FORMAT), "not ascii format"},
aa8173
     {ERR_REASON(ASN1_R_NOT_ENOUGH_DATA), "not enough data"},
aa8173
diff -up openssl-1.0.2k/crypto/asn1/asn1.h.asn1-recursive openssl-1.0.2k/crypto/asn1/asn1.h
aa8173
--- openssl-1.0.2k/crypto/asn1/asn1.h.asn1-recursive	2018-06-18 13:46:23.857127431 +0200
aa8173
+++ openssl-1.0.2k/crypto/asn1/asn1.h	2018-06-18 15:07:53.915826715 +0200
aa8173
@@ -1365,6 +1365,7 @@ void ERR_load_ASN1_strings(void);
aa8173
 # define ASN1_R_MSTRING_NOT_UNIVERSAL                     139
aa8173
 # define ASN1_R_MSTRING_WRONG_TAG                         140
aa8173
 # define ASN1_R_NESTED_ASN1_STRING                        197
aa8173
+# define ASN1_R_NESTED_TOO_DEEP                           219
aa8173
 # define ASN1_R_NON_HEX_CHARACTERS                        141
aa8173
 # define ASN1_R_NOT_ASCII_FORMAT                          190
aa8173
 # define ASN1_R_NOT_ENOUGH_DATA                           142
aa8173
diff -up openssl-1.0.2k/crypto/asn1/tasn_dec.c.asn1-recursive openssl-1.0.2k/crypto/asn1/tasn_dec.c
aa8173
--- openssl-1.0.2k/crypto/asn1/tasn_dec.c.asn1-recursive	2017-01-26 14:22:03.000000000 +0100
aa8173
+++ openssl-1.0.2k/crypto/asn1/tasn_dec.c	2018-06-18 15:14:28.978308482 +0200
aa8173
@@ -4,7 +4,7 @@
aa8173
  * 2000.
aa8173
  */
aa8173
 /* ====================================================================
aa8173
- * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
aa8173
+ * Copyright (c) 2000-2018 The OpenSSL Project.  All rights reserved.
aa8173
  *
aa8173
  * Redistribution and use in source and binary forms, with or without
aa8173
  * modification, are permitted provided that the following conditions
aa8173
@@ -65,6 +65,14 @@
aa8173
 #include <openssl/buffer.h>
aa8173
 #include <openssl/err.h>
aa8173
 
aa8173
+/*
aa8173
+ * Constructed types with a recursive definition (such as can be found in PKCS7)
aa8173
+ * could eventually exceed the stack given malicious input with excessive
aa8173
+ * recursion. Therefore we limit the stack depth. This is the maximum number of
aa8173
+ * recursive invocations of asn1_item_embed_d2i().
aa8173
+ */
aa8173
+#define ASN1_MAX_CONSTRUCTED_NEST 30
aa8173
+
aa8173
 static int asn1_check_eoc(const unsigned char **in, long len);
aa8173
 static int asn1_find_end(const unsigned char **in, long len, char inf);
aa8173
 
aa8173
@@ -81,11 +89,11 @@ static int asn1_check_tlen(long *olen, i
aa8173
 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
aa8173
                                 const unsigned char **in, long len,
aa8173
                                 const ASN1_TEMPLATE *tt, char opt,
aa8173
-                                ASN1_TLC *ctx);
aa8173
+                                ASN1_TLC *ctx, int depth);
aa8173
 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
aa8173
                                    const unsigned char **in, long len,
aa8173
                                    const ASN1_TEMPLATE *tt, char opt,
aa8173
-                                   ASN1_TLC *ctx);
aa8173
+                                   ASN1_TLC *ctx, int depth);
aa8173
 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
aa8173
                                  const unsigned char **in, long len,
aa8173
                                  const ASN1_ITEM *it,
aa8173
@@ -154,17 +162,16 @@ int ASN1_template_d2i(ASN1_VALUE **pval,
aa8173
 {
aa8173
     ASN1_TLC c;
aa8173
     asn1_tlc_clear_nc(&c);
aa8173
-    return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
aa8173
+    return asn1_template_ex_d2i(pval, in, len, tt, 0, &c, 0);
aa8173
 }
aa8173
 
aa8173
 /*
aa8173
  * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
aa8173
  * tag mismatch return -1 to handle OPTIONAL
aa8173
  */
aa8173
-
aa8173
-int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
aa8173
-                     const ASN1_ITEM *it,
aa8173
-                     int tag, int aclass, char opt, ASN1_TLC *ctx)
aa8173
+static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
aa8173
+                            long len, const ASN1_ITEM *it, int tag, int aclass,
aa8173
+                            char opt, ASN1_TLC *ctx, int depth)
aa8173
 {
aa8173
     const ASN1_TEMPLATE *tt, *errtt = NULL;
aa8173
     const ASN1_COMPAT_FUNCS *cf;
aa8173
@@ -189,6 +196,11 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval,
aa8173
     else
aa8173
         asn1_cb = 0;
aa8173
 
aa8173
+    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
aa8173
+        ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NESTED_TOO_DEEP);
aa8173
+        goto err;
aa8173
+    }
aa8173
+
aa8173
     switch (it->itype) {
aa8173
     case ASN1_ITYPE_PRIMITIVE:
aa8173
         if (it->templates) {
aa8173
@@ -204,7 +216,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval,
aa8173
                 goto err;
aa8173
             }
aa8173
             return asn1_template_ex_d2i(pval, in, len,
aa8173
-                                        it->templates, opt, ctx);
aa8173
+                                        it->templates, opt, ctx, depth);
aa8173
         }
aa8173
         return asn1_d2i_ex_primitive(pval, in, len, it,
aa8173
                                      tag, aclass, opt, ctx);
aa8173
@@ -326,7 +338,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval,
aa8173
             /*
aa8173
              * We mark field as OPTIONAL so its absence can be recognised.
aa8173
              */
aa8173
-            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
aa8173
+            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
aa8173
             /* If field not present, try the next one */
aa8173
             if (ret == -1)
aa8173
                 continue;
aa8173
@@ -444,7 +456,8 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval,
aa8173
              * attempt to read in field, allowing each to be OPTIONAL
aa8173
              */
aa8173
 
aa8173
-            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
aa8173
+            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
aa8173
+                                       depth);
aa8173
             if (!ret) {
aa8173
                 errtt = seqtt;
aa8173
                 goto err;
aa8173
@@ -514,6 +527,13 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval,
aa8173
     return 0;
aa8173
 }
aa8173
 
aa8173
+int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
aa8173
+                     const ASN1_ITEM *it,
aa8173
+                     int tag, int aclass, char opt, ASN1_TLC *ctx)
aa8173
+{
aa8173
+    return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
aa8173
+}
aa8173
+
aa8173
 /*
aa8173
  * Templates are handled with two separate functions. One handles any
aa8173
  * EXPLICIT tag and the other handles the rest.
aa8173
@@ -522,7 +542,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval,
aa8173
 static int asn1_template_ex_d2i(ASN1_VALUE **val,
aa8173
                                 const unsigned char **in, long inlen,
aa8173
                                 const ASN1_TEMPLATE *tt, char opt,
aa8173
-                                ASN1_TLC *ctx)
aa8173
+                                ASN1_TLC *ctx, int depth)
aa8173
 {
aa8173
     int flags, aclass;
aa8173
     int ret;
aa8173
@@ -557,7 +577,7 @@ static int asn1_template_ex_d2i(ASN1_VAL
aa8173
             return 0;
aa8173
         }
aa8173
         /* We've found the field so it can't be OPTIONAL now */
aa8173
-        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
aa8173
+        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
aa8173
         if (!ret) {
aa8173
             ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
aa8173
             return 0;
aa8173
@@ -581,7 +601,7 @@ static int asn1_template_ex_d2i(ASN1_VAL
aa8173
             }
aa8173
         }
aa8173
     } else
aa8173
-        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
aa8173
+        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
aa8173
 
aa8173
     *in = p;
aa8173
     return 1;
aa8173
@@ -594,7 +614,7 @@ static int asn1_template_ex_d2i(ASN1_VAL
aa8173
 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
aa8173
                                    const unsigned char **in, long len,
aa8173
                                    const ASN1_TEMPLATE *tt, char opt,
aa8173
-                                   ASN1_TLC *ctx)
aa8173
+                                   ASN1_TLC *ctx, int depth)
aa8173
 {
aa8173
     int flags, aclass;
aa8173
     int ret;
aa8173
@@ -665,14 +685,15 @@ static int asn1_template_noexp_d2i(ASN1_
aa8173
                 break;
aa8173
             }
aa8173
             skfield = NULL;
aa8173
-            if (!ASN1_item_ex_d2i(&skfield, &p, len,
aa8173
-                                  ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
aa8173
+            if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item),
aa8173
+                                  -1, 0, 0, ctx, depth)) {
aa8173
                 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
aa8173
                         ERR_R_NESTED_ASN1_ERROR);
aa8173
                 goto err;
aa8173
             }
aa8173
             len -= p - q;
aa8173
             if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
aa8173
+                ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item));
aa8173
                 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
aa8173
                 goto err;
aa8173
             }
aa8173
@@ -683,9 +704,8 @@ static int asn1_template_noexp_d2i(ASN1_
aa8173
         }
aa8173
     } else if (flags & ASN1_TFLG_IMPTAG) {
aa8173
         /* IMPLICIT tagging */
aa8173
-        ret = ASN1_item_ex_d2i(val, &p, len,
aa8173
-                               ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
aa8173
-                               ctx);
aa8173
+        ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag,
aa8173
+                               aclass, opt, ctx, depth);
aa8173
         if (!ret) {
aa8173
             ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
aa8173
             goto err;
aa8173
@@ -693,8 +713,9 @@ static int asn1_template_noexp_d2i(ASN1_
aa8173
             return -1;
aa8173
     } else {
aa8173
         /* Nothing special */
aa8173
-        ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
aa8173
-                               -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
aa8173
+        ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
aa8173
+                               -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx,
aa8173
+                               depth);
aa8173
         if (!ret) {
aa8173
             ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
aa8173
             goto err;