Blame SOURCES/ovt-Don-t-accept-tokens-with-unrelated-certs.patch

af4ca1
From c1be13f4eb5b3d24a273b6c8c9ca5421dcfac87b Mon Sep 17 00:00:00 2001
af4ca1
From: Katy Feng <fkaty@vmware.com>
af4ca1
Date: Tue, 17 Oct 2023 15:24:48 -0700
af4ca1
Subject: [PATCH 1/2] Don't accept tokens with unrelated certs
af4ca1
af4ca1
RH-Author: Ani Sinha <None>
af4ca1
RH-MergeRequest: 41: Don't accept tokens with unrelated certs
af4ca1
RH-Jira: RHEL-14642
af4ca1
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
af4ca1
RH-Commit: [1/1] e1be40a04b53412393050e1e6106e8edad8b1716
af4ca1
af4ca1
If a SAML token has a cert that's not a part of a chain,
af4ca1
fail the token as invalid.
af4ca1
af4ca1
CVE-2023-34058
af4ca1
(cherry picked from commit 1bfe23d728b74e08f4f65cd9b0093ca73937003a)
af4ca1
---
af4ca1
 open-vm-tools/vgauth/common/certverify.c      | 147 +++++++++++++++++-
af4ca1
 open-vm-tools/vgauth/common/certverify.h      |   6 +-
af4ca1
 open-vm-tools/vgauth/common/prefs.h           |   4 +-
af4ca1
 .../vgauth/serviceImpl/saml-xmlsec1.c         |  14 ++
af4ca1
 4 files changed, 168 insertions(+), 3 deletions(-)
