Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222070; b=O5NuE92FLdalNrc9dTr/NSKBP2OPhcVNS9I3NwY52hk7idT9vRTEfXybFkasw9hRzyoX lNkQ1qc80pxYi2O3EFcS4d+5t9UaAkbYK9DCVVh5Xd7q7AvZzpDeSxbbT6QfSwqqUAQl+ Igl+j8Ju8tJq9vqpwL7gtx1DFRTJLSks2v6HxiINxFTrAXiUOCvK8pOl3NueEG12cYNIL NC7+T3iAe+GD4XKi5Jl3tqnAUcK0huc3CiYV9rPrtFdR17zLKCWUVYadaiVM97irRJrsj NA6u8pSG4TvjQ/bgVsGR41ekT8FpxksBJrQe54JSOt2nz6JQkWKnT7ZNwOSeWH2G+yw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222070; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To:X-Scanned-By; bh=xL0cYdxEcmUW0ZnDyV2FiQ5Hhga1g2WcvXq3b2JEYlo=; b=qBFEw0juwLM+4Hg+j8eo/YUWlvyvQ+zw6VMPOHd920xVg581m0qFk+BKFqtXrEesywf5 fjmVxhUnlhEcNhTqu0A6hl779YaBrqhy2Gb5Kvio8SH3Lh1PWtqwKWcZRubgJkZo8BsHh sNLk8OTN1bFQOK2Tk6tVD1FONAOhtAjN4E3sD7jp189HoRypZW+5n6HpvQZPuaa9uCdQY g8ePCJnm4FL8jsayK0GEXk8mWdLPS0NQlwQdO7DUk+bGAxYkbEjSNFPTz0XdSBEvDAe6E 1uPm+cB4LKElQRWH3Lh9DAUZszoJNPLgLMS3U1WmyMc/2dfSE4FqeqsUz048/c7x6vw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NAawChxr; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1ED7A606A338 for ; Wed, 2 Dec 2026 14:34:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1ED7A606A338 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NAawChxr DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796222069; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xL0cYdxEcmUW0ZnDyV2FiQ5Hhga1g2WcvXq3b2JEYlo=; b=NAawChxrIKMWtjl3qMUdUr/w7UUL7v+scrbML06+82vXV3DwA40NT4dK0z9opWRFWBxnOg WMhKdvVAcBSlKvUQQ2kKdoKZ5g9GSqF4idD5pG2WRC0IQZqPpK5mJKNm0tpifAsPgBVqVR 3EjheJMKSjViSJ/H/HjTUOSegCd4k90= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-546-3i6Z6UkRPP2pPYXH-PR3Rg-1; Wed, 2 Dec 2026 14:34:27 +0000 X-MC-Unique: 3i6Z6UkRPP2pPYXH-PR3Rg-1 X-Mimecast-MFC-AGG-ID: 3i6Z6UkRPP2pPYXH-PR3Rg_1796222066 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 652C4189AFE6; Wed, 2 Dec 2026 14:34:21 +0000 (UTC) Received: from smayhew-thinkpadp1gen4i.remote.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 38E33300039F; Wed, 2 Dec 2026 14:34:21 +0000 (UTC) Received: by smayhew-thinkpadp1gen4i.remote.csb (Postfix, from userid 13752) id 8C7905D3C567; Wed, 02 Dec 2026 09:34:20 -0500 (EST) Date: Wed, 2 Dec 2026 09:34:20 -0500 From: Scott Mayhew To: Dan Aloni Cc: steved@redhat.com, linux-nfs@vger.kernel.org, Hubert Dziedziczak Subject: Re: [PATCH v3] gssd: check the machine ccache's TGT instead of a cached endtime Message-ID: References: <20261201152511.381146-1-dan.aloni@vastdata.com> X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261201152511.381146-1-dan.aloni@vastdata.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 On Tue, 01 Dec 2026, Dan Aloni wrote: > gssd_get_single_krb5_cred() skips the keytab kinit while ple->endtime, > the endtime of gssd's own last kinit, is more than 5 minutes away and the > machine ccache file exists. Nothing reads the file again. If another > writer replaces it with a ticket that then expires, gssd keeps logging > "Credentials in CC ... are good until " and returning success, > while every machine upcall fails and the kernel gets -EACCES. File > existence and mtime say nothing about the TGT: libkrb5 appends each new > service ticket to the selected ccache, and only the TGT can get a ticket > for a server that is not already in the file. > > We hit this where a separate service keeps the machine ccache fresh by > atomically replacing the file. When that service's ticket expired, > machine upcalls failed for hours, and gssd logged "Credentials in CC > 'FILE:...' are good until" a time 10 hours ahead immediately before > "Failed to create machine krb5 context" and an err -13 downcall. > > For the file ccache, read it on every call instead. Its default > principal must be ple->princ, and it must hold a krbtgt/REALM@REALM > ticket issued to ple->princ that has already started, is not marked > invalid (postdated) and is valid for another 5 minutes, the existing > margin. If so, use it. Otherwise kinit from the keytab exactly as > before. > > The TGT's client is checked in the same scan that finds the TGT. Each > krb5_cc_* call reopens a FILE ccache, so a rename between > krb5_cc_get_principal() and the scan could otherwise pair the old file's > principal with the new file's TGT. Where an external refresher keeps the > file valid, gssd now never kinits, so it no longer fights that refresher > over the file. The read takes no lock: replacing the file with rename(2) > is atomic, and a read that races gssd's own kinit fails the check and > takes the existing locked kinit. > > Also pass the retry flag in krb5_use_machine_creds() as force_renew. It > passed a literal 0, so the "Machine cache prematurely expired or corrupted > trying to recreate cache" retry repeated the first attempt with > identical arguments. It now skips the ccache check and kinits from the > keytab. > > The memcache path is unchanged, and so is the existing "good until" log > text, which now reports the endtime of the TGT in the file. > > Co-authored-by: Hubert Dziedziczak > Signed-off-by: Dan Aloni > --- > utils/gssd/gssd_proc.c | 2 +- > utils/gssd/krb5_util.c | 98 +++++++++++++++++++++++++++++++++--------- > 2 files changed, 79 insertions(+), 21 deletions(-) > > diff --git a/utils/gssd/gssd_proc.c b/utils/gssd/gssd_proc.c > index 6bf5f2b5..3b7f09be 100644 > --- a/utils/gssd/gssd_proc.c > +++ b/utils/gssd/gssd_proc.c > @@ -636,7 +636,7 @@ krb5_use_machine_creds(struct clnt_info *clp, uid_t uid, > > do { > gssd_refresh_krb5_machine_credential(clp->servername, > - service, srchost, 0); > + service, srchost, nocache); > /* > * Get a list of credential cache names and try each > * of them until one works or we've tried them all > diff --git a/utils/gssd/krb5_util.c b/utils/gssd/krb5_util.c > index 0e8f889b..125bf567 100644 > --- a/utils/gssd/krb5_util.c > +++ b/utils/gssd/krb5_util.c > @@ -186,6 +186,9 @@ static int gssd_get_single_krb5_cred(krb5_context context, > krb5_ccache ccache); > static int query_krb5_ccache(const char* cred_cache, char **ret_princname, > char **ret_realm); > +static int check_for_tgt(krb5_context context, krb5_ccache ccache, > + krb5_principal principal, int match_client, time_t until, > + krb5_timestamp *endtime); > > static void release_ple_locked(krb5_context context, > struct gssd_k5_kt_princ *ple) > @@ -377,23 +380,35 @@ gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, > return err; > } > > -/* check if the ticket cache exists, if not set nocache=1 so that new > - * tgt is gotten > +/* > + * Return 1 if the machine ccache belongs to ple->princ and holds a TGT > + * issued to ple->princ for its realm that is still valid at 'until', > + * setting *endtime to the TGT's endtime. Any error returns 0, so the > + * caller gets new credentials. > + * > + * Each krb5_cc_* call on a FILE ccache opens the file again, so a rename > + * between two calls can give the default principal of one file and the > + * TGT of another. The TGT's client is therefore checked in the same scan > + * that finds it, which reads one open file. The default principal is > + * checked as well because GSS-API initiates as that name. > + * ple->princ is set only in new_ple() and the caller holds a reference, > + * so no lock is needed. > */ > static int > -gssd_check_if_cc_exists(struct gssd_k5_kt_princ *ple) > +gssd_machine_cc_is_valid(krb5_context context, krb5_ccache ccache, > + struct gssd_k5_kt_princ *ple, time_t until, > + krb5_timestamp *endtime) > { > - int fd; > - char cc_name[BUFSIZ]; > + krb5_principal cc_princ = NULL; > + int valid = 0; > > - snprintf(cc_name, sizeof(cc_name), "%s/%s%s_%s", > - ccachesearch[0], GSSD_DEFAULT_CRED_PREFIX, > - GSSD_DEFAULT_MACHINE_CRED_SUFFIX, ple->realm); > - fd = open(cc_name, O_RDONLY); > - if (fd < 0) > - return 1; > - close(fd); > - return 0; > + if (krb5_cc_get_principal(context, ccache, &cc_princ)) > + return 0; > + if (krb5_principal_compare(context, cc_princ, ple->princ)) > + valid = check_for_tgt(context, ccache, ple->princ, 1, until, > + endtime); > + krb5_free_principal(context, cc_princ); > + return valid; > } > > /* > @@ -420,20 +435,46 @@ gssd_get_single_krb5_cred(krb5_context context, > time_t now = time(0); > char *pname = NULL; > char *k5err = NULL; > - int nocache = 0; > pthread_t tid = pthread_self(); > + krb5_timestamp endtime = 0; > + time_t endtime_t; > + char timebuf[32]; > > memset(&my_creds, 0, sizeof(my_creds)); > > - if (!use_memcache) > - nocache = gssd_check_if_cc_exists(ple); > /* > * Workaround for clock skew among NFS server, NFS client and KDC > * 300 because clock skew must be within 300sec for kerberos > */ > now += 300; > + /* > + * The file ccache may be kept valid by an external refresher, and any > + * writer can replace it, so judge it by the TGT in it now and not by > + * the endtime of our last kinit. No lock: replacing the file with > + * rename(2) is atomic, and a read that races our own kinit below just > + * fails the check and kinits. > + */ > + if (!use_memcache && !force_renew) { > + if (gssd_machine_cc_is_valid(context, ccache, ple, now, > + &endtime)) { > + endtime_t = endtime; > + printerr(3, "%s(0x%lx): Credentials in CC '%s:%s' are " > + "good until %s", __func__, (unsigned long)tid, > + krb5_cc_get_type(context, ccache), > + krb5_cc_get_name(context, ccache), > + ctime_r(&endtime_t, timebuf)); > + code = 0; > + goto out; > + } > + printerr(3, "%s(0x%lx): No valid TGT for this principal in " > + "CC '%s:%s', getting new credentials\n", > + __func__, (unsigned long)tid, > + krb5_cc_get_type(context, ccache), > + krb5_cc_get_name(context, ccache)); > + } > pthread_mutex_lock(&ple_lock); > - if (ple->ccname && ple->endtime > now && !nocache && !force_renew) { > + if (use_memcache && ple->ccname && ple->endtime > now && > + !force_renew) { > printerr(3, "%s(0x%lx): Credentials in CC '%s' are good until %s", > __func__, (unsigned long)tid, ple->ccname, ctime((time_t *)&ple->endtime)); > code = 0; > @@ -1034,9 +1075,17 @@ static inline int data_is_equal(krb5_data d1, krb5_data d2) > && memcmp(d1.data, d2.data, d1.length) == 0); > } > > +/* > + * Return 1 if ccache holds a TGT for principal's realm that is still valid > + * at 'until', stopping at the first one. If match_client, the TGT must > + * also have been issued to principal, have started and not be marked > + * invalid (postdated). If endtime is not NULL, set it to that TGT's > + * endtime. > + */ > static int > check_for_tgt(krb5_context context, krb5_ccache ccache, > - krb5_principal principal) > + krb5_principal principal, int match_client, time_t until, > + krb5_timestamp *endtime) > { > krb5_error_code ret; > krb5_creds creds; > @@ -1057,8 +1106,17 @@ check_for_tgt(krb5_context context, krb5_ccache ccache, > "krbtgt", 6) == 0 && > data_is_equal(creds.server->data[1], > principal->realm) && > - creds.times.endtime > time(NULL)) > + creds.times.endtime > until && > + (!match_client || > + (krb5_principal_compare(context, creds.client, > + principal) && > + !(creds.ticket_flags & TKT_FLG_INVALID) && > + (creds.times.starttime ? creds.times.starttime : > + creds.times.authtime) <= time(NULL)))) { Your changes look okay to me, but this conditional was already pretty unwieldy/awkward to begin with, and this patch makes it more so. It might be better to have a preparatory patch that refactors this into a couple of helpers (no behavior change), and then layer your changes on top in another patch. -Scott > found = 1; > + if (endtime) > + *endtime = creds.times.endtime; > + } > krb5_free_cred_contents(context, &creds); > } > krb5_cc_end_seq_get(context, ccache, &cur); > @@ -1094,7 +1152,7 @@ query_krb5_ccache(const char* cred_cache, char **ret_princname, > if (ret) > goto err_princ; > > - found = check_for_tgt(context, ccache, principal); > + found = check_for_tgt(context, ccache, principal, 0, time(NULL), NULL); > if (found) { > ret = krb5_unparse_name(context, principal, &princstring); > if (ret == 0) { > -- > 2.52.0 > >