Blame SOURCES/krb5-1.11.3-copy_context.patch

7d335d
Adjusted for 1.11.3, which still had vtbl, locate_fptrs, and (vestigial)
7d335d
profile_in_memory fields, but didn't have localauth_handles,
7d335d
hostrealm_handles, or dns_canonicalize_hostname, and drop the hunk that
7d335d
touched .gitignore.
7d335d
7d335d
commit c452644d91d57d8b05ef396a029e34d0c7a48920
7d335d
Author: Greg Hudson <ghudson@mit.edu>
7d335d
Date:   Wed Dec 18 15:03:03 2013 -0500
7d335d
7d335d
    Fix krb5_copy_context
7d335d
    
7d335d
    krb5_copy_context has been broken since 1.8 (it broke in r22456)
7d335d
    because k5_copy_etypes crashes on null enctype lists.  Subsequent
7d335d
    additions to the context structure were not reflected in
7d335d
    krb5_copy_context, creating double-free bugs.  Make k5_copy_etypes
7d335d
    handle null input and account for all new fields in krb5_copy_context.
7d335d
    Reported by Arran Cudbard-Bell.
7d335d
    
7d335d
    ticket: 7807 (new)
7d335d
    target_version: 1.12.1
7d335d
    tags: pullup
7d335d
7d335d
diff --git a/src/lib/krb5/krb/copy_ctx.c b/src/lib/krb5/krb/copy_ctx.c
7d335d
index 0bc92f8..4237023 100644
7d335d
--- a/src/lib/krb5/krb/copy_ctx.c
7d335d
+++ b/src/lib/krb5/krb/copy_ctx.c
7d335d
@@ -77,13 +77,24 @@ krb5_copy_context(krb5_context ctx, krb5_context *nctx_out)
7d335d
     nctx->ser_ctx_count = 0;
7d335d
     nctx->ser_ctx = NULL;
7d335d
     nctx->prompt_types = NULL;
7d335d
+    nctx->preauth_context = NULL;
7d335d
+    nctx->ccselect_handles = NULL;
7d335d
+    nctx->kdblog_context = NULL;
7d335d
+    nctx->trace_callback = NULL;
7d335d
+    nctx->trace_callback_data = NULL;
7d335d
+    nctx->plugin_base_dir = NULL;
7d335d
     nctx->os_context.default_ccname = NULL;
7d335d
7d335d
+#ifdef KRB5_DNS_LOOKUP
7d335d
+    nctx->profile_in_memory = 0;
7d335d
+#endif /* KRB5_DNS_LOOKUP */
7d335d
+
7d335d
     memset(&nctx->libkrb5_plugins, 0, sizeof(nctx->libkrb5_plugins));
7d335d
     nctx->vtbl = NULL;
7d335d
     nctx->locate_fptrs = NULL;
7d335d
 
7d335d
     memset(&nctx->err, 0, sizeof(nctx->err));
7d335d
+    memset(&nctx->plugins, 0, sizeof(nctx->plugins));
7d335d
 
7d335d
     ret = k5_copy_etypes(ctx->in_tkt_etypes, &nctx->in_tkt_etypes);
7d335d
     if (ret)
7d335d
@@ -101,6 +109,11 @@ krb5_copy_context(krb5_context ctx, krb5_context *nctx_out)
7d335d
     ret = krb5_get_profile(ctx, &nctx->profile);
7d335d
     if (ret)
7d335d
         goto errout;
7d335d
+    nctx->plugin_base_dir = strdup(ctx->plugin_base_dir);
7d335d
+    if (nctx->plugin_base_dir == NULL) {
7d335d
+        ret = ENOMEM;
7d335d
+        goto errout;
7d335d
+    }
7d335d
 
7d335d
 errout:
7d335d
     if (ret) {
7d335d
diff --git a/src/lib/krb5/krb/etype_list.c b/src/lib/krb5/krb/etype_list.c
7d335d
index 9efe2e0..71f664f 100644
7d335d
--- a/src/lib/krb5/krb/etype_list.c
7d335d
+++ b/src/lib/krb5/krb/etype_list.c
7d335d
@@ -49,6 +49,8 @@ k5_copy_etypes(const krb5_enctype *old_list, krb5_enctype **new_list)
7d335d
     krb5_enctype *list;
7d335d
 
7d335d
     *new_list = NULL;
7d335d
+    if (old_list == NULL)
7d335d
+        return 0;
7d335d
     count = k5_count_etypes(old_list);
7d335d
     list = malloc(sizeof(krb5_enctype) * (count + 1));
7d335d
     if (list == NULL)
7d335d
7d335d
commit b78c3c8c5025aec870d20472f80d4a652062f921
7d335d
Author: Greg Hudson <ghudson@mit.edu>
7d335d
Date:   Wed Dec 18 13:08:25 2013 -0500
7d335d
7d335d
    Add a test program for krb5_copy_context
7d335d
    
7d335d
    This test program isn't completely proof against the kind of mistakes
7d335d
    we've made with krb5_copy_context in the past, but it at least
7d335d
    exercises krb5_copy_context and can detect some kinds of bugs.
7d335d
    
7d335d
    ticket: 7807
7d335d
7d335d
diff --git a/src/lib/krb5/krb/Makefile.in b/src/lib/krb5/krb/Makefile.in
7d335d
index 7d1682d..3b58219 100644
7d335d
--- a/src/lib/krb5/krb/Makefile.in
7d335d
+++ b/src/lib/krb5/krb/Makefile.in
7d335d
@@ -349,6 +349,7 @@ SRCS=	$(srcdir)/addr_comp.c	\
7d335d
 	$(srcdir)/t_expire_warn.c \
7d335d
 	$(srcdir)/t_authdata.c	\
7d335d
 	$(srcdir)/t_cc_config.c	\
7d335d
+	$(srcdir)/t_copy_context.c \
7d335d
 	$(srcdir)/t_in_ccache.c	\
7d335d
 	$(srcdir)/t_response_items.c \
7d335d
 	$(srcdir)/t_vfy_increds.c
7d335d
@@ -429,11 +430,14 @@ t_in_ccache: t_in_ccache.o $(KRB5_BASE_DEPLIBS)
7d335d
 t_cc_config: t_cc_config.o $(KRB5_BASE_DEPLIBS)
7d335d
 	$(CC_LINK) -o $@ t_cc_config.o $(KRB5_BASE_LIBS)
7d335d
 
7d335d
+t_copy_context: t_copy_context.o $(KRB5_BASE_DEPLIBS)
7d335d
+	$(CC_LINK) -o $@ t_copy_context.o $(KRB5_BASE_LIBS)
7d335d
+
7d335d
 t_response_items: t_response_items.o response_items.o $(KRB5_BASE_DEPLIBS)
7d335d
 	$(CC_LINK) -o $@ t_response_items.o response_items.o $(KRB5_BASE_LIBS)
7d335d
 
7d335d
 TEST_PROGS= t_walk_rtree t_kerb t_ser t_deltat t_expand t_authdata t_pac \
7d335d
-	t_in_ccache t_cc_config \
7d335d
+	t_in_ccache t_cc_config t_copy_context \
7d335d
 	t_princ t_etypes t_vfy_increds t_response_items
7d335d
 
7d335d
 check-unix:: $(TEST_PROGS)
7d335d
@@ -473,6 +477,8 @@ check-unix:: $(TEST_PROGS)
7d335d
 	$(RUN_SETUP) $(VALGRIND) ./t_princ
7d335d
 	$(RUN_SETUP) $(VALGRIND) ./t_etypes
7d335d
 	$(RUN_SETUP) $(VALGRIND) ./t_response_items
7d335d
+	KRB5_CONFIG=$(srcdir)/t_krb5.conf ; export KRB5_CONFIG ;\
7d335d
+		$(RUN_SETUP) $(VALGRIND) ./t_copy_context
7d335d
 
7d335d
 check-pytests:: t_expire_warn t_vfy_increds
7d335d
 	$(RUNPYTEST) $(srcdir)/t_expire_warn.py $(PYTESTFLAGS)
7d335d
@@ -491,6 +497,7 @@ clean::
7d335d
 		$(OUTPRE)t_pac$(EXEEXT) $(OUTPRE)t_pac.$(OBJEXT)	\
7d335d
 		$(OUTPRE)t_princ$(EXEEXT) $(OUTPRE)t_princ.$(OBJEXT)	\
7d335d
 	$(OUTPRE)t_authdata$(EXEEXT) $(OUTPRE)t_authdata.$(OBJEXT)	\
7d335d
+	$(OUTPRE)t_copy_context(EXEEXT) $(OUTPRE)t_copy_context.$(OBJEXT) \
7d335d
 	$(OUTPRE)t_vfy_increds$(EXEEXT) $(OUTPRE)t_vfy_increds.$(OBJEXT) \
7d335d
 	$(OUTPRE)t_response_items$(EXEEXT) $(OUTPRE)t_response_items.$(OBJEXT)
7d335d
 
7d335d
diff --git a/src/lib/krb5/krb/t_copy_context.c b/src/lib/krb5/krb/t_copy_context.c
7d335d
new file mode 100644
7d335d
index 0000000..522fa0c
7d335d
--- /dev/null
7d335d
+++ b/src/lib/krb5/krb/t_copy_context.c
7d335d
@@ -0,0 +1,162 @@
7d335d
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
7d335d
+/* lib/krb5/krb/t_copy_context.C - Test program for krb5_copy_context */
7d335d
+/*
7d335d
+ * Copyright (C) 2013 by the Massachusetts Institute of Technology.
7d335d
+ * All rights reserved.
7d335d
+ *
7d335d
+ * Redistribution and use in source and binary forms, with or without
7d335d
+ * modification, are permitted provided that the following conditions
7d335d
+ * are met:
7d335d
+ *
7d335d
+ * * Redistributions of source code must retain the above copyright
7d335d
+ *   notice, this list of conditions and the following disclaimer.
7d335d
+ *
7d335d
+ * * Redistributions in binary form must reproduce the above copyright
7d335d
+ *   notice, this list of conditions and the following disclaimer in
7d335d
+ *   the documentation and/or other materials provided with the
7d335d
+ *   distribution.
7d335d
+ *
7d335d
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7d335d
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7d335d
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
7d335d
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
7d335d
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
7d335d
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7d335d
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
7d335d
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
7d335d
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
7d335d
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
7d335d
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
7d335d
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
7d335d
+ */
7d335d
+
7d335d
+#include <k5-int.h>
7d335d
+
7d335d
+static void
7d335d
+trace(krb5_context ctx, const krb5_trace_info *info, void *data)
7d335d
+{
7d335d
+}
7d335d
+
7d335d
+static void
7d335d
+check(int cond)
7d335d
+{
7d335d
+    if (!cond)
7d335d
+        abort();
7d335d
+}
7d335d
+
7d335d
+static void
7d335d
+compare_string(const char *str1, const char *str2)
7d335d
+{
7d335d
+    check((str1 == NULL) == (str2 == NULL));
7d335d
+    if (str1 != NULL)
7d335d
+        check(strcmp(str1, str2) == 0);
7d335d
+}
7d335d
+
7d335d
+static void
7d335d
+compare_etypes(krb5_enctype *list1, krb5_enctype *list2)
7d335d
+{
7d335d
+    check((list1 == NULL) == (list2 == NULL));
7d335d
+    if (list1 == NULL)
7d335d
+        return;
7d335d
+    while (*list1 != ENCTYPE_NULL && *list1 == *list2)
7d335d
+        list1++, list2++;
7d335d
+    check(*list1 == *list2);
7d335d
+}
7d335d
+
7d335d
+/* Check that the context c is a valid copy of the reference context r. */
7d335d
+static void
7d335d
+check_context(krb5_context c, krb5_context r)
7d335d
+{
7d335d
+    int i;
7d335d
+
7d335d
+    /* Check fields which should have been propagated from r. */
7d335d
+    compare_etypes(c->in_tkt_etypes, r->in_tkt_etypes);
7d335d
+    compare_etypes(c->tgs_etypes, r->tgs_etypes);
7d335d
+    check(c->os_context.time_offset == r->os_context.time_offset);
7d335d
+    check(c->os_context.usec_offset == r->os_context.usec_offset);
7d335d
+    check(c->os_context.os_flags == r->os_context.os_flags);
7d335d
+    compare_string(c->os_context.default_ccname, r->os_context.default_ccname);
7d335d
+    check(c->clockskew == r->clockskew);
7d335d
+    check(c->kdc_req_sumtype == r->kdc_req_sumtype);
7d335d
+    check(c->default_ap_req_sumtype == r->default_ap_req_sumtype);
7d335d
+    check(c->default_safe_sumtype == r->default_safe_sumtype);
7d335d
+    check(c->kdc_default_options == r->kdc_default_options);
7d335d
+    check(c->library_options == r->library_options);
7d335d
+    check(c->profile_secure == r->profile_secure);
7d335d
+    check(c->fcc_default_format == r->fcc_default_format);
7d335d
+    check(c->udp_pref_limit == r->udp_pref_limit);
7d335d
+    check(c->use_conf_ktypes == r->use_conf_ktypes);
7d335d
+    check(c->allow_weak_crypto == r->allow_weak_crypto);
7d335d
+    check(c->ignore_acceptor_hostname == r->ignore_acceptor_hostname);
7d335d
+    compare_string(c->plugin_base_dir, r->plugin_base_dir);
7d335d
+
7d335d
+    /* Check fields which don't propagate. */
7d335d
+    check(c->dal_handle == NULL);
7d335d
+    check(c->ser_ctx_count == 0);
7d335d
+    check(c->ser_ctx == NULL);
7d335d
+    check(c->prompt_types == NULL);
7d335d
+    check(c->libkrb5_plugins.files == NULL);
7d335d
+    check(c->preauth_context == NULL);
7d335d
+    check(c->ccselect_handles == NULL);
7d335d
+    check(c->err.code == 0);
7d335d
+    check(c->err.msg == NULL);
7d335d
+    check(c->kdblog_context == NULL);
7d335d
+    check(c->trace_callback == NULL);
7d335d
+    check(c->trace_callback_data == NULL);
7d335d
+    for (i = 0; i < PLUGIN_NUM_INTERFACES; i++) {
7d335d
+        check(c->plugins[i].modules == NULL);
7d335d
+        check(!c->plugins[i].configured);
7d335d
+    }
7d335d
+}
7d335d
+
7d335d
+int
7d335d
+main(int argc, char **argv)
7d335d
+{
7d335d
+    krb5_context ctx, ctx2;
7d335d
+    krb5_plugin_initvt_fn *mods;
7d335d
+    const krb5_enctype etypes1[] = { ENCTYPE_DES3_CBC_SHA1, 0 };
7d335d
+    const krb5_enctype etypes2[] = { ENCTYPE_AES128_CTS_HMAC_SHA1_96,
7d335d
+                                     ENCTYPE_AES256_CTS_HMAC_SHA1_96, 0 };
7d335d
+    krb5_prompt_type ptypes[] = { KRB5_PROMPT_TYPE_PASSWORD };
7d335d
+
7d335d
+    /* Copy a default context and verify the result. */
7d335d
+    check(krb5_init_context(&ctx) == 0);
7d335d
+    check(krb5_copy_context(ctx, &ctx2) == 0);
7d335d
+    check_context(ctx2, ctx);
7d335d
+    krb5_free_context(ctx2);
7d335d
+
7d335d
+    /* Set non-default values for all of the propagated fields in ctx. */
7d335d
+    ctx->allow_weak_crypto = TRUE;
7d335d
+    check(krb5_set_default_in_tkt_ktypes(ctx, etypes1) == 0);
7d335d
+    check(krb5_set_default_tgs_enctypes(ctx, etypes2) == 0);
7d335d
+    check(krb5_set_debugging_time(ctx, 1234, 5678) == 0);
7d335d
+    check(krb5_cc_set_default_name(ctx, "defccname") == 0);
7d335d
+    check(krb5_set_default_realm(ctx, "defrealm") == 0);
7d335d
+    ctx->clockskew = 18;
7d335d
+    ctx->kdc_req_sumtype = CKSUMTYPE_NIST_SHA;
7d335d
+    ctx->default_ap_req_sumtype = CKSUMTYPE_HMAC_SHA1_96_AES128;
7d335d
+    ctx->default_safe_sumtype = CKSUMTYPE_HMAC_SHA1_96_AES256;
7d335d
+    ctx->kdc_default_options = KDC_OPT_FORWARDABLE;
7d335d
+    ctx->library_options = 0;
7d335d
+    ctx->profile_secure = TRUE;
7d335d
+    ctx->udp_pref_limit = 2345;
7d335d
+    ctx->use_conf_ktypes = TRUE;
7d335d
+    ctx->ignore_acceptor_hostname = TRUE;
7d335d
+    free(ctx->plugin_base_dir);
7d335d
+    check((ctx->plugin_base_dir = strdup("/a/b/c/d")) != NULL);
7d335d
+
7d335d
+    /* Also set some of the non-propagated fields. */
7d335d
+    ctx->prompt_types = ptypes;
7d335d
+    check(k5_plugin_load_all(ctx, PLUGIN_INTERFACE_PWQUAL, &mods) == 0);
7d335d
+    k5_plugin_free_modules(ctx, mods);
7d335d
+    krb5_set_error_message(ctx, ENOMEM, "nooooooooo");
7d335d
+    krb5_set_trace_callback(ctx, trace, ctx);
7d335d
+
7d335d
+    /* Copy the intentionally messy context and verify the result. */
7d335d
+    check(krb5_copy_context(ctx, &ctx2) == 0);
7d335d
+    check_context(ctx2, ctx);
7d335d
+    krb5_free_context(ctx2);
7d335d
+
7d335d
+    krb5_free_context(ctx);
7d335d
+    return 0;
7d335d
+}