Blame SOURCES/Fix-KDC-null-deref-on-bad-encrypted-challenge.patch

ab83d2
From 4e8579f0a41b66ed8029f21a52082e1c27ab3996 Mon Sep 17 00:00:00 2001
ab83d2
From: Joseph Sutton <josephsutton@catalyst.net.nz>
ab83d2
Date: Wed, 7 Jul 2021 11:47:44 +1200
ab83d2
Subject: [PATCH] Fix KDC null deref on bad encrypted challenge
ab83d2
ab83d2
The function ec_verify() in src/kdc/kdc_preauth_ec.c contains a check
ab83d2
to avoid further processing if the armor key is NULL.  However, this
ab83d2
check is bypassed by a call to k5memdup0() which overwrites retval
ab83d2
with 0 if the allocation succeeds.  If the armor key is NULL, a call
ab83d2
to krb5_c_fx_cf2_simple() will then dereference it, resulting in a
ab83d2
crash.  Add a check before the k5memdup0() call to avoid overwriting
ab83d2
retval.
ab83d2
ab83d2
CVE-2021-36222:
ab83d2
ab83d2
In MIT krb5 releases 1.16 and later, an unauthenticated attacker can
ab83d2
cause a null dereference in the KDC by sending a request containing a
ab83d2
PA-ENCRYPTED-CHALLENGE padata element without using FAST.
ab83d2
ab83d2
[ghudson@mit.edu: trimmed patch; added test case; edited commit
ab83d2
message]
ab83d2
ab83d2
(cherry picked from commit fc98f520caefff2e5ee9a0026fdf5109944b3562)
ab83d2
ab83d2
ticket: 9007
ab83d2
version_fixed: 1.18.4
ab83d2
ab83d2
(cherry picked from commit c4a406095b3ea4a67ae5b8ea586cbe9abdbae76f)
ab83d2
---
ab83d2
 src/kdc/kdc_preauth_ec.c      |  3 ++-
ab83d2
 src/tests/Makefile.in         |  1 +
ab83d2
 src/tests/t_cve-2021-36222.py | 46 +++++++++++++++++++++++++++++++++++
ab83d2
 3 files changed, 49 insertions(+), 1 deletion(-)
ab83d2
 create mode 100644 src/tests/t_cve-2021-36222.py
ab83d2
ab83d2
diff --git a/src/kdc/kdc_preauth_ec.c b/src/kdc/kdc_preauth_ec.c
ab83d2
index 7e636b3f9..43a9902cc 100644
ab83d2
--- a/src/kdc/kdc_preauth_ec.c
ab83d2
+++ b/src/kdc/kdc_preauth_ec.c
ab83d2
@@ -87,7 +87,8 @@ ec_verify(krb5_context context, krb5_data *req_pkt, krb5_kdc_req *request,
ab83d2
     }
ab83d2
 
ab83d2
     /* Check for a configured FAST ec auth indicator. */
ab83d2
-    realmstr = k5memdup0(realm.data, realm.length, &retval);
ab83d2
+    if (retval == 0)
ab83d2
+        realmstr = k5memdup0(realm.data, realm.length, &retval);
ab83d2
     if (realmstr != NULL)
