167778
From 129e5a5694783bb033532e5933b2eeefabc5a35d Mon Sep 17 00:00:00 2001
167778
From: Greg Hudson <ghudson@mit.edu>
167778
Date: Fri, 13 Jan 2017 15:35:48 -0500
167778
Subject: [PATCH] Preserve method data in get_in_tkt.c
167778
167778
To continue after preauth failures, we need a persistent field in
167778
krb5_init_creds_context containing the METHOD-DATA from a
167778
KDC_PREAUTH_REQUIRED or KDC_PREAUTH_FAILED error.  If we overwrite
167778
this field with the padata in a KDC_MORE_PREAUTH_DATA_REQUIRED error,
167778
or conflate it with an optimistic padata list, we won't be able to
167778
correctly continue after a preauth failure.
167778
167778
In krb5_init_creds_context, split the preauth_to_use field into
167778
optimistic_padata, method_padata, and more_padata.  Separately handle
167778
KDC_ERR_MORE_PREAUTH_DATA_REQUIRED in init_creds_step_request() and
167778
init_creds_step_reply(), and separately handle optimistic preauth in
167778
init_creds_step_request().  Do not call k5_preauth() if none of the
167778
padata lists are set.
167778
167778
Also stop clearing ctx->err_reply when processing a
167778
KDC_ERR_PREAUTH_REQUIRED response.  Instead look for that error code
167778
in init_creds_step_request().  Eliminate the preauth_required field of
167778
krb5_init_creds_context as it can be inferred from whether we are
167778
performing optimistic preauth.
167778
167778
ticket: 8537
167778
(cherry picked from commit 97a9b0c4ef3fc7b20e6ae592201bcb132d58bbe5)
167778
---
167778
 src/include/k5-trace.h            | 11 +++++
167778
 src/lib/krb5/krb/get_in_tkt.c     | 71 +++++++++++++++++++++----------
167778
 src/lib/krb5/krb/init_creds_ctx.h |  5 ++-
167778
 3 files changed, 62 insertions(+), 25 deletions(-)
167778
167778
diff --git a/src/include/k5-trace.h b/src/include/k5-trace.h
167778
index 814da3195..e60ee0b75 100644
167778
--- a/src/include/k5-trace.h
167778
+++ b/src/include/k5-trace.h
167778
@@ -213,8 +213,19 @@ void krb5int_trace(krb5_context context, const char *fmt, ...);
167778
     TRACE(c, "Looked up etypes in keytab: {etypes}", etypes)
167778
 #define TRACE_INIT_CREDS_KEYTAB_LOOKUP_FAILED(c, code)          \
167778
     TRACE(c, "Couldn't lookup etypes in keytab: {kerr}", code)
167778
+#define TRACE_INIT_CREDS_PREAUTH(c)                     \
167778
+    TRACE(c, "Preauthenticating using KDC method data")
167778
 #define TRACE_INIT_CREDS_PREAUTH_DECRYPT_FAIL(c, code)                  \
167778
     TRACE(c, "Decrypt with preauth AS key failed: {kerr}", code)
167778
+#define TRACE_INIT_CREDS_PREAUTH_MORE(c, patype)                \
167778
+    TRACE(c, "Continuing preauth mech {int}", (int)patype)
167778
+#define TRACE_INIT_CREDS_PREAUTH_NONE(c)        \
167778
+    TRACE(c, "Sending unauthenticated request")
167778
+#define TRACE_INIT_CREDS_PREAUTH_OPTIMISTIC(c)  \
167778
+    TRACE(c, "Attempting optimistic preauth")
167778
+#define TRACE_INIT_CREDS_PREAUTH_TRYAGAIN(c, patype, code)              \
167778
+    TRACE(c, "Recovering from KDC error {int} using preauth mech {int}", \
167778
+          (int)patype, (int)code)
167778
 #define TRACE_INIT_CREDS_RESTART_FAST(c)        \
167778
     TRACE(c, "Restarting to upgrade to FAST")
167778
 #define TRACE_INIT_CREDS_RESTART_PREAUTH_FAILED(c)                      \
