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

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