Skip to contentSkip to conversation

gssd: check the machine ccache's TGT instead of a cached endtime

PATCH Superseded Dan Aloni 2 participants 5 messages Nov 29 – Dec 1

Subsystems (2)
lore
Share & tools
lore

Dan Aloni

author
#0 lore raw

PATCH v2 1/1 gssd: check the machine ccache's TGT instead of a cached endtime

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 <date>" 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.

---
 utils/gssd/gssd_proc.c |  2 +-
 utils/gssd/krb5_util.c | 97 +++++++++++++++++++++++++++++++++---------
 2 files changed, 78 insertions(+), 21 deletions(-)
utils/gssd/gssd_proc.c +1 -1
index 6bf5f2b5..3b7f09be 100644
@@ -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
utils/gssd/krb5_util.c +77 -20

150 lines not on this page · raw message

Signature · 1 line
2.52.0
author
#1 lore raw

First send has included tools/nfs-fh-verify/nfs-fh-verify which is a built binary that was added in commit c1f521c4f7e198c. Better remove that in a separate commit and add to .gitignore.

Quoted 1 line · Dan Aloni

gssd_get_single_krb5_cred() skips the keytab kinit while ple->endtime,

[..]

Signature · 1 line
Dan Aloni
#2 lore raw
Quoted 3 lines · Dan Aloni

First send has included tools/nfs-fh-verify/nfs-fh-verify which is a built binary that was added in commit c1f521c4f7e198c. Better remove that in a separate commit and add to .gitignore.

Sent SteveD a patch do do that here:
https://lore.kernel.org/linux-nfs/[email protected]/T/#u

-Scott

Quoted 9 lines · Dan Aloni
Quoted 1 line

gssd_get_single_krb5_cred() skips the keytab kinit while ple->endtime,

[..]

Signature · 1 line
Dan Aloni
#3 lore raw
In reply to Dan Aloni

Adding SteveD, the nfs-utils maintainer.

Quoted 146 lines · Dan Aloni

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 <date>" 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.

---
 utils/gssd/gssd_proc.c |  2 +-
 utils/gssd/krb5_util.c | 97 +++++++++++++++++++++++++++++++++---------
 2 files changed, 78 insertions(+), 21 deletions(-)
utils/gssd/gssd_proc.c +1 -1
index 6bf5f2b5..3b7f09be 100644
@@ -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
utils/gssd/krb5_util.c +34 -16
index 0e8f889b..4f6e9f3c 100644
@@ -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

utils/gssd/krb5_util.c gssd_get_single_krb5_cred(krb5_context context, · line 462

Quoted 10 lines
utils/gssd/krb5_util.c +10 -0
+	 * 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").

utils/gssd/krb5_util.c gssd_get_single_krb5_cred(krb5_context context, · line 470

Quoted 8 lines
utils/gssd/krb5_util.c +8 -0
+				 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.

Quoted 61 lines
utils/gssd/krb5_util.c +25 -4
+			 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) {
Signature · 1 line
2.52.0
author
#4 lore raw

PATCH v3 1/1 gssd: check the machine ccache's TGT instead of a cached endtime

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 <date>" 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.

---
 utils/gssd/gssd_proc.c |  2 +-
 utils/gssd/krb5_util.c | 98 +++++++++++++++++++++++++++++++++---------
 2 files changed, 79 insertions(+), 21 deletions(-)
utils/gssd/gssd_proc.c +1 -1
index 6bf5f2b5..3b7f09be 100644
@@ -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
utils/gssd/krb5_util.c +78 -20

151 lines not on this page · raw message

Signature · 1 line
2.52.0