Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795943059; b=VErMDiYT1hTOU9efr3s8Rh2tJiSxMZ6fdWR/VBjklEd7f8nftgN2tLwZsiidEp4j8Cot ZKk+ub4+8Brl19uFaV1VU2jK4GvRrvP79gp2l2xvXeA4U00u2TbQmQU68i6tvUNSxPiIy KcUeKiOVo8IRfX0EBHVpmQZ7iYvglGsbEiqZyb2Hy5SdR8H8E9myX3yVCQY8ZD9Mb9YgZ ePv3zEW4fV+HsZToFN5eYooWRqWHH33AuV9JqtYtBx5U61FG5mg0a9BC19DJYxrUayYKW O9svfSEGzRM6cpzthcRZIZGi9LiFqgZQvrBHN26XHAGZvWahVsJW4zoO/SanJgRyYWQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795943059; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=HENt5G1O5bJHEqflpeG//I4Z998Gt46sdiLoEcLa8DM=; b=tCrQDD6xTxBcaUuRrsvQ0zlEoSa0Xt8wFVhPJcyo6ESnuQRkDgvX4jGFR73fZ01o0mdn og6ifdzXifsHla/PqKZJJgFDmsWHka8eNZ6G8pMh3mWB5DYWN3r55qIFs8o/JVLVzHGOV tLzsEaMymtKIiYqnTiq5Kz3Gl726WlTlwf989B2fKSx8egV8DQVfPANWOTHjMeGJkCSBu 6Ce7l49+fQJKRUlIM7CQeoVV3yn1xTt6IRezOmQHqtcKiMzoG1f8+v1oZkcD4yYtny1Xe ECCqQ8IWVQMnykqGZhvI/PxWboQjm9+sGUGnQXHYoyZ6hhA2SxuZqRbHYi+U/Zmj7mQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=vastdata.com; spf=pass smtp.mailfrom=vastdata.com; dkim=pass header.d=vastdata.com header.i=@vastdata.com header.a=rsa-sha256 header.s=google header.b=dg23juzX; arc=none smtp.remote-ip=209.85.128.49 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05611606A31C for ; Sun, 29 Nov 2026 09:04:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 05611606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vastdata.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vastdata.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=vastdata.com header.i=@vastdata.com header.a=rsa-sha256 header.s=google header.b=dg23juzX Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4bb5b8ef642so79174545e9.1 for ; Sun, 29 Nov 2026 01:04:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vastdata.com; s=google; t=1795943057; x=1796547857; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=HENt5G1O5bJHEqflpeG//I4Z998Gt46sdiLoEcLa8DM=; b=dg23juzXOLnwq9tov5RUMWaWS8/1UL0JF6ES/GUEX1mDMNEKBnSAko4wjmm6NWCMma Qq2v4WeMAJBwaOfE6Iap7JpgXZyfapuevyITAcnY8IwLS0k0hf5BH8mvXHz/0MrLNhT/ puU8OB9ZeOomspx+OXd488wsIL5UFuwvps7CZSG6ljtYsSmczzzToIFJ6IgTCUS+3Dcc dMV0vpnZy2nWKEKCvboqW4aCCgcr7VArtT1sqrYSCQR7LNjhNa4WHn7RrsjZW5hWc1lD mm0w7poFbzKoGY7DYUbAyeo0ReJPF3iyla/rcmZrc85Jk6KFLIbAASxJt90x13Pe/JaQ APbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795943057; x=1796547857; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=HENt5G1O5bJHEqflpeG//I4Z998Gt46sdiLoEcLa8DM=; b=0op7AhtxiFES0oq/bUTANg/y2o9fFidXUhae3nDX1ue7LW9+ueELPrvOkB9oUZpQIw vUMthM6zu+6sm/MWN/2GpEUBx6jdhiA1djco+DpJ+awd/knGnQb8H+W1AmK+6YRmUvj/ zbV/UdVxBRePdXrICmvMRs3i9Yzl5C3ltYwUxHizEq7r5GvxblW/n5DXd3Of9j6b8NF4 cazyGFxl0/nKisVGlGj2B5zZrTWeZPdktq1n0xGyzd9GfOd2wmJs2SqHlyxAWz9eDOBN k60D2lq816rPwwBYEoy78g2VyA3T5wS+9EmHGx1/0Zq2ibTcFobq1U/01L03geVCOxTe Z2rQ== X-Gm-Message-State: APuU4Z8eI9AV3hTOau5mL1OfaY3Oh1qFrp9AHgIwOTywHUGGZuGeXbWM F1YMtivmeYW9bM0vJqIsfFt38rhEdTRyyGgz0ppIjKsMGXwV2kjZBWVU/cOfpgrC2zqe1l8lQ38 xtLyPEjk= X-Gm-Gg: AZFdTZciliyDslwM9dP06iVjucBykzONsamYiF7ZhhOrdIvGxpm6FEh1a87r4wlcO1u SxdzKn7BO5AEdMFrT1NCXDguzO9Yzs8SGSQtjvdydYqzu5Pd3Am1n9tWvivj/BdCB+7v0ptex+5 UFZih5lrq7ZPgy+UrB/4PAFbg4hqlMR/IvMFTFND0+TdlwcrC3fUS60++7/Dj27XMi/Fx1VNfzs AwkMWrqkZlDxwyD4ttzJTC9s1a0O8bRPUJK+6ZLI8stzl3DJLO+H8RlBUFk+dPwpnvDkrAhB8AZ v5p7pRBKk4FZ3mWtxej0/xAWN/8D3HZx5fBHgBR2xY3a9Mu8GAE50560LyrxeCSWmK49I70v8uM vC+B8aPYgYjf2WtrPheN+iM219ES5phED3e/+NltL1clu0vz9dA+sNiPW5FO9w6pBgKFGs4mWzb m13NxX3JQqt/Noydec++X7MlprjrK9jyiI/TtUpOji5nF0QFJEUD9A4KWkQdHU8jL83zpY9CctJ Wj3+6TrnW72eAP2uA55LAs13b6slx3OzIBEl3ZPUnZ+rZa90SiCHgVSt4zI1in7gH3He4w0WX7E kwtzgOojEkR1uB/Pmboklj4z0Sa1/C3Eri6Hjg77Qx++TZ1KBajysN+C3RxR/JBr7V3svYD9oaz GI5chvbYm+PGXmqs= X-Received: by 2002:a05:600c:a05:b0:4bb:5e6b:49e with SMTP id 5b1f17b1804b1-4bb88807c0fmr601659505e9.6.1795943057350; Sun, 29 Nov 2026 01:04:17 -0800 (PST) Received: from jupiter.vstd.int ([192.116.95.190]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb906635c7sm551853905e9.2.2026.11.29.01.04.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 01:04:16 -0800 (PST) From: Dan Aloni To: smayhew@redhat.com Cc: linux-nfs@vger.kernel.org, Hubert Dziedziczak Subject: [PATCH v2] gssd: check the machine ccache's TGT instead of a cached endtime Date: Sun, 29 Nov 2026 11:04:14 +0200 Message-ID: <20261129090414.2057668-1-dan.aloni@vastdata.com> X-Mailer: git-send-email 2.52.0 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-Transfer-Encoding: 8bit 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, + 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, + 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