Blame SOURCES/cyrus-sasl-2.1.27-Add-support-for-setting-max-ssf-0-to-GSS-SPNEGO.patch

b0654d
From 49e965f41257a0ed299c58a7cf1c120ddf944aaa Mon Sep 17 00:00:00 2001
b0654d
From: Simo Sorce <simo@redhat.com>
b0654d
Date: Tue, 5 May 2020 14:51:36 -0400
b0654d
Subject: [PATCH] Add support for setting max ssf 0 to GSS-SPNEGO
b0654d
b0654d
Bacport form this proposed PR (still open at bacport time):
b0654d
https://github.com/cyrusimap/cyrus-sasl/pull/603
b0654d
b0654d
Signed-off-by: Simo Sorce <simo@redhat.com>
b0654d
---
b0654d
 m4/sasl2.m4          | 13 +++++++
b0654d
 plugins/gssapi.c     | 44 ++++++++++++++++++++-
b0654d
 tests/runtests.py    | 91 ++++++++++++++++++++++++++++++++++++++++----
b0654d
 tests/t_common.c     | 13 ++++---
b0654d
 tests/t_common.h     |  3 +-
b0654d
 tests/t_gssapi_cli.c | 25 ++++++++++--
b0654d
 tests/t_gssapi_srv.c | 28 +++++++++++---
b0654d
 7 files changed, 194 insertions(+), 23 deletions(-)
b0654d
b0654d
diff --git a/m4/sasl2.m4 b/m4/sasl2.m4
b0654d
index 56e0504..6effe99 100644
b0654d
--- a/m4/sasl2.m4
b0654d
+++ b/m4/sasl2.m4
b0654d
@@ -287,6 +287,19 @@ if test "$gssapi" != no; then
b0654d
   AC_CHECK_FUNCS(gss_oid_equal)
b0654d
   LIBS="$cmu_save_LIBS"
b0654d
 
b0654d
+  cmu_save_LIBS="$LIBS"
b0654d
+  LIBS="$LIBS $GSSAPIBASE_LIBS"
b0654d
+  if test "$ac_cv_header_gssapi_gssapi_krb5_h" = "yes"; then
b0654d
+    AC_CHECK_DECL(GSS_KRB5_CRED_NO_CI_FLAGS_X,
b0654d
+                  [AC_DEFINE(HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X,1,
b0654d
+                             [Define if your GSSAPI implementation supports GSS_KRB5_CRED_NO_CI_FLAGS_X])],,
b0654d
+                  [
b0654d
+                    AC_INCLUDES_DEFAULT
b0654d
+                    #include <gssapi/gssapi_krb5.h>
b0654d
+                    ])
b0654d
+  fi
b0654d
+  LIBS="$cmu_save_LIBS"
b0654d
+
b0654d
   cmu_save_LIBS="$LIBS"
b0654d
   LIBS="$LIBS $GSSAPIBASE_LIBS"
b0654d
   AC_CHECK_FUNCS(gss_get_name_attribute)
b0654d
diff --git a/plugins/gssapi.c b/plugins/gssapi.c
b0654d
index 5d900c5..7480316 100644
b0654d
--- a/plugins/gssapi.c
b0654d
+++ b/plugins/gssapi.c
b0654d
@@ -1783,7 +1783,49 @@ static int gssapi_client_mech_step(void *conn_context,
b0654d
 		/* We want to try for privacy */
b0654d
 		req_flags |= GSS_C_CONF_FLAG;
b0654d
 	    }
