Blame SOURCES/nfs-utils-2.3.3-gssd-failed-thread.patch

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