Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796138716; b=m9tErklWVWvNshUwEBPw8XJsmaE9P0szcyuzXN2lkrrPxeyLHF2+lI8rDZK9OTAP+NgP A4wQqlXGMSWUVqBLAwmZa5OD5ap7f6M3IAeJ3ReRwy6FpofpWcQAJvZmDs/vRXj9KK+uj d4v+obdUy5/olD9vYvZjPK1PS864qkzTTOYrZv4z7J7H+DHdpmEuxSojwq07NH0TVrbZk 0sNZgcwlYpF04J31uLQCOnbTuRuLPceICWRs/MPy3q+5sL2yvOs+w57iCvgqEXjB75E68 1z6qtvovzrhf3JuxO74Nkbuh8Gdg6JdCBH3bJNi0WGCX7gGMbP2MTUU72/P3lx0kapg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796138716; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=hoFYc47wGKs4pHLIcYkl1VySCCbjinl6GHUPmnQFRCs=; b=tEjAbQZKYSL01GFjdsZ05bgPevCpt8tOmGNT3Eg1NHNmK0RNoZzWOT29qRteAnGbYMpt Zp7POYsIGM0ei0w65LiOMplOyWKm73bkhev7ZPisAZ+ICT1CLL4+XiBtORydDSpDBgkAm MifBkz7urewhpLDnzdUr/88isnogVVz9D7TFjW4nOxJEeQgV3Rv2kgvsxOedL8tSR2Bb5 EJM75AbtX5fq051J68UGgx2Td/GNeKNFuJjqUOPWlOOxyqyCZ2g/z9i5OiR9rOR/5nQIL ey1l5McfVS0fmQikwLv5tJJI1/LYLePdFBqFxGcnpWy8V9gl8WCa65Onva2jP/CBdsg== 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=IkrJZhE4; arc=none smtp.remote-ip=209.85.221.52 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 46B1B606A31A for ; Tue, 1 Dec 2026 15:25:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 46B1B606A31A 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=IkrJZhE4 Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-4ad6219f7d8so10880921f8f.0 for ; Tue, 01 Dec 2026 07:25:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vastdata.com; s=google; t=1796138714; x=1796743514; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=hoFYc47wGKs4pHLIcYkl1VySCCbjinl6GHUPmnQFRCs=; b=IkrJZhE4zfE70Q9yjG2ZqFVbomQOI1FVHx7zU37Ft9xmY0k+M+WTLeVMioO5VMynNU O7TRaO9nAeWIPQLxS0eKIjixFv5u3ZiKuXI+LhoYHRT3P8/3n5NPykNF+9sJ/igI8bkr SELdorcg5Q73c57GNFu6tuj3ORWT1lXQ9lzzpzIhHNdeMn88zFbMbhkhFsHVyl3U9Uqr hxTWuug1EuVWh4VmMVnE6yaGfSo9Dcu1/J4UeAzRyDCgZrZtFJ4pHzpjHnRshpq/fKdi oXTMblbz6GfS4LAZ6U9CiW8xXxk1OMI9qEAa4kX37PzvSkQfmj+8EgaZmvvA+MyUGSG5 0bYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796138714; x=1796743514; h=content-transfer-encoding:mime-version:references:in-reply-to :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=hoFYc47wGKs4pHLIcYkl1VySCCbjinl6GHUPmnQFRCs=; b=EEmwk0AFP5fMPJN9mqjFNZ6F78V10H2xj4ZB0p/z+JvLf1WXWbDk8KzPlaw43rSUNa VXE/M/WH9zTSyuyUCuVflHjVMy/M5kEpPrJqLxrIbLKm4/SB7JyUXwDfx1LogFPNue6l Wn96ZQNg+OqZ2qxFzgXv0TlKjZEioPRqyIAPA5l6QggSBltXthY+bpMTyWaxWsmtqWaV cRJwFNCt4ZrqfZx5aEa5R+UYpCGyZxeCisUsoc9fDixAEe+WmSnpmAIFvbHY69exGy0z wUl5r2VKlZynLiV4FqFEOYvh4dXKuHkwUFOnvHOhEoBIM83+NUAy95QjYVjHpTT83uZ9 icxA== X-Forwarded-Encrypted: i=1; AL+hErFURUPBiplX+WzXt0W2H2DzAwMNoC8iM24syb0zC1klvVrTEagMvyVYk46G9Lhng8dLnTAPqCYBbyE=@vger.kernel.org X-Gm-Message-State: APuU4Z9GTJMbkB0vWvTisXoSb7zGx3hoY0OkC4WYJfS2yzD5lvSMEuSF IVNenlutnugzvJIcP0qzoRsX/kqvX2yNOGk7Jw8o3cC1m9qYLKOetXZOsSiTNwx8SjKzbd1dM+q B0RCG1AQ= X-Gm-Gg: AZFdTZewDRsP+drlJarq9RRsDMbHTjXfPZNSCAgvasPTEVp3iO4+3l+kuhrmdsKlwKv 3WTRVZv1Hkve1WnS0pOTg6oYx+UKVBCcSoc5gYlZFNXQIGyfexHLCk4jfkyTUm6SI1fs+h1jiB1 s8zDUJqCSSSw/dyI6J/c2AGMKqZCgNHjXXS+W/k5hTCFiTpIo92UEI9lxh6d/YeCrepGyjvesF3 gWaiIQjAuTR5GcYTh7rI7RpWrFJu3dhqE6kzA75HeRJDrTo9cKdpRofEk4ONvx8xgswaBwH4KY4 V2VusEyhExhrRMjg8pb1L0jTCP6LsN93cA+nWN0Q0MC4HFmodAfmFyA/+Jl0iqtjhrfZwfnOJmE Azt16dXAoXGWUvzVcN+XpIik+HT68S+5u8C9TZWqeXzOi039c3D9AtSP58QLMkOby3H6+EjrWw5 HwaiQbpXFyZayMQc6fZR5Z6vDMm1hArqljcINp8Hoc3sqFEA0h/00uwmui/wPRhVneaEUKPuKVB UHsGQ9l79hELWuI3z5BnYVTEx+UeqoKa6mdURqnB/RcjuaXOOOxV8W4hAXpz68ESBsFZc3Gq3Bk 2aLtGInJiUcYfGTNzusTh+t90Afi+aZGfHRjOfuCZna9N47t29E+UI2JKICcPBz4pxpsuw== X-Received: by 2002:a05:600c:c092:b0:4bb:a5e1:720a with SMTP id 5b1f17b1804b1-4bba5e187bamr364190815e9.15.1796138714442; Tue, 01 Dec 2026 07:25:14 -0800 (PST) Received: from jupiter.vstd.int ([192.116.95.190]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb906d6a13sm621563115e9.3.2026.12.01.07.25.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 07:25:13 -0800 (PST) From: Dan Aloni To: steved@redhat.com Cc: smayhew@redhat.com, linux-nfs@vger.kernel.org, Hubert Dziedziczak Subject: [PATCH v3] gssd: check the machine ccache's TGT instead of a cached endtime Date: Tue, 1 Dec 2026 17:25:11 +0200 Message-ID: <20261201152511.381146-1-dan.aloni@vastdata.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: 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 | 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)))) { 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