b0654d
-	}
b0654d
+#ifdef HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X
b0654d
+        /* The krb5 mechanism automatically adds INTEG and CONF flags even when
b0654d
+         * not specified, this has the effect of rendering explicit requests
b0654d
+         * of no confidentiality and integrity via setting maxssf 0 moot.
b0654d
+         * However to interoperate with Windows machines it needs to be
b0654d
+         * possible to unset these flags as Windows machines refuse to allow
b0654d
+         * two layers (say TLS and GSSAPI) to both provide these services.
b0654d
+         * So if we do not suppress these flags a SASL/GSS-SPNEGO negotiation
b0654d
+         * over, say, LDAPS will fail against Windows Servers */
b0654d
+	} else if (params->props.max_ssf == 0) {
b0654d
+            gss_buffer_desc empty_buffer = GSS_C_EMPTY_BUFFER;
b0654d
+            if (client_creds == GSS_C_NO_CREDENTIAL) {
b0654d
+                gss_OID_set_desc mechs = { 0 };
b0654d
+                gss_OID_set desired_mechs = GSS_C_NO_OID_SET;
b0654d
+                if (text->mech_type != GSS_C_NO_OID) {
b0654d
+                    mechs.count = 1;
b0654d
+                    mechs.elements = text->mech_type;
b0654d
+                    desired_mechs = &mechs;
b0654d
+                }
b0654d
+
b0654d
+                maj_stat = gss_acquire_cred(&min_stat, GSS_C_NO_NAME,
b0654d
+                                            GSS_C_INDEFINITE, desired_mechs,
b0654d
+                                            GSS_C_INITIATE,
b0654d
+                                            &text->client_creds, NULL, NULL);
b0654d
+                if (GSS_ERROR(maj_stat)) {
b0654d
+                    sasl_gss_seterror(text->utils, maj_stat, min_stat);
b0654d
+                    sasl_gss_free_context_contents(text);
b0654d
+                    return SASL_FAIL;
b0654d
+                }
b0654d
+                client_creds = text->client_creds;
b0654d
+            }
b0654d
+
b0654d
+            maj_stat = gss_set_cred_option(&min_stat, &client_creds,
b0654d
+                                           (gss_OID)GSS_KRB5_CRED_NO_CI_FLAGS_X,
b0654d
+                                            &empty_buffer);
b0654d
+            if (GSS_ERROR(maj_stat)) {
b0654d
+                sasl_gss_seterror(text->utils, maj_stat, min_stat);
b0654d
+                sasl_gss_free_context_contents(text);
b0654d
+                return SASL_FAIL;
b0654d
+            }
b0654d
+#endif
b0654d
+        }
b0654d
+
b0654d
 
b0654d
 	if (params->props.security_flags & SASL_SEC_PASS_CREDENTIALS) {
b0654d
 	    req_flags = req_flags |  GSS_C_DELEG_FLAG;
b0654d
diff --git a/tests/runtests.py b/tests/runtests.py
b0654d
index fc9cf24..4106401 100755
b0654d
--- a/tests/runtests.py
b0654d
+++ b/tests/runtests.py
b0654d
@@ -6,6 +6,7 @@ import os
b0654d
 import shutil
b0654d
 import signal
b0654d
 import subprocess
b0654d
+import sys
b0654d
 import time
b0654d
 from string import Template
b0654d
 
b0654d
@@ -149,11 +150,12 @@ def gssapi_basic_test(kenv):
b0654d
                 srv.returncode, srv.stderr.read().decode('utf-8')))
b0654d
     except Exception as e:
b0654d
         print("FAIL: {}".format(e))
b0654d
-        return
b0654d
+        return 1
b0654d
 
b0654d
     print("PASS: CLI({}) SRV({})".format(
b0654d
         cli.stdout.read().decode('utf-8').strip(),
b0654d
         srv.stdout.read().decode('utf-8').strip()))
b0654d
+    return 0
b0654d
 
b0654d
 def gssapi_channel_binding_test(kenv):
b0654d
     try:
b0654d
@@ -178,11 +180,12 @@ def gssapi_channel_binding_test(kenv):
b0654d
                 srv.returncode, srv.stderr.read().decode('utf-8')))
b0654d
     except Exception as e:
b0654d
         print("FAIL: {}".format(e))
b0654d
-        return
b0654d
+        return 1
b0654d
 
b0654d
     print("PASS: CLI({}) SRV({})".format(
b0654d
         cli.stdout.read().decode('utf-8').strip(),
b0654d
         srv.stdout.read().decode('utf-8').strip()))
b0654d
+    return 0
b0654d
 
b0654d
 def gssapi_channel_binding_mismatch_test(kenv):
b0654d
     result = "FAIL"
b0654d
@@ -212,11 +215,70 @@ def gssapi_channel_binding_mismatch_test(kenv):
b0654d
                 cli.returncode, cli_err, srv.returncode, srv_err))
b0654d
     except Exception as e:
b0654d
         print("{}: {}".format(result, e))
b0654d
-        return
b0654d
+        return 0
b0654d
 
b0654d
     print("FAIL: This test should fail [CLI({}) SRV({})]".format(
b0654d
         cli.stdout.read().decode('utf-8').strip(),
b0654d
         srv.stdout.read().decode('utf-8').strip()))
