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

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