27013b
From 3c47e4adbed5e0a2e7f3993a24097889216a9d50 Mon Sep 17 00:00:00 2001
287c72
From: Greg Hudson <ghudson@mit.edu>
287c72
Date: Sat, 31 Oct 2020 17:07:05 -0400
287c72
Subject: [PATCH] Add recursion limit for ASN.1 indefinite lengths
287c72
287c72
The libkrb5 ASN.1 decoder supports BER indefinite lengths.  It
287c72
computes the tag length using recursion; the lack of a recursion limit
287c72
allows an attacker to overrun the stack and cause the process to
287c72
crash.  Reported by Demi Obenour.
287c72
287c72
CVE-2020-28196:
287c72
287c72
In MIT krb5 releases 1.11 and later, an unauthenticated attacker can
287c72
cause a denial of service for any client or server to which it can
287c72
send an ASN.1-encoded Kerberos message of sufficient length.
287c72
287c72
(cherry picked from commit 57415dda6cf04e73ffc3723be518eddfae599bfd)
287c72
287c72
ticket: 8959
287c72
version_fixed: 1.18.3
287c72
287c72
(cherry picked from commit 207ad69c87cf1b5c047d6c0c0165e5afe29700a6)
287c72
---
287c72
 src/lib/krb5/asn.1/asn1_encode.c | 16 +++++++++-------
287c72
 1 file changed, 9 insertions(+), 7 deletions(-)
287c72
287c72
diff --git a/src/lib/krb5/asn.1/asn1_encode.c b/src/lib/krb5/asn.1/asn1_encode.c
287c72
index a160cf4fe..cd6b879f7 100644
287c72
--- a/src/lib/krb5/asn.1/asn1_encode.c
287c72
+++ b/src/lib/krb5/asn.1/asn1_encode.c
287c72
@@ -356,7 +356,7 @@ make_tag(asn1buf *buf, const taginfo *t, size_t len)
287c72
 static krb5_error_code
287c72
 get_tag(const uint8_t *asn1, size_t len, taginfo *tag_out,
287c72
         const uint8_t **contents_out, size_t *clen_out,
287c72
-        const uint8_t **remainder_out, size_t *rlen_out)
287c72
+        const uint8_t **remainder_out, size_t *rlen_out, int recursion)
287c72
 {
287c72
     krb5_error_code ret;
287c72
     uint8_t o;
287c72
@@ -394,9 +394,11 @@ get_tag(const uint8_t *asn1, size_t len, taginfo *tag_out,
287c72
         /* Indefinite form (should not be present in DER, but we accept it). */
287c72
         if (tag_out->construction != CONSTRUCTED)
287c72
             return ASN1_MISMATCH_INDEF;
287c72
+        if (recursion >= 32)
287c72
+            return ASN1_OVERFLOW;
287c72
         p = asn1;
287c72
         while (!(len >= 2 && p[0] == 0 && p[1] == 0)) {
287c72
-            ret = get_tag(p, len, &t, &c, &clen, &p, &len;;
287c72
+            ret = get_tag(p, len, &t, &c, &clen, &p, &len, recursion + 1);
287c72
             if (ret)
287c72
                 return ret;
287c72
         }
287c72
@@ -613,7 +615,7 @@ split_der(asn1buf *buf, uint8_t *const *der, size_t len, taginfo *tag_out)
287c72
     const uint8_t *contents, *remainder;
287c72
     size_t clen, rlen;
287c72
 
287c72
-    ret = get_tag(*der, len, tag_out, &contents, &clen, &remainder, &rlen);
287c72
+    ret = get_tag(*der, len, tag_out, &contents, &clen, &remainder, &rlen, 0);
287c72
     if (ret)
287c72
         return ret;
287c72
     if (rlen != 0)
287c72
@@ -1199,7 +1201,7 @@ decode_atype(const taginfo *t, const uint8_t *asn1, size_t len,
287c72
         const uint8_t *rem;
287c72
         size_t rlen;
287c72
         if (!tag->implicit) {
287c72
-            ret = get_tag(asn1, len, &inner_tag, &asn1, &len, &rem, &rlen);
287c72
+            ret = get_tag(asn1, len, &inner_tag, &asn1, &len, &rem, &rlen, 0);
287c72
             if (ret)
287c72
                 return ret;
287c72
             /* Note: we don't check rlen (it should be 0). */
287c72
@@ -1420,7 +1422,7 @@ decode_sequence(const uint8_t *asn1, size_t len, const struct seq_info *seq,
287c72
     for (i = 0; i < seq->n_fields; i++) {
287c72
         if (len == 0)
287c72
             break;
287c72
-        ret = get_tag(asn1, len, &t, &contents, &clen, &asn1, &len;;
287c72
+        ret = get_tag(asn1, len, &t, &contents, &clen, &asn1, &len, 0);
287c72
         if (ret)
287c72
             goto error;
287c72
         /*
287c72
@@ -1478,7 +1480,7 @@ decode_sequence_of(const uint8_t *asn1, size_t len,
287c72
     *seq_out = NULL;
287c72
     *count_out = 0;
287c72
     while (len > 0) {
287c72
-        ret = get_tag(asn1, len, &t, &contents, &clen, &asn1, &len;;
287c72
+        ret = get_tag(asn1, len, &t, &contents, &clen, &asn1, &len, 0);
287c72
         if (ret)
287c72
             goto error;
287c72
         if (!check_atype_tag(elemtype, &t)) {
287c72
@@ -1584,7 +1586,7 @@ k5_asn1_full_decode(const krb5_data *code, const struct atype_info *a,
287c72
 
287c72
     *retrep = NULL;
287c72
     ret = get_tag((uint8_t *)code->data, code->length, &t, &contents,
287c72
-                  &clen, &remainder, &rlen);
287c72
+                  &clen, &remainder, &rlen, 0);
287c72
     if (ret)
287c72
         return ret;
287c72
     /* rlen should be 0, but we don't check it (and due to padding in