b0654d
+    return 1
b0654d
+
b0654d
+def gss_spnego_basic_test(kenv):
b0654d
+    try:
b0654d
+        srv = subprocess.Popen(["../tests/t_gssapi_srv", "-N"],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        srv.stdout.readline() # Wait for srv to say it is ready
b0654d
+        cli = subprocess.Popen(["../tests/t_gssapi_cli", "-N"],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        try:
b0654d
+            cli.wait(timeout=5)
b0654d
+            srv.wait(timeout=5)
b0654d
+        except Exception as e:
b0654d
+            print("Failed on {}".format(e));
b0654d
+            cli.kill()
b0654d
+            srv.kill()
b0654d
+        if cli.returncode != 0 or srv.returncode != 0:
b0654d
+            raise Exception("CLI ({}): {} --> SRV ({}): {}".format(
b0654d
+                cli.returncode, cli.stderr.read().decode('utf-8'),
b0654d
+                srv.returncode, srv.stderr.read().decode('utf-8')))
b0654d
+    except Exception as e:
b0654d
+        print("FAIL: {}".format(e))
b0654d
+        return 1
b0654d
+
b0654d
+    print("PASS: CLI({}) SRV({})".format(
b0654d
+        cli.stdout.read().decode('utf-8').strip(),
b0654d
+        srv.stdout.read().decode('utf-8').strip()))
b0654d
+    return 0
b0654d
+
b0654d
+def gss_spnego_zeromaxssf_test(kenv):
b0654d
+    try:
b0654d
+        srv = subprocess.Popen(["../tests/t_gssapi_srv", "-N", "-z"],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        srv.stdout.readline() # Wait for srv to say it is ready
b0654d
+        cli = subprocess.Popen(["../tests/t_gssapi_cli", "-N", "-z"],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        try:
b0654d
+            cli.wait(timeout=5)
b0654d
+            srv.wait(timeout=5)
b0654d
+        except Exception as e:
b0654d
+            print("Failed on {}".format(e));
b0654d
+            cli.kill()
b0654d
+            srv.kill()
b0654d
+        if cli.returncode != 0 or srv.returncode != 0:
b0654d
+            raise Exception("CLI ({}): {} --> SRV ({}): {}".format(
b0654d
+                cli.returncode, cli.stderr.read().decode('utf-8'),
b0654d
+                srv.returncode, srv.stderr.read().decode('utf-8')))
b0654d
+    except Exception as e:
b0654d
+        print("FAIL: {}".format(e))
b0654d
+        return 1
b0654d
+
b0654d
+    print("PASS: CLI({}) SRV({})".format(
b0654d
+        cli.stdout.read().decode('utf-8').strip(),
b0654d
+        srv.stdout.read().decode('utf-8').strip()))
b0654d
+    return 0
b0654d
 
b0654d
 def gssapi_tests(testdir):
b0654d
     """ SASL/GSSAPI Tests """
b0654d
@@ -225,20 +287,32 @@ def gssapi_tests(testdir):
b0654d
     #print("KDC: {}, ENV: {}".format(kdc, kenv))
b0654d
     kenv['KRB5_TRACE'] = os.path.join(testdir, 'trace.log')
b0654d
 
b0654d
+    err = 0
b0654d
+
b0654d
     print('GSSAPI BASIC:')
b0654d
     print('    ', end='')
b0654d
-    gssapi_basic_test(kenv)
b0654d
+    err += gssapi_basic_test(kenv)
b0654d
 
b0654d
     print('GSSAPI CHANNEL BINDING:')
b0654d
     print('    ', end='')
b0654d
-    gssapi_channel_binding_test(kenv)
b0654d
+    err += gssapi_channel_binding_test(kenv)
b0654d
 
b0654d
     print('GSSAPI CHANNEL BINDING MISMTACH:')
b0654d
     print('    ', end='')
b0654d
-    gssapi_channel_binding_mismatch_test(kenv)
b0654d
+    err += gssapi_channel_binding_mismatch_test(kenv)
b0654d
+
b0654d
+    print('GSS-SPNEGO BASIC:')
b0654d
+    print('    ', end='')
b0654d
+    err += gss_spnego_basic_test(kenv)
b0654d
+
b0654d
+    print('GSS-SPNEGO 0 MAXSSF:')
b0654d
+    print('    ', end='')
b0654d
+    err += gss_spnego_zeromaxssf_test(kenv)
b0654d
 
b0654d
     os.killpg(kdc.pid, signal.SIGTERM)
b0654d
 
b0654d
+    return err
b0654d
+
b0654d
 
b0654d
 if __name__ == "__main__":
b0654d
 
b0654d
@@ -253,4 +327,7 @@ if __name__ == "__main__":
b0654d
         shutil.rmtree(T)
b0654d
     os.makedirs(T)
b0654d
 
b0654d
-    gssapi_tests(T)
b0654d
+    err = gssapi_tests(T)
b0654d
+    if err != 0:
b0654d
+        print('{} test(s) FAILED'.format(err))
b0654d
+        sys.exit(-1)
b0654d
diff --git a/tests/t_common.c b/tests/t_common.c
b0654d
index 478e6a1..f56098e 100644
b0654d
--- a/tests/t_common.c
b0654d
+++ b/tests/t_common.c
b0654d
@@ -23,20 +23,21 @@ void send_string(int sd, const char *s, unsigned int l)
b0654d
     if (ret != l) s_error("send data", ret, l, errno);
b0654d
 }
b0654d
 
b0654d
-void recv_string(int sd, char *buf, unsigned int *buflen)
b0654d
+void recv_string(int sd, char *buf, unsigned int *buflen, bool allow_eof)
b0654d
 {
b0654d
+    unsigned int bufsize = *buflen;
b0654d
     unsigned int l;
b0654d
     ssize_t ret;
b0654d
 
b0654d
+    *buflen = 0;
b0654d
+
b0654d
     ret = recv(sd, &l, sizeof(l), MSG_WAITALL);
b0654d
+    if (allow_eof && ret == 0) return;
b0654d
     if (ret != sizeof(l)) s_error("recv size", ret, sizeof(l), errno);
b0654d
 
b0654d
-    if (l == 0) {
b0654d
-        *buflen = 0;
b0654d
-        return;
b0654d
-    }
b0654d
+    if (l == 0) return;
b0654d
 
b0654d
-    if (*buflen < l) s_error("recv len", l, *buflen, E2BIG);
b0654d
+    if (bufsize < l) s_error("recv len", l, bufsize, E2BIG);
b0654d
 
b0654d
     ret = recv(sd, buf, l, 0);
b0654d
     if (ret != l) s_error("recv data", ret, l, errno);
b0654d
diff --git a/tests/t_common.h b/tests/t_common.h
b0654d
index a10def1..be24a53 100644
b0654d
--- a/tests/t_common.h
b0654d
+++ b/tests/t_common.h
b0654d
@@ -4,6 +4,7 @@
b0654d
 #include "config.h"
b0654d
 
b0654d
 #include <errno.h>
b0654d
+#include <stdbool.h>
b0654d
 #include <stdio.h>
b0654d
 #include <sys/socket.h>
b0654d
 
b0654d
@@ -12,7 +13,7 @@
b0654d
 
b0654d
 void s_error(const char *hdr, ssize_t ret, ssize_t len, int err);
b0654d
 void send_string(int sd, const char *s, unsigned int l);
b0654d
-void recv_string(int sd, char *buf, unsigned int *buflen);
b0654d
+void recv_string(int sd, char *buf, unsigned int *buflen, bool allow_eof);
b0654d
 void saslerr(int why, const char *what);
b0654d
 int getpath(void *context __attribute__((unused)), const char **path);
b0654d
 void parse_cb(sasl_channel_binding_t *cb, char *buf, unsigned max, char *in);
b0654d
diff --git a/tests/t_gssapi_cli.c b/tests/t_gssapi_cli.c
b0654d
index a44a3f5..d9eafe1 100644
b0654d
--- a/tests/t_gssapi_cli.c
b0654d
+++ b/tests/t_gssapi_cli.c
b0654d
@@ -46,12 +46,21 @@ int main(int argc, char *argv[])
b0654d
     char cb_buf[256];
b0654d
     int sd;
b0654d
     int c, r;
b0654d
+    const char *sasl_mech = "GSSAPI";
b0654d
+    bool spnego = false;
b0654d
+    bool zeromaxssf = false;
b0654d
 
b0654d
-    while ((c = getopt(argc, argv, "c:")) != EOF) {
b0654d
+    while ((c = getopt(argc, argv, "c:zN")) != EOF) {
b0654d
         switch (c) {
b0654d
         case 'c':
b0654d
             parse_cb(&cb, cb_buf, 256, optarg);
b0654d
             break;
b0654d
+        case 'z':
b0654d
+            zeromaxssf = true;
b0654d
+            break;
b0654d
+        case 'N':
b0654d
+            spnego = true;
b0654d
+            break;
b0654d
         default:
b0654d
             break;
b0654d
         }
b0654d
@@ -78,7 +87,17 @@ int main(int argc, char *argv[])
b0654d
         sasl_setprop(conn, SASL_CHANNEL_BINDING, &cb;;
b0654d
     }
b0654d
 
b0654d
-    r = sasl_client_start(conn, "GSSAPI", NULL, &data, &len, &chosenmech);
b0654d
+    if (spnego) {
b0654d
+        sasl_mech = "GSS-SPNEGO";
b0654d
+    }
b0654d
+
b0654d
+    if (zeromaxssf) {
b0654d
+        /* set all security properties to 0 including maxssf */
b0654d
+        sasl_security_properties_t secprops = { 0 };
b0654d
+        sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
b0654d
+    }
b0654d
+
b0654d
+    r = sasl_client_start(conn, sasl_mech, NULL, &data, &len, &chosenmech);
b0654d
     if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
 	saslerr(r, "starting SASL negotiation");
b0654d
 	printf("\n%s\n", sasl_errdetail(conn));
b0654d
@@ -90,7 +109,7 @@ int main(int argc, char *argv[])
b0654d
     while (r == SASL_CONTINUE) {
b0654d
         send_string(sd, data, len);
b0654d
         len = 8192;
b0654d
-        recv_string(sd, buf, &len;;
b0654d
+        recv_string(sd, buf, &len, false);
b0654d
 
b0654d
 	r = sasl_client_step(conn, buf, len, NULL, &data, &len;;
b0654d
 	if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
diff --git a/tests/t_gssapi_srv.c b/tests/t_gssapi_srv.c
b0654d
index ef1217f..448a218 100644
b0654d
--- a/tests/t_gssapi_srv.c
b0654d
+++ b/tests/t_gssapi_srv.c
b0654d
@@ -56,12 +56,21 @@ int main(int argc, char *argv[])
b0654d
     unsigned char cb_buf[256];
b0654d
     int sd;
b0654d
     int c, r;
b0654d
+    const char *sasl_mech = "GSSAPI";
b0654d
+    bool spnego = false;
b0654d
+    bool zeromaxssf = false;
b0654d
 
b0654d
-    while ((c = getopt(argc, argv, "c:")) != EOF) {
b0654d
+    while ((c = getopt(argc, argv, "c:zN")) != EOF) {
b0654d
         switch (c) {
b0654d
         case 'c':
b0654d
             parse_cb(&cb, cb_buf, 256, optarg);
b0654d
             break;
b0654d
+        case 'z':
b0654d
+            zeromaxssf = true;
b0654d
+            break;
b0654d
+        case 'N':
b0654d
+            spnego = true;
b0654d
+            break;
b0654d
         default:
b0654d
             break;
b0654d
         }
b0654d
@@ -90,12 +99,22 @@ int main(int argc, char *argv[])
b0654d
         sasl_setprop(conn, SASL_CHANNEL_BINDING, &cb;;
b0654d
     }
b0654d
 
b0654d
+    if (spnego) {
b0654d
+        sasl_mech = "GSS-SPNEGO";
b0654d
+    }
b0654d
+
b0654d
+    if (zeromaxssf) {
b0654d
+        /* set all security properties to 0 including maxssf */
b0654d
+        sasl_security_properties_t secprops = { 0 };
b0654d
+        sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
b0654d
+    }
b0654d
+
b0654d
     sd = setup_socket();
b0654d
 
b0654d
     len = 8192;
b0654d
-    recv_string(sd, buf, &len;;
b0654d
+    recv_string(sd, buf, &len, false);
b0654d
 
b0654d
-    r = sasl_server_start(conn, "GSSAPI", buf, len, &data, &len;;
b0654d
+    r = sasl_server_start(conn, sasl_mech, buf, len, &data, &len;;
b0654d
     if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
 	saslerr(r, "starting SASL negotiation");
b0654d
 	printf("\n%s\n", sasl_errdetail(conn));
b0654d
@@ -105,7 +124,7 @@ int main(int argc, char *argv[])
b0654d
     while (r == SASL_CONTINUE) {
b0654d
         send_string(sd, data, len);
b0654d
         len = 8192;
b0654d
-        recv_string(sd, buf, &len;;
b0654d
+        recv_string(sd, buf, &len, true);
b0654d
 
b0654d
 	r = sasl_server_step(conn, buf, len, &data, &len;;
b0654d
 	if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
@@ -113,7 +132,6 @@ int main(int argc, char *argv[])
b0654d
 	    printf("\n%s\n", sasl_errdetail(conn));
b0654d
 	    exit(-1);
b0654d
 	}
b0654d
-
b0654d
     }
b0654d
 
b0654d
     if (r != SASL_OK) exit(-1);
b0654d
-- 
b0654d
2.18.2
b0654d