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

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