af4ca1
af4ca1
diff --git a/open-vm-tools/vgauth/common/certverify.c b/open-vm-tools/vgauth/common/certverify.c
af4ca1
index db280e91..b84ebc7d 100644
af4ca1
--- a/open-vm-tools/vgauth/common/certverify.c
af4ca1
+++ b/open-vm-tools/vgauth/common/certverify.c
af4ca1
@@ -1,5 +1,5 @@
af4ca1
 /*********************************************************
af4ca1
- * Copyright (C) 2011-2016,2018-2019 VMware, Inc. All rights reserved.
af4ca1
+ * Copyright (c) 2011-2016, 2018-2019, 2021-2023 VMware, Inc. All rights reserved.
af4ca1
  *
af4ca1
  * This program is free software; you can redistribute it and/or modify it
af4ca1
  * under the terms of the GNU Lesser General Public License as published
af4ca1
@@ -893,3 +893,148 @@ done:
af4ca1
 
af4ca1
    return err;
af4ca1
 }
af4ca1
+
af4ca1
+
af4ca1
+/*
af4ca1
+ * Finds a cert with a subject (if checkSubj is set) or issuer (if
af4ca1
+ * checkSUbj is unset), matching 'val' in the list
af4ca1
+ * of certs.  Returns a match or NULL.
af4ca1
+ */
af4ca1
+
af4ca1
+static X509 *
af4ca1
+FindCert(GList *cList,
af4ca1
+         X509_NAME *val,
af4ca1
+         int checkSubj)
af4ca1
+{
af4ca1
+   GList *l;
af4ca1
+   X509 *c;
af4ca1
+   X509_NAME *v;
af4ca1
+
af4ca1
+   l = cList;
af4ca1
+   while (l != NULL) {
af4ca1
+      c = (X509 *) l->data;
af4ca1
+      if (checkSubj) {
af4ca1
+         v = X509_get_subject_name(c);
af4ca1
+      } else {
af4ca1
+         v = X509_get_issuer_name(c);
af4ca1
+      }
af4ca1
+      if (X509_NAME_cmp(val, v) == 0) {
af4ca1
+         return c;
af4ca1
+      }
af4ca1
+      l = l->next;
af4ca1
+   }
af4ca1
+   return NULL;
af4ca1
+}
af4ca1
+
af4ca1
+
af4ca1
+/*
af4ca1
+ ******************************************************************************
af4ca1
+ * CertVerify_CheckForUnrelatedCerts --                                  */ /**
af4ca1
+ *
af4ca1
+ * Looks over a list of certs.  If it finds that they are not all
af4ca1
+ * part of the same chain, returns failure.
af4ca1
+ *
af4ca1
+ * @param[in]     numCerts      The number of certs in the chain.
af4ca1
+ * @param[in]     pemCerts      The chain of certificates to verify.
af4ca1
+ *
af4ca1
+ * @return VGAUTH_E_OK on success, VGAUTH_E_FAIL if unrelated certs are found.
af4ca1
+ *
af4ca1
+ ******************************************************************************
af4ca1
+ */
af4ca1
+
af4ca1
+VGAuthError
af4ca1
+CertVerify_CheckForUnrelatedCerts(int numCerts,
af4ca1
+                                  const char **pemCerts)
af4ca1
+{
af4ca1
+   VGAuthError err = VGAUTH_E_FAIL;
af4ca1
+   int chainLen = 0;
af4ca1
+   int i;
af4ca1
+   X509 **certs = NULL;
af4ca1
+   GList *rawList = NULL;
af4ca1
+   X509 *baseCert;
af4ca1
+   X509 *curCert;
af4ca1
+   X509_NAME *subject;
af4ca1
+   X509_NAME *issuer;
af4ca1
+
af4ca1
+   /* common single cert case; nothing to do */
af4ca1
+   if (numCerts == 1) {
af4ca1
+      return VGAUTH_E_OK;
af4ca1
+   }
af4ca1
+
af4ca1
+   /* convert all PEM to X509 objects */
af4ca1
+   certs = g_malloc0(numCerts * sizeof(X509 *));
af4ca1
+   for (i = 0; i < numCerts; i++) {
af4ca1
+      certs[i] = CertStringToX509(pemCerts[i]);
af4ca1
+      if (NULL == certs[i]) {
af4ca1
+         g_warning("%s: failed to convert cert to X509\n", __FUNCTION__);
af4ca1
+         goto done;
af4ca1
+      }
af4ca1
+   }
af4ca1
+
af4ca1
+   /* choose the cert to start the chain.  shouldn't matter which */
af4ca1
+   baseCert = certs[0];
af4ca1
+
af4ca1
+   /* put the rest into a list */
af4ca1
+   for (i = 1; i < numCerts; i++) {
af4ca1
+      rawList = g_list_append(rawList, certs[i]);
af4ca1
+   }
af4ca1
+
af4ca1
+   /* now chase down to a leaf, looking for certs the baseCert issued */
af4ca1
+   subject = X509_get_subject_name(baseCert);
af4ca1
+   while ((curCert = FindCert(rawList, subject, 0)) != NULL) {
af4ca1
+      /* pull it from the list */
af4ca1
+      rawList = g_list_remove(rawList, curCert);
af4ca1
+      /* set up the next find */
af4ca1
+      subject = X509_get_subject_name(curCert);
af4ca1
+   }
af4ca1
+
af4ca1
+   /*
af4ca1
+    * walk up to the root cert, by finding a cert where the
af4ca1
+    * issuer equals the subject of the current
af4ca1
+    */
af4ca1
+   issuer = X509_get_issuer_name(baseCert);
af4ca1
+   while ((curCert = FindCert(rawList, issuer, 1)) != NULL) {
af4ca1
+      /* pull it from the list */
af4ca1
+      rawList = g_list_remove(rawList, curCert);
af4ca1
+      /* set up the next find */
af4ca1
+      issuer = X509_get_issuer_name(curCert);
af4ca1
+   }
af4ca1
+
af4ca1
+   /*
af4ca1
+    * At this point, anything on the list should be certs that are not part
af4ca1
+    * of the chain that includes the original 'baseCert'.
af4ca1
+    *
af4ca1
+    * For a valid token, the list should be empty.
af4ca1
+    */
af4ca1
+   chainLen = g_list_length(rawList);
af4ca1
+   if (chainLen != 0 ) {
af4ca1
+      GList *l;
af4ca1
+
af4ca1
+      g_warning("%s: %d unrelated certs found in list\n",
af4ca1
+                __FUNCTION__, chainLen);
af4ca1
+
af4ca1
+      /* debug helper */
af4ca1
+      l = rawList;
af4ca1
+      while (l != NULL) {
af4ca1
+         X509* c = (X509 *) l->data;
af4ca1
+         char *s = X509_NAME_oneline(X509_get_subject_name(c), NULL, 0);
af4ca1
+
af4ca1
+         g_debug("%s: unrelated cert subject: %s\n", __FUNCTION__, s);
af4ca1
+         free(s);
af4ca1
+         l = l->next;
af4ca1
+      }
af4ca1
+
af4ca1
+      goto done;
af4ca1
+   }
af4ca1
+
af4ca1
+   g_debug("%s: Success!  no unrelated certs found\n", __FUNCTION__);
af4ca1
+   err = VGAUTH_E_OK;
af4ca1
+
af4ca1
+done:
af4ca1
+   g_list_free(rawList);
af4ca1
+   for (i = 0; i < numCerts; i++) {
af4ca1
+      X509_free(certs[i]);
af4ca1
+   }
af4ca1
+   g_free(certs);
af4ca1
+   return err;
af4ca1
+}
af4ca1
diff --git a/open-vm-tools/vgauth/common/certverify.h b/open-vm-tools/vgauth/common/certverify.h
af4ca1
index 3fe1bcb4..228c143c 100644
af4ca1
--- a/open-vm-tools/vgauth/common/certverify.h
af4ca1
+++ b/open-vm-tools/vgauth/common/certverify.h
af4ca1
@@ -1,5 +1,5 @@
af4ca1
 /*********************************************************
af4ca1
- * Copyright (C) 2011-2016 VMware, Inc. All rights reserved.
af4ca1
+ * Copyright (C) 2011-2016, 2020, 2023 VMware, Inc. All rights reserved.
af4ca1
  *
af4ca1
  * This program is free software; you can redistribute it and/or modify it
af4ca1
  * under the terms of the GNU Lesser General Public License as published
af4ca1
@@ -66,6 +66,10 @@ VGAuthError CertVerify_CheckSignatureUsingCert(VGAuthHashAlg hash,
af4ca1
                                                size_t signatureLen,
af4ca1
                                                const unsigned char *signature);
af4ca1
 
af4ca1
+
af4ca1
+VGAuthError CertVerify_CheckForUnrelatedCerts(int numCerts,
af4ca1
+                                              const char **pemCerts);
af4ca1
+
af4ca1
 gchar * CertVerify_StripPEMCert(const gchar *pemCert);
af4ca1
 
af4ca1
 gchar * CertVerify_CertToX509String(const gchar *pemCert);
af4ca1
diff --git a/open-vm-tools/vgauth/common/prefs.h b/open-vm-tools/vgauth/common/prefs.h
af4ca1
index ff116928..6c58f3f4 100644
af4ca1
--- a/open-vm-tools/vgauth/common/prefs.h
af4ca1
+++ b/open-vm-tools/vgauth/common/prefs.h
af4ca1
@@ -1,5 +1,5 @@
af4ca1
 /*********************************************************
af4ca1
- * Copyright (C) 2011-2019 VMware, Inc. All rights reserved.
af4ca1
+ * Copyright (C) 2011-2019,2023 VMware, Inc. All rights reserved.
af4ca1
  *
af4ca1
  * This program is free software; you can redistribute it and/or modify it
af4ca1
  * under the terms of the GNU Lesser General Public License as published
af4ca1
@@ -136,6 +136,8 @@ msgCatalog = /etc/vmware-tools/vgauth/messages
af4ca1
 #define VGAUTH_PREF_ALIASSTORE_DIR         "aliasStoreDir"
af4ca1
 /** The number of seconds slack allowed in either direction in SAML token date checks. */