ab83d2
         retval = profile_get_string(context->profile, KRB5_CONF_REALMS,
ab83d2
                                     realmstr,
ab83d2
diff --git a/src/tests/Makefile.in b/src/tests/Makefile.in
ab83d2
index 3f88f1713..0ffbebf56 100644
ab83d2
--- a/src/tests/Makefile.in
ab83d2
+++ b/src/tests/Makefile.in
ab83d2
@@ -158,6 +158,7 @@ check-pytests: unlockiter s4u2self
ab83d2
 	$(RUNPYTEST) $(srcdir)/t_cve-2012-1015.py $(PYTESTFLAGS)
ab83d2
 	$(RUNPYTEST) $(srcdir)/t_cve-2013-1416.py $(PYTESTFLAGS)
ab83d2
 	$(RUNPYTEST) $(srcdir)/t_cve-2013-1417.py $(PYTESTFLAGS)
ab83d2
+	$(RUNPYTEST) $(srcdir)/t_cve-2021-36222.py $(PYTESTFLAGS)
ab83d2
 	$(RM) au.log
ab83d2
 	$(RUNPYTEST) $(srcdir)/t_audit.py $(PYTESTFLAGS)
ab83d2
 	$(RUNPYTEST) $(srcdir)/jsonwalker.py -d $(srcdir)/au_dict.json \
ab83d2
diff --git a/src/tests/t_cve-2021-36222.py b/src/tests/t_cve-2021-36222.py
ab83d2
new file mode 100644
ab83d2
index 000000000..57e04993b
ab83d2
--- /dev/null
ab83d2
+++ b/src/tests/t_cve-2021-36222.py
ab83d2
@@ -0,0 +1,46 @@
ab83d2
+import socket
ab83d2
+from k5test import *
ab83d2
+
ab83d2
+realm = K5Realm()
ab83d2
+
ab83d2
+# CVE-2021-36222 KDC null dereference on encrypted challenge preauth
ab83d2
+# without FAST
ab83d2
+
ab83d2
+s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ab83d2
+a = (hostname, realm.portbase)
ab83d2
+
ab83d2
+m = ('6A81A0' '30819D'          # [APPLICATION 10] SEQUENCE
ab83d2
+     'A103' '0201' '05'         #  [1] pvno = 5
ab83d2
+     'A203' '0201' '0A'         #  [2] msg-type = 10
ab83d2
+     'A30E' '300C'              #  [3] padata = SEQUENCE OF
ab83d2
+     '300A'                     #   SEQUENCE
ab83d2
+     'A104' '0202' '008A'       #    [1] padata-type = PA-ENCRYPTED-CHALLENGE
ab83d2
+     'A202' '0400'              #    [2] padata-value = ""
ab83d2
+     'A48180' '307E'            #  [4] req-body = SEQUENCE
ab83d2
+     'A007' '0305' '0000000000' #   [0] kdc-options = 0
ab83d2
+     'A120' '301E'              #   [1] cname = SEQUENCE
ab83d2
+     'A003' '0201' '01'         #    [0] name-type = NT-PRINCIPAL
ab83d2
+     'A117' '3015'              #    [1] name-string = SEQUENCE-OF
ab83d2
+     '1B06' '6B7262746774'      #     krbtgt
ab83d2
+     '1B0B' '4B5242544553542E434F4D'
ab83d2
+                                #     KRBTEST.COM
ab83d2
+     'A20D' '1B0B' '4B5242544553542E434F4D'
ab83d2
+                                #   [2] realm = KRBTEST.COM
ab83d2
+     'A320' '301E'              #   [3] sname = SEQUENCE
ab83d2
+     'A003' '0201' '01'         #    [0] name-type = NT-PRINCIPAL
ab83d2
+     'A117' '3015'              #    [1] name-string = SEQUENCE-OF
ab83d2
+     '1B06' '6B7262746774'      #     krbtgt
ab83d2
+     '1B0B' '4B5242544553542E434F4D'
ab83d2
+                                #     KRBTEST.COM
ab83d2
+     'A511' '180F' '31393934303631303036303331375A'
ab83d2
+                                #   [5] till = 19940610060317Z
ab83d2
+     'A703' '0201' '00'         #   [7] nonce = 0
ab83d2
+     'A808' '3006'              #   [8] etype = SEQUENCE OF
ab83d2
+     '020112' '020111')         #    aes256-cts aes128-cts
ab83d2
+
ab83d2
+s.sendto(bytes.fromhex(m), a)
ab83d2
+
ab83d2
+# Make sure kinit still works.
ab83d2
+realm.kinit(realm.user_princ, password('user'))
ab83d2
+
ab83d2
+success('CVE-2021-36222 regression test')