]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Using locking in MEMORY krb5_cc_get_principal()
authorGreg Hudson <ghudson@mit.edu>
Sun, 20 Jun 2021 23:24:07 +0000 (19:24 -0400)
committerGreg Hudson <ghudson@mit.edu>
Mon, 12 Jul 2021 15:59:57 +0000 (11:59 -0400)
Without locking, the principal pointer could be freed out from under
krb5_copy_principal() by another thread calling krb5_cc_initialize()
or krb5_cc_destroy().

(cherry picked from commit 1848447291c68e21311f441b0458ae53471d00d3)

ticket: 9014
version_fixed: 1.19.2

src/lib/krb5/ccache/cc_memory.c

index 610091a2573a9fe53b9c7b52c6cb348338e92849..e4c795d259f3890d96fa115af3523da7e8ce9524 100644 (file)
@@ -575,12 +575,17 @@ krb5_mcc_get_name (krb5_context context, krb5_ccache id)
 krb5_error_code KRB5_CALLCONV
 krb5_mcc_get_principal(krb5_context context, krb5_ccache id, krb5_principal *princ)
 {
-    krb5_mcc_data *ptr = (krb5_mcc_data *)id->data;
-    if (!ptr->prin) {
-        *princ = 0L;
-        return KRB5_FCC_NOFILE;
-    }
-    return krb5_copy_principal(context, ptr->prin, princ);
+    krb5_error_code ret;
+    krb5_mcc_data *d = id->data;
+
+    *princ = NULL;
+    k5_cc_mutex_lock(context, &d->lock);
+    if (d->prin == NULL)
+        ret = KRB5_FCC_NOFILE;
+    else
+        ret = krb5_copy_principal(context, d->prin, princ);
+    k5_cc_mutex_unlock(context, &d->lock);
+    return ret;
 }
 
 krb5_error_code KRB5_CALLCONV