af4ca1
 #define VGAUTH_PREF_CLOCK_SKEW_SECS        "clockSkewAdjustment"
af4ca1
+/** If unrelated certificates are allowed in a SAML token */
af4ca1
+#define VGAUTH_PREF_ALLOW_UNRELATED_CERTS  "allowUnrelatedCerts"
af4ca1
 
af4ca1
 /** Ticket group name. */
af4ca1
 #define VGAUTH_PREF_GROUP_NAME_TICKET      "ticket"
af4ca1
diff --git a/open-vm-tools/vgauth/serviceImpl/saml-xmlsec1.c b/open-vm-tools/vgauth/serviceImpl/saml-xmlsec1.c
af4ca1
index 57db3b88..1c3e96e7 100644
af4ca1
--- a/open-vm-tools/vgauth/serviceImpl/saml-xmlsec1.c
af4ca1
+++ b/open-vm-tools/vgauth/serviceImpl/saml-xmlsec1.c
af4ca1
@@ -47,6 +47,7 @@
af4ca1
 #include "vmxlog.h"
af4ca1
 
af4ca1
 static int gClockSkewAdjustment = VGAUTH_PREF_DEFAULT_CLOCK_SKEW_SECS;
af4ca1
+static gboolean gAllowUnrelatedCerts = FALSE;
af4ca1
 static xmlSchemaPtr gParsedSchemas = NULL;
af4ca1
 static xmlSchemaValidCtxtPtr gSchemaValidateCtx = NULL;
af4ca1
 
af4ca1
@@ -313,6 +314,10 @@ LoadPrefs(void)
af4ca1
                                       VGAUTH_PREF_DEFAULT_CLOCK_SKEW_SECS);
af4ca1
     Log("%s: Allowing %d of clock skew for SAML date validation\n",
af4ca1
         __FUNCTION__, gClockSkewAdjustment);
af4ca1
+    gAllowUnrelatedCerts = Pref_GetBool(gPrefs,
af4ca1
+                                        VGAUTH_PREF_ALLOW_UNRELATED_CERTS,
af4ca1
+                                        VGAUTH_PREF_GROUP_NAME_SERVICE,
af4ca1
+                                        FALSE);
af4ca1
 }
af4ca1
 
af4ca1
 
af4ca1
@@ -1525,6 +1530,15 @@ SAML_VerifyBearerTokenAndChain(const char *xmlText,
af4ca1
       return VGAUTH_E_AUTHENTICATION_DENIED;
af4ca1
    }
af4ca1
 
af4ca1
+   if (!gAllowUnrelatedCerts) {
af4ca1
+      err = CertVerify_CheckForUnrelatedCerts(num, (const char **) certChain);
af4ca1
+      if (err != VGAUTH_E_OK) {
af4ca1
+         VMXLog_Log(VMXLOG_LEVEL_WARNING,
af4ca1
+                    "Unrelated certs found in SAML token, failing\n");
af4ca1
+         return VGAUTH_E_AUTHENTICATION_DENIED;
af4ca1
+      }
af4ca1
+   }
af4ca1
+
af4ca1
    subj.type = SUBJECT_TYPE_NAMED;
af4ca1
    subj.name = *subjNameOut;
af4ca1
    err = ServiceVerifyAndCheckTrustCertChainForSubject(num,
af4ca1
-- 
af4ca1
2.39.3
af4ca1