Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796137125; b=piwAA3lz2q+KcgUlgDlXmingRGAnq4s+pIWNUfsH0Oh5l0a3YW5P3p0P2R2SNZOWDq9m d0YQZtre1KFgfYsPxSYZ48PY/3QBBAlC4I2j3V8FrXsqDB7SlaBD74dSeg8r/bqXuIVk7 2OTjepW+ckJlBBAxWrRPiYRr3fL9hE08PxAumUyJvU888sh78Hdwx8S703PQEi6jLx2lp vK5KrcHuabQrNiHKx0rL7mFtGUNb6zL6+Cmq4GU0EG4KrnlrZcbP44+ShATh1L67GRaNj bVNrv4Re2hn52TKof6WfSxPIltFoUmb6E57Or9C3RlOxze0NuNZjRG41QNmgztn9EAA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137125; 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=kHNfX7QdjghACOlVskc/m7SHJsV1Er/HXqQat6a5jA0=; b=LvCBYMzbYW54FCcrsa8/RnUiRGghmj9QByOHhRdh7/RczgorW5kJKxd+MY1pLMtR9UVV hHCcFtZ6ZsqR5BBvRDdlhHNA56qW909ubWDjYRgD84iMhz2PGsO83zjVkbvNh6QFQsxgP xc7QN+6qj6sUjf2UxM4vMy8mZe9RnD6Z3F2x/ExxLcrulN7DYsUG34vDAxnDGnLyZ6rKD HsC83/iWSndPB9nQqer7mLAxZYvnYaGTBHFe1j9Vhg5ZGZ+FVGXJipHXa10te8hbKZEc9 37oSW22KecI1Dgyghthxja9dbOEsSxX4APLgkcfPMBU/zdVosJJgkudE5uhStXTVkqQ== 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=E2776Lme; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 B0C77606A325 for ; Tue, 1 Dec 2026 14:58:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B0C77606A325 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=E2776Lme DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796137123; 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=kHNfX7QdjghACOlVskc/m7SHJsV1Er/HXqQat6a5jA0=; b=E2776Lme5mxvdJjo/wxTFvHbLyPbnPewiASmKIq4JsupwwcgRfMbmWNDkwBTxkP6m8j2+4 Xt+KIOd8b6uXAlmWgnrtaSU6vU+ByJsJwgmIV4EHlzx8fa1N+trvoztgbsPVkF3wnf/l0s rjlG7AMgLbp7TjCAgszKwV4NLGywbW0= 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-445-OxFSJKDUOymbGCJ9gYwuXA-1; Tue, 1 Dec 2026 14:58:38 +0000 X-MC-Unique: OxFSJKDUOymbGCJ9gYwuXA-1 X-Mimecast-MFC-AGG-ID: OxFSJKDUOymbGCJ9gYwuXA_1796137117 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 08F9D197759B; Tue, 1 Dec 2026 14:58:37 +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 D055830002F6; Tue, 1 Dec 2026 14:58:36 +0000 (UTC) Received: by smayhew-thinkpadp1gen4i.remote.csb (Postfix, from userid 13752) id 219635D1DDC5; Tue, 01 Dec 2026 09:58:36 -0500 (EST) Date: Tue, 1 Dec 2026 09:58:36 -0500 From: Scott Mayhew To: Dan Aloni Cc: linux-nfs@vger.kernel.org, Hubert Dziedziczak , steved@redhat.com Subject: Re: [PATCH v2] gssd: check the machine ccache's TGT instead of a cached endtime Message-ID: References: <20261129090414.2057668-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: <20261129090414.2057668-1-dan.aloni@vastdata.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Adding SteveD, the nfs-utils maintainer. On Sun, 29 Nov 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 | 97 +++++++++++++++++++++++++++++++++--------- > 2 files changed, 78 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..4f6e9f3c 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,45 @@ 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__, tid, This needs to cast tid to unsigned long, otherwise it reintroduces the bug that was fixed by commit c1f521c4 ("gssd: fix build failure with musl libc"). > + 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__, tid, Same thing here. > + 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 +1074,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 +1105,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)))) { > found = 1; > + if (endtime) > + *endtime = creds.times.endtime; > + } > krb5_free_cred_contents(context, &creds); > } > krb5_cc_end_seq_get(context, ccache, &cur); > @@ -1094,7 +1151,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 > >