8f2508
diff -up nfs-utils-2.3.3/utils/gssd/gssd.c.orig nfs-utils-2.3.3/utils/gssd/gssd.c
8f2508
--- nfs-utils-2.3.3/utils/gssd/gssd.c.orig	2021-07-19 09:39:04.273895536 -0400
8f2508
+++ nfs-utils-2.3.3/utils/gssd/gssd.c	2021-07-19 09:40:13.942751214 -0400
8f2508
@@ -364,7 +364,7 @@ out:
8f2508
 /* Actually frees clp and fields that might be used from other
8f2508
  * threads if was last reference.
8f2508
  */
8f2508
-static void
8f2508
+void
8f2508
 gssd_free_client(struct clnt_info *clp)
8f2508
 {
8f2508
 	int refcnt;
8f2508
@@ -416,55 +416,6 @@ gssd_destroy_client(struct clnt_info *cl
8f2508
 
8f2508
 static void gssd_scan(void);
8f2508
 
8f2508
-static int
8f2508
-start_upcall_thread(void (*func)(struct clnt_upcall_info *), void *info)
8f2508
-{
8f2508
-	pthread_attr_t attr;
8f2508
-	pthread_t th;
8f2508
-	int ret;
8f2508
-
8f2508
-	ret = pthread_attr_init(&attr);
8f2508
-	if (ret != 0) {
8f2508
-		printerr(0, "ERROR: failed to init pthread attr: ret %d: %s\n",
8f2508
-			 ret, strerror(errno));
8f2508
-		return ret;
8f2508
-	}
8f2508
-	ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
8f2508
-	if (ret != 0) {
8f2508
-		printerr(0, "ERROR: failed to create pthread attr: ret %d: "
8f2508
-			 "%s\n", ret, strerror(errno));
8f2508
-		return ret;
8f2508
-	}
8f2508
-
8f2508
-	ret = pthread_create(&th, &attr, (void *)func, (void *)info);
8f2508
-	if (ret != 0)
8f2508
-		printerr(0, "ERROR: pthread_create failed: ret %d: %s\n",
8f2508
-			 ret, strerror(errno));
8f2508
-	return ret;
8f2508
-}
8f2508
-
8f2508
-static struct clnt_upcall_info *alloc_upcall_info(struct clnt_info *clp)
8f2508
-{
8f2508
-	struct clnt_upcall_info *info;
8f2508
-
8f2508
-	info = malloc(sizeof(struct clnt_upcall_info));
8f2508
-	if (info == NULL)
8f2508
-		return NULL;
8f2508
-
8f2508
-	pthread_mutex_lock(&clp_lock);
8f2508
-	clp->refcount++;
8f2508
-	pthread_mutex_unlock(&clp_lock);
8f2508
-	info->clp = clp;
8f2508
-
8f2508
-	return info;
8f2508
-}
8f2508
-
8f2508
-void free_upcall_info(struct clnt_upcall_info *info)
8f2508
-{
8f2508
-	gssd_free_client(info->clp);
8f2508
-	free(info);
8f2508
-}
8f2508
-
8f2508
 /* For each upcall read the upcall info into the buffer, then create a
8f2508
  * thread in a detached state so that resources are released back into
8f2508
  * the system without the need for a join.
8f2508
@@ -473,44 +424,16 @@ static void
8f2508
 gssd_clnt_gssd_cb(int UNUSED(fd), short UNUSED(which), void *data)
8f2508
 {
8f2508
 	struct clnt_info *clp = data;
8f2508
-	struct clnt_upcall_info *info;
8f2508
-
8f2508
-	info = alloc_upcall_info(clp);
8f2508
-	if (info == NULL)
8f2508
-		return;
8f2508
-
8f2508
-	info->lbuflen = read(clp->gssd_fd, info->lbuf, sizeof(info->lbuf));
8f2508
-	if (info->lbuflen <= 0 || info->lbuf[info->lbuflen-1] != '\n') {
8f2508
-		printerr(0, "WARNING: %s: failed reading request\n", __func__);
8f2508
-		free_upcall_info(info);
8f2508
-		return;
8f2508
-	}
8f2508
-	info->lbuf[info->lbuflen-1] = 0;
8f2508
 
8f2508
-	if (start_upcall_thread(handle_gssd_upcall, info))
8f2508
-		free_upcall_info(info);
8f2508
+	handle_gssd_upcall(clp);
8f2508
 }
8f2508
 
8f2508
 static void
8f2508
 gssd_clnt_krb5_cb(int UNUSED(fd), short UNUSED(which), void *data)
8f2508
 {
8f2508
 	struct clnt_info *clp = data;
8f2508
-	struct clnt_upcall_info *info;
8f2508
-
8f2508
-	info = alloc_upcall_info(clp);
8f2508
-	if (info == NULL)
8f2508
-		return;
8f2508
-
8f2508
-	if (read(clp->krb5_fd, &info->uid,
8f2508
-			sizeof(info->uid)) < (ssize_t)sizeof(info->uid)) {
8f2508
-		printerr(0, "WARNING: %s: failed reading uid from krb5 "
8f2508
-			 "upcall pipe: %s\n", __func__, strerror(errno));
8f2508
-		free_upcall_info(info);
8f2508
-		return;
8f2508
-	}
8f2508
 
8f2508
-	if (start_upcall_thread(handle_krb5_upcall, info))
8f2508
-		free_upcall_info(info);
8f2508
+	handle_krb5_upcall(clp);
8f2508
 }
8f2508
 
8f2508
 static struct clnt_info *
8f2508
diff -up nfs-utils-2.3.3/utils/gssd/gssd.h.orig nfs-utils-2.3.3/utils/gssd/gssd.h
8f2508
--- nfs-utils-2.3.3/utils/gssd/gssd.h.orig	2021-07-19 09:39:04.269895430 -0400
8f2508
+++ nfs-utils-2.3.3/utils/gssd/gssd.h	2021-07-19 09:40:13.943751240 -0400
8f2508
@@ -84,14 +84,17 @@ struct clnt_info {
8f2508
 
8f2508
 struct clnt_upcall_info {
8f2508
 	struct clnt_info 	*clp;
8f2508
-	char			lbuf[RPC_CHAN_BUF_SIZE];
8f2508
-	int			lbuflen;
8f2508
 	uid_t			uid;
8f2508
+	int			fd;
8f2508
+	char			*srchost;
8f2508
+	char			*target;
8f2508
+	char			*service;
8f2508
 };
8f2508
 
8f2508
-void handle_krb5_upcall(struct clnt_upcall_info *clp);
8f2508
-void handle_gssd_upcall(struct clnt_upcall_info *clp);
8f2508
+void handle_krb5_upcall(struct clnt_info *clp);
8f2508
+void handle_gssd_upcall(struct clnt_info *clp);
8f2508
 void free_upcall_info(struct clnt_upcall_info *info);
8f2508
+void gssd_free_client(struct clnt_info *clp);
8f2508
 
8f2508
 
8f2508
 #endif /* _RPC_GSSD_H_ */
8f2508
diff -up nfs-utils-2.3.3/utils/gssd/gssd_proc.c.orig nfs-utils-2.3.3/utils/gssd/gssd_proc.c
8f2508
--- nfs-utils-2.3.3/utils/gssd/gssd_proc.c.orig	2021-07-19 09:39:04.269895430 -0400
8f2508
+++ nfs-utils-2.3.3/utils/gssd/gssd_proc.c	2021-07-19 09:40:13.944751267 -0400
8f2508
@@ -80,6 +80,8 @@
8f2508
 #include "nfslib.h"
8f2508
 #include "gss_names.h"
8f2508
 
8f2508
+extern pthread_mutex_t clp_lock;
8f2508
+
8f2508
 /* Encryption types supported by the kernel rpcsec_gss code */
8f2508
 int num_krb5_enctypes = 0;
8f2508
 krb5_enctype *krb5_enctypes = NULL;
8f2508
@@ -719,22 +721,133 @@ out_return_error:
8f2508
 	goto out;
8f2508
 }
8f2508
 
8f2508
-void
8f2508
-handle_krb5_upcall(struct clnt_upcall_info *info)
8f2508
-{
8f2508
-	struct clnt_info *clp = info->clp;
8f2508
+static struct clnt_upcall_info *
8f2508
+alloc_upcall_info(struct clnt_info *clp, uid_t uid, int fd, char *srchost,
8f2508
+		  char *target, char *service)
8f2508
+{
8f2508
+	struct clnt_upcall_info *info;
8f2508
+
8f2508
+	info = malloc(sizeof(struct clnt_upcall_info));
8f2508
+	if (info == NULL)
8f2508
+		return NULL;
8f2508
+
8f2508
+	memset(info, 0, sizeof(*info));
8f2508
+	pthread_mutex_lock(&clp_lock);
8f2508
+	clp->refcount++;
8f2508
+	pthread_mutex_unlock(&clp_lock);
8f2508
+	info->clp = clp;
8f2508
+	info->uid = uid;
8f2508
+	info->fd = fd;
8f2508
+	if (srchost) {
8f2508
+		info->srchost = strdup(srchost);
8f2508
+		if (info->srchost == NULL)
8f2508
+			goto out_info;
8f2508
+	}
8f2508
+	if (target) {
8f2508
+		info->target = strdup(target);
8f2508
+		if (info->target == NULL)
8f2508
+			goto out_srchost;
8f2508
+	}
8f2508
+	if (service) {
8f2508
+		info->service = strdup(service);
8f2508
+		if (info->service == NULL)
8f2508
+			goto out_target;
8f2508
+	}
8f2508
+
8f2508
+out:
8f2508
+	return info;
8f2508
 
8f2508
-	printerr(2, "\n%s: uid %d (%s)\n", __func__, info->uid, clp->relpath);
8f2508
+out_target:
8f2508
+	if (info->target)
8f2508
+		free(info->target);
8f2508
+out_srchost:
8f2508
+	if (info->srchost)
8f2508
+		free(info->srchost);
8f2508
+out_info:
8f2508
+	free(info);
8f2508
+	info = NULL;
8f2508
+	goto out;
8f2508
+}
8f2508
+
8f2508
+void free_upcall_info(struct clnt_upcall_info *info)
8f2508
+{
8f2508
+	gssd_free_client(info->clp);
8f2508
+	if (info->service)
8f2508
+		free(info->service);
8f2508
+	if (info->target)
8f2508
+		free(info->target);
8f2508
+	if (info->srchost)
8f2508
+		free(info->srchost);
8f2508
+	free(info);
8f2508
+}
8f2508
 
8f2508
-	process_krb5_upcall(clp, info->uid, clp->krb5_fd, NULL, NULL, NULL);
8f2508
+static void
8f2508
+gssd_work_thread_fn(struct clnt_upcall_info *info)
8f2508
+{
8f2508
+	process_krb5_upcall(info->clp, info->uid, info->fd, info->srchost, info->target, info->service);
8f2508
 	free_upcall_info(info);
8f2508
 }
8f2508
 
8f2508
+static int
8f2508
+start_upcall_thread(void (*func)(struct clnt_upcall_info *), void *info)
8f2508
+{
8f2508
+	pthread_attr_t attr;
8f2508
+	pthread_t th;
8f2508
+	int ret;
8f2508
+
8f2508
+	ret = pthread_attr_init(&attr);
8f2508
+	if (ret != 0) {
8f2508
+		printerr(0, "ERROR: failed to init pthread attr: ret %d: %s\n",
8f2508
+			 ret, strerror(errno));
8f2508
+		return ret;
8f2508
+	}
8f2508
+	ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
8f2508
+	if (ret != 0) {
8f2508
+		printerr(0, "ERROR: failed to create pthread attr: ret %d: "
8f2508
+			 "%s\n", ret, strerror(errno));
8f2508
+		return ret;
8f2508
+	}
8f2508
+
8f2508
+	ret = pthread_create(&th, &attr, (void *)func, (void *)info);
8f2508
+	if (ret != 0)
8f2508
+		printerr(0, "ERROR: pthread_create failed: ret %d: %s\n",
8f2508
+			 ret, strerror(errno));
8f2508
+	return ret;
8f2508
+}
8f2508
+
8f2508
 void
8f2508
-handle_gssd_upcall(struct clnt_upcall_info *info)
8f2508
+handle_krb5_upcall(struct clnt_info *clp)
8f2508
 {
8f2508
-	struct clnt_info	*clp = info->clp;
8f2508
 	uid_t			uid;
8f2508
+	struct clnt_upcall_info	*info;
8f2508
+	int			err;
8f2508
+
8f2508
+	if (read(clp->krb5_fd, &uid, sizeof(uid)) < (ssize_t)sizeof(uid)) {
8f2508
+		printerr(0, "WARNING: failed reading uid from krb5 "
8f2508
+			    "upcall pipe: %s\n", strerror(errno));
8f2508
+		return;
8f2508
+	}
8f2508
+	printerr(2, "\n%s: uid %d (%s)\n", __func__, uid, clp->relpath);
8f2508
+
8f2508
+	info = alloc_upcall_info(clp, uid, clp->krb5_fd, NULL, NULL, NULL);
8f2508
+	if (info == NULL) {
8f2508
+		printerr(0, "%s: failed to allocate clnt_upcall_info\n", __func__);
8f2508
+		do_error_downcall(clp->krb5_fd, uid, -EACCES);
8f2508
+		return;
8f2508
+	}
8f2508
+	err = start_upcall_thread(gssd_work_thread_fn, info);
8f2508
+	if (err != 0) {
8f2508
+		do_error_downcall(clp->krb5_fd, uid, -EACCES);
8f2508
+		free_upcall_info(info);
8f2508
+	}
8f2508
+}
8f2508
+
8f2508
+void
8f2508
+handle_gssd_upcall(struct clnt_info *clp)
8f2508
+{
8f2508
+	uid_t			uid;
8f2508
+	char			lbuf[RPC_CHAN_BUF_SIZE];
8f2508
+	int			lbuflen = 0;
8f2508
 	char			*p;
8f2508
 	char			*mech = NULL;
8f2508
 	char			*uidstr = NULL;
8f2508
@@ -742,20 +855,22 @@ handle_gssd_upcall(struct clnt_upcall_in
8f2508
 	char			*service = NULL;
8f2508
 	char			*srchost = NULL;
8f2508
 	char			*enctypes = NULL;
8f2508
-	char			*upcall_str;
8f2508
-	char			*pbuf = info->lbuf;
8f2508
 	pthread_t tid = pthread_self();
8f2508
+	struct clnt_upcall_info	*info;
8f2508
+	int			err;
8f2508
 
8f2508
-	printerr(2, "\n%s(0x%x): '%s' (%s)\n", __func__, tid, 
8f2508
-		info->lbuf, clp->relpath);
8f2508
-
8f2508
-	upcall_str = strdup(info->lbuf);
8f2508
-	if (upcall_str == NULL) {
8f2508
-		printerr(0, "ERROR: malloc failure\n");
8f2508
-		goto out_nomem;
8f2508
+	lbuflen = read(clp->gssd_fd, lbuf, sizeof(lbuf));
8f2508
+	if (lbuflen <= 0 || lbuf[lbuflen-1] != '\n') {
8f2508
+		printerr(0, "WARNING: handle_gssd_upcall: "
8f2508
+			    "failed reading request\n");
8f2508
+		return;
8f2508
 	}
8f2508
+	lbuf[lbuflen-1] = 0;
8f2508
+
8f2508
+	printerr(2, "\n%s(0x%x): '%s' (%s)\n", __func__, tid,
8f2508
+		 lbuf, clp->relpath);
8f2508
 
8f2508
-	while ((p = strsep(&pbuf, " "))) {
8f2508
+	for (p = strtok(lbuf, " "); p; p = strtok(NULL, " ")) {
8f2508
 		if (!strncmp(p, "mech=", strlen("mech=")))
8f2508
 			mech = p + strlen("mech=");
8f2508
 		else if (!strncmp(p, "uid=", strlen("uid=")))
8f2508
@@ -773,8 +888,8 @@ handle_gssd_upcall(struct clnt_upcall_in
8f2508
 	if (!mech || strlen(mech) < 1) {
8f2508
 		printerr(0, "WARNING: handle_gssd_upcall: "
8f2508
 			    "failed to find gss mechanism name "
8f2508
-			    "in upcall string '%s'\n", upcall_str);
8f2508
-		goto out;
8f2508
+			    "in upcall string '%s'\n", lbuf);
8f2508
+		return;
8f2508
 	}
8f2508
 
8f2508
 	if (uidstr) {
8f2508
@@ -786,21 +901,21 @@ handle_gssd_upcall(struct clnt_upcall_in
8f2508
 	if (!uidstr) {
8f2508
 		printerr(0, "WARNING: handle_gssd_upcall: "
8f2508
 			    "failed to find uid "
8f2508
-			    "in upcall string '%s'\n", upcall_str);
8f2508
-		goto out;
8f2508
+			    "in upcall string '%s'\n", lbuf);
8f2508
+		return;
8f2508
 	}
8f2508
 
8f2508
 	if (enctypes && parse_enctypes(enctypes) != 0) {
8f2508
 		printerr(0, "WARNING: handle_gssd_upcall: "
8f2508
 			 "parsing encryption types failed: errno %d\n", errno);
8f2508
-		goto out;
8f2508
+		return;
8f2508
 	}
8f2508
 
8f2508
 	if (target && strlen(target) < 1) {
8f2508
 		printerr(0, "WARNING: handle_gssd_upcall: "
8f2508
 			 "failed to parse target name "
8f2508
-			 "in upcall string '%s'\n", upcall_str);
8f2508
-		goto out;
8f2508
+			 "in upcall string '%s'\n", lbuf);
8f2508
+		return;
8f2508
 	}
8f2508
 
8f2508
 	/*
8f2508
@@ -814,21 +929,26 @@ handle_gssd_upcall(struct clnt_upcall_in
8f2508
 	if (service && strlen(service) < 1) {
8f2508
 		printerr(0, "WARNING: handle_gssd_upcall: "
8f2508
 			 "failed to parse service type "
8f2508
-			 "in upcall string '%s'\n", upcall_str);
8f2508
-		goto out;
8f2508
+			 "in upcall string '%s'\n", lbuf);
8f2508
+		return;
8f2508
 	}
8f2508
 
8f2508
-	if (strcmp(mech, "krb5") == 0 && clp->servername)
8f2508
-		process_krb5_upcall(clp, uid, clp->gssd_fd, srchost, target, service);
8f2508
-	else {
8f2508
+	if (strcmp(mech, "krb5") == 0 && clp->servername) {
8f2508
+		info = alloc_upcall_info(clp, uid, clp->gssd_fd, srchost, target, service);
8f2508
+		if (info == NULL) {
8f2508
+			printerr(0, "%s: failed to allocate clnt_upcall_info\n", __func__);
8f2508
+			do_error_downcall(clp->gssd_fd, uid, -EACCES);
8f2508
+			return;
8f2508
+		}
8f2508
+		err = start_upcall_thread(gssd_work_thread_fn, info);
8f2508
+		if (err != 0) {
8f2508
+			do_error_downcall(clp->gssd_fd, uid, -EACCES);
8f2508
+			free_upcall_info(info);
8f2508
+		}
8f2508
+	} else {
8f2508
 		if (clp->servername)
8f2508
 			printerr(0, "WARNING: handle_gssd_upcall: "
8f2508
 				 "received unknown gss mech '%s'\n", mech);
8f2508
 		do_error_downcall(clp->gssd_fd, uid, -EACCES);
8f2508
 	}
8f2508
-out:
8f2508
-	free(upcall_str);
8f2508
-out_nomem:
8f2508
-	free_upcall_info(info);
8f2508
-	return;
8f2508
 }