From: Greg Hudson Date: Sun, 20 Jun 2021 23:24:07 +0000 (-0400) Subject: Using locking in MEMORY krb5_cc_get_principal() X-Git-Tag: krb5-1.20-beta1~74 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1197%2Fhead;p=thirdparty%2Fkrb5.git Using locking in MEMORY krb5_cc_get_principal() 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(). ticket: 9014 (new) tags: pullup target_version: 1.19-next target_version: 1.18-next --- diff --git a/src/lib/krb5/ccache/cc_memory.c b/src/lib/krb5/ccache/cc_memory.c index 529ada5455..0897d6ba3e 100644 --- a/src/lib/krb5/ccache/cc_memory.c +++ b/src/lib/krb5/ccache/cc_memory.c @@ -578,12 +578,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