Blame SOURCES/Track-preauth-failures-instead-of-tries.patch

167778
From 4a8e9b806ce2fc1234504498fc54f36dd8b482f8 Mon Sep 17 00:00:00 2001
167778
From: Greg Hudson <ghudson@mit.edu>
167778
Date: Fri, 13 Jan 2017 12:16:04 -0500
167778
Subject: [PATCH] Track preauth failures instead of tries
167778
167778
In preauth2.c, instead of noting whenever we try a real preauth mech,
167778
note when a mechanism fails on our side.  Tracking only failures
167778
eliminates the need to reset the list for multi-step preauth exchanges
167778
or for processing padata in the AS-REP, but we will need the function
167778
later for continuing after optimistic preauth failures.
167778
167778
ticket: 8537
167778
(cherry picked from commit a1dc81d22304e77edaa8388c7d7d75cade81dc80)
167778
---
167778
 src/lib/krb5/krb/get_in_tkt.c |  3 --
167778
 src/lib/krb5/krb/int-proto.h  |  3 ++
167778
 src/lib/krb5/krb/preauth2.c   | 65 ++++++++++++++++++++---------------
167778
 3 files changed, 40 insertions(+), 31 deletions(-)
167778
167778
diff --git a/src/lib/krb5/krb/get_in_tkt.c b/src/lib/krb5/krb/get_in_tkt.c
167778
index 48dc00ea6..bc903b6e9 100644
167778
--- a/src/lib/krb5/krb/get_in_tkt.c
167778
+++ b/src/lib/krb5/krb/get_in_tkt.c
167778
@@ -1496,8 +1496,6 @@ init_creds_step_reply(krb5_context context,
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
-            /* reset the list of preauth types to try */
167778
-            k5_reset_preauth_types_tried(ctx);
167778
             krb5_free_pa_data(context, ctx->preauth_to_use);
167778
             ctx->preauth_to_use = ctx->err_padata;
167778
             ctx->err_padata = NULL;
167778
@@ -1547,7 +1545,6 @@ init_creds_step_reply(krb5_context context,
167778
         goto cleanup;
167778
 
167778
     /* process any preauth data in the as_reply */
167778
-    k5_reset_preauth_types_tried(ctx);
167778
     code = krb5int_fast_process_response(context, ctx->fast_state,
167778
                                          ctx->reply, &strengthen_key);
167778
     if (code != 0)
167778
diff --git a/src/lib/krb5/krb/int-proto.h b/src/lib/krb5/krb/int-proto.h
167778
index 8903df232..41a69c207 100644
167778
--- a/src/lib/krb5/krb/int-proto.h
167778
+++ b/src/lib/krb5/krb/int-proto.h
167778
@@ -197,6 +197,9 @@ k5_free_preauth_context(krb5_context context);
167778
 void
167778
 k5_reset_preauth_types_tried(krb5_init_creds_context ctx);
167778
 
167778
+krb5_error_code
167778
+k5_preauth_note_failed(krb5_init_creds_context ctx, krb5_preauthtype pa_type);
167778
+
167778
 void
167778
 k5_preauth_prepare_request(krb5_context context, krb5_get_init_creds_opt *opt,
167778
                            krb5_kdc_req *request);
167778
diff --git a/src/lib/krb5/krb/preauth2.c b/src/lib/krb5/krb/preauth2.c
167778
index 354234a93..17f2133b1 100644
167778
--- a/src/lib/krb5/krb/preauth2.c
167778
+++ b/src/lib/krb5/krb/preauth2.c
167778
@@ -54,7 +54,7 @@ struct krb5_preauth_context_st {
167778
 
167778
 struct krb5_preauth_req_context_st {
167778
     krb5_context orig_context;
167778
-    krb5_preauthtype *tried;
167778
+    krb5_preauthtype *failed;
167778
     krb5_clpreauth_modreq *modreqs;
167778
 };
167778
 
167778
@@ -201,11 +201,7 @@ cleanup:
167778
     free_handles(context, list);
167778
 }
167778
 
167778
-/*
167778
- * Reset the memory of which preauth types we have already tried, because we
167778
- * are entering a new phase of padata processing (such as the padata in an
167778
- * AS-REP).
167778
- */
167778
+/* Reset the memory of which preauth types we have already tried. */
167778
 void
167778
 k5_reset_preauth_types_tried(krb5_init_creds_context ctx)
167778
 {
167778
@@ -213,10 +209,27 @@ k5_reset_preauth_types_tried(krb5_init_creds_context ctx)
167778
 
167778
     if (reqctx == NULL)
167778
         return;
167778
-    free(reqctx->tried);
167778
-    reqctx->tried = NULL;
167778
+    free(reqctx->failed);
167778
+    reqctx->failed = NULL;
167778
 }
167778
 
167778
+/* Add pa_type to the list of types which has previously failed. */
167778
+krb5_error_code
167778
+k5_preauth_note_failed(krb5_init_creds_context ctx, krb5_preauthtype pa_type)
167778
+{
167778
+    krb5_preauth_req_context reqctx = ctx->preauth_reqctx;
167778
+    krb5_preauthtype *newptr;
167778
+    size_t i;
167778
+
167778
+    for (i = 0; reqctx->failed != NULL && reqctx->failed[i] != 0; i++);
167778
+    newptr = realloc(reqctx->failed, (i + 2) * sizeof(*newptr));
167778
+    if (newptr == NULL)
167778
+        return ENOMEM;
167778
+    reqctx->failed = newptr;
167778
+    reqctx->failed[i] = pa_type;
167778
+    reqctx->failed[i + 1] = 0;
167778
+    return 0;
167778
+}
167778
 
167778
 /* Free the per-krb5_context preauth_context. This means clearing any
167778
  * plugin-specific context which may have been created, and then
167778
@@ -291,7 +304,7 @@ k5_preauth_request_context_fini(krb5_context context,
167778
         TRACE_PREAUTH_WRONG_CONTEXT(context);
167778
     }
167778
     free(reqctx->modreqs);
167778
-    free(reqctx->tried);
167778
+    free(reqctx->failed);
167778
     free(reqctx);
167778
     ctx->preauth_reqctx = NULL;
167778
 }
167778
@@ -612,28 +625,17 @@ pa_type_allowed(krb5_init_creds_context ctx, krb5_preauthtype pa_type)
167778
         pa_type == ctx->allowed_preauth_type;
167778
 }
167778
 
167778
-/*
167778
- * If pa_type has already been tried as a real preauth type for this
167778
- * authentication, return true.  Otherwise ass pa_type to the list of tried
167778
- * types and return false.
167778
- */
167778
+/* Return true if pa_type previously failed during this authentication. */
167778
 static krb5_boolean
167778
-already_tried(krb5_init_creds_context ctx, krb5_preauthtype pa_type)
167778
+previously_failed(krb5_init_creds_context ctx, krb5_preauthtype pa_type)
167778
 {
167778
     krb5_preauth_req_context reqctx = ctx->preauth_reqctx;
167778
     size_t i;
167778
-    krb5_preauthtype *newptr;
167778
 
167778
-    for (i = 0; reqctx->tried != NULL && reqctx->tried[i] != 0; i++) {
167778
-        if (reqctx->tried[i] == pa_type)
167778
+    for (i = 0; reqctx->failed != NULL && reqctx->failed[i] != 0; i++) {
167778
+        if (reqctx->failed[i] == pa_type)
167778
             return TRUE;
167778
     }
167778
-    newptr = realloc(reqctx->tried, (i + 2) * sizeof(*newptr));
167778
-    if (newptr == NULL)
167778
-        return FALSE;
167778
-    reqctx->tried = newptr;
167778
-    reqctx->tried[i] = pa_type;
167778
-    reqctx->tried[i + 1] = ENCTYPE_NULL;
167778
     return FALSE;
167778
 }
167778
 
167778
@@ -665,8 +667,8 @@ process_pa_data(krb5_context context, krb5_init_creds_context ctx,
167778
             /* Make sure this type is for the current pass. */
167778
             if (clpreauth_is_real(context, h, pa->pa_type) != real)
167778
                 continue;
167778
-            /* Only try a real mechanism once per authentication. */
167778
-            if (real && already_tried(ctx, pa->pa_type))
167778
+            /* Don't try a real mechanism again after failure. */
167778
+            if (real && previously_failed(ctx, pa->pa_type))
167778
                 continue;
167778
             mod_pa = NULL;
167778
             ret = clpreauth_process(context, h, modreq, ctx->opt, &callbacks,
167778
@@ -694,6 +696,12 @@ process_pa_data(krb5_context context, krb5_init_creds_context ctx,
167778
                 /* Save the first error we get from a real preauth type. */
167778
                 k5_save_ctx_error(context, ret, &save);
167778
             }
167778
+            if (real && ret) {
167778
+                /* Don't try this mechanism again for this authentication. */
167778
+                ret = k5_preauth_note_failed(ctx, pa->pa_type);
167778
+                if (ret)
167778
+                    goto cleanup;
167778
+            }
167778
         }
167778
     }
167778
 
167778
@@ -944,9 +952,10 @@ k5_preauth_tryagain(krb5_context context, krb5_init_creds_context ctx,
167778
     TRACE_PREAUTH_TRYAGAIN(context, h->vt.name, pa_type, ret);
167778
     if (!ret && mod_pa == NULL)
167778
         ret = KRB5KRB_ERR_GENERIC;
167778
-    if (ret)
167778
+    if (ret) {
167778
+        k5_preauth_note_failed(ctx, pa_type);
167778
         return ret;
167778
-
167778
+    }
167778
 
167778
     for (count = 0; mod_pa[count] != NULL; count++);
167778
     ret = copy_cookie(context, err_padata, &mod_pa, &count);