167778
diff --git a/src/lib/krb5/krb/get_in_tkt.c b/src/lib/krb5/krb/get_in_tkt.c
167778
index bc903b6e9..8c7919e65 100644
167778
--- a/src/lib/krb5/krb/get_in_tkt.c
167778
+++ b/src/lib/krb5/krb/get_in_tkt.c
167778
@@ -575,7 +575,9 @@ krb5_init_creds_free(krb5_context context,
167778
     krb5_free_data(context, ctx->inner_request_body);
167778
     krb5_free_data(context, ctx->encoded_previous_request);
167778
     krb5int_fast_free_state(context, ctx->fast_state);
167778
-    krb5_free_pa_data(context, ctx->preauth_to_use);
167778
+    krb5_free_pa_data(context, ctx->optimistic_padata);
167778
+    krb5_free_pa_data(context, ctx->method_padata);
167778
+    krb5_free_pa_data(context, ctx->more_padata);
167778
     krb5_free_data_contents(context, &ctx->salt);
167778
     krb5_free_data_contents(context, &ctx->s2kparams);
167778
     krb5_free_keyblock_contents(context, &ctx->as_key);
167778
@@ -827,10 +829,13 @@ restart_init_creds_loop(krb5_context context, krb5_init_creds_context ctx,
167778
 {
167778
     krb5_error_code code = 0;
167778
 
167778
-    krb5_free_pa_data(context, ctx->preauth_to_use);
167778
+    krb5_free_pa_data(context, ctx->optimistic_padata);
167778
+    krb5_free_pa_data(context, ctx->method_padata);
167778
+    krb5_free_pa_data(context, ctx->more_padata);
167778
     krb5_free_pa_data(context, ctx->err_padata);
167778
     krb5_free_error(context, ctx->err_reply);
167778
-    ctx->preauth_to_use = ctx->err_padata = NULL;
167778
+    ctx->optimistic_padata = ctx->method_padata = ctx->more_padata = NULL;
167778
+    ctx->err_padata = NULL;
167778
     ctx->err_reply = NULL;
167778
     ctx->selected_preauth_type = KRB5_PADATA_NONE;
167778
 
167778
@@ -849,7 +854,7 @@ restart_init_creds_loop(krb5_context context, krb5_init_creds_context ctx,
167778
     if (ctx->opt->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
167778
         code = make_preauth_list(context, ctx->opt->preauth_list,
167778
                                  ctx->opt->preauth_list_length,
167778
-                                 &ctx->preauth_to_use);
167778
+                                 &ctx->optimistic_padata);
167778
         if (code)
167778
             goto cleanup;
167778
     }
167778
@@ -1301,6 +1306,7 @@ init_creds_step_request(krb5_context context,
167778
                         krb5_data *out)
167778
 {
167778
     krb5_error_code code;
167778
+    krb5_preauthtype pa_type;
167778
 
167778
     if (ctx->loopcount >= MAX_IN_TKT_LOOPS) {
167778
         code = KRB5_GET_IN_TKT_LOOP;
167778
@@ -1331,17 +1337,36 @@ init_creds_step_request(krb5_context context,
167778
     read_cc_config_in_data(context, ctx);
167778
     clear_cc_config_out_data(context, ctx);
167778
 
167778
-    if (ctx->err_reply == NULL) {
167778
-        /* Either our first attempt, or retrying after KDC_ERR_PREAUTH_REQUIRED
167778
-         * or KDC_ERR_MORE_PREAUTH_DATA_REQUIRED. */
167778
-        code = k5_preauth(context, ctx, ctx->preauth_to_use,
167778
-                          ctx->preauth_required, &ctx->request->padata,
167778
-                          &ctx->selected_preauth_type);
167778
+    ctx->request->padata = NULL;
167778
+    if (ctx->optimistic_padata != NULL) {
167778
+        /* Our first attempt, using an optimistic padata list. */
167778
+        TRACE_INIT_CREDS_PREAUTH_OPTIMISTIC(context);
167778
+        code = k5_preauth(context, ctx, ctx->optimistic_padata, FALSE,
167778
+                          &ctx->request->padata, &ctx->selected_preauth_type);
167778
+        krb5_free_pa_data(context, ctx->optimistic_padata);
167778
+        ctx->optimistic_padata = NULL;
167778
         if (code != 0)
167778
             goto cleanup;
167778
-    } else {
167778
-        /* Retry after an error other than PREAUTH_NEEDED, using error padata
167778
+    } if (ctx->more_padata != NULL) {
167778
+        /* Continuing after KDC_ERR_MORE_PREAUTH_DATA_REQUIRED. */
167778
+        TRACE_INIT_CREDS_PREAUTH_MORE(context, ctx->selected_preauth_type);
167778
+        code = k5_preauth(context, ctx, ctx->more_padata, TRUE,
167778
+                          &ctx->request->padata, &pa_type);
167778
+        if (code != 0)
167778
+            goto cleanup;
167778
+    } else if (ctx->err_reply != NULL &&
167778
+               ctx->err_reply->error == KDC_ERR_PREAUTH_REQUIRED) {
167778
+        /* Continuing after KDC_ERR_PREAUTH_REQUIRED, using method data. */
167778
+        TRACE_INIT_CREDS_PREAUTH(context);
167778
+        code = k5_preauth(context, ctx, ctx->method_padata, TRUE,
167778
+                          &ctx->request->padata, &ctx->selected_preauth_type);
167778
+        if (code != 0)
167778
+            goto cleanup;
167778
+    } else if (ctx->err_reply != NULL) {
167778
+        /* Retry after an error other than PREAUTH_REQUIRED, using error padata
167778
          * to figure out what to change. */
167778
+        TRACE_INIT_CREDS_PREAUTH_TRYAGAIN(context, ctx->err_reply->error,
167778
+                                          ctx->selected_preauth_type);
167778
         code = k5_preauth_tryagain(context, ctx, ctx->selected_preauth_type,
167778
                                    ctx->err_reply, ctx->err_padata,
167778
                                    &ctx->request->padata);
167778
@@ -1351,6 +1376,8 @@ init_creds_step_request(krb5_context context,
167778
             goto cleanup;
167778
         }
167778
     }
167778
+    if (ctx->request->padata == NULL)
167778
+        TRACE_INIT_CREDS_PREAUTH_NONE(context);
167778
 
167778
     /* Remember when we sent this request (after any preauth delay). */
167778
     ctx->request_time = time(NULL);
167778
@@ -1467,8 +1494,9 @@ init_creds_step_reply(krb5_context context,
167778
         ctx->request->client->type == KRB5_NT_ENTERPRISE_PRINCIPAL;
167778
 
167778
     if (ctx->err_reply != NULL) {
167778
+        krb5_free_pa_data(context, ctx->more_padata);
167778
         krb5_free_pa_data(context, ctx->err_padata);
167778
-        ctx->err_padata = NULL;
167778
+        ctx->more_padata = ctx->err_padata = NULL;
167778
         code = krb5int_fast_process_error(context, ctx->fast_state,
167778
                                           &ctx->err_reply, &ctx->err_padata,
167778
                                           &retry);
167778
@@ -1494,21 +1522,18 @@ init_creds_step_reply(krb5_context context,
167778
              * FAST upgrade. */
167778
             ctx->restarted = FALSE;
167778
             code = restart_init_creds_loop(context, ctx, FALSE);
167778
-        } else if ((reply_code == KDC_ERR_MORE_PREAUTH_DATA_REQUIRED ||
167778
-                    reply_code == KDC_ERR_PREAUTH_REQUIRED) && retry) {
167778
-            krb5_free_pa_data(context, ctx->preauth_to_use);
167778
-            ctx->preauth_to_use = ctx->err_padata;
167778
+        } else if (reply_code == KDC_ERR_PREAUTH_REQUIRED && retry) {
167778
+            krb5_free_pa_data(context, ctx->method_padata);
167778
+            ctx->method_padata = ctx->err_padata;
167778
             ctx->err_padata = NULL;
167778
             note_req_timestamp(context, ctx, ctx->err_reply->stime,
167778
                                ctx->err_reply->susec);
167778
-            /* This will trigger a new call to k5_preauth(). */
167778
-            krb5_free_error(context, ctx->err_reply);
167778
-            ctx->err_reply = NULL;
167778
             code = sort_krb5_padata_sequence(context,
167778
                                              &ctx->request->client->realm,
167778
-                                             ctx->preauth_to_use);
167778
-            ctx->preauth_required = TRUE;
167778
-
167778
+                                             ctx->method_padata);
167778
+        } else if (reply_code == KDC_ERR_MORE_PREAUTH_DATA_REQUIRED && retry) {
167778
+            ctx->more_padata = ctx->err_padata;
167778
+            ctx->err_padata = NULL;
167778
         } else if (canon_flag && is_referral(context, ctx->err_reply,
167778
                                              ctx->request->client)) {
167778
             TRACE_INIT_CREDS_REFERRAL(context, &ctx->err_reply->client->realm);
167778
diff --git a/src/lib/krb5/krb/init_creds_ctx.h b/src/lib/krb5/krb/init_creds_ctx.h
167778
index 8c8b7494b..fe769685b 100644
167778
--- a/src/lib/krb5/krb/init_creds_ctx.h
167778
+++ b/src/lib/krb5/krb/init_creds_ctx.h
167778
@@ -50,7 +50,9 @@ struct _krb5_init_creds_context {
167778
     krb5_data *inner_request_body; /**< For preauth */
167778
     krb5_data *encoded_previous_request;
167778
     struct krb5int_fast_request_state *fast_state;
167778
-    krb5_pa_data **preauth_to_use;
167778
+    krb5_pa_data **optimistic_padata; /* from gic options */
167778
+    krb5_pa_data **method_padata; /* from PREAUTH_REQUIRED or PREAUTH_FAILED */
167778
+    krb5_pa_data **more_padata; /* from MORE_PREAUTH_DATA_REQUIRED */
167778
     krb5_boolean default_salt;
167778
     krb5_data salt;
167778
     krb5_data s2kparams;
167778
@@ -58,7 +60,6 @@ struct _krb5_init_creds_context {
167778
     krb5_enctype etype;
167778
     krb5_boolean enc_pa_rep_permitted;
167778
     krb5_boolean restarted;
167778
-    krb5_boolean preauth_required;
167778
     struct krb5_responder_context_st rctx;
167778
     krb5_preauthtype selected_preauth_type;
167778
     krb5_preauthtype allowed_preauth_type;