Skip to contentSkip to conversation

various random nfs-utils clean ups

PATCH In review Chuck Lever 2 participants 19 messages Sep 4, 2008 – Sep 5, 2008 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover various random nfs-utils clean ups

Hi Steve-

Here are a series of nfs-utils clean ups that I've culled out of my IPv6 series, including the RESTRICTED_STATD removal patches. They are non-IPv6 related, and should be easy to review.

Please pay close attention to the last one in the series. This one may be an actual bug, but it is difficult for me to test it thoroughly.

Would you consider these for the next release of nfs-utils?

---

Chuck Lever (16):
rpc.statd: Stop overloading sockfd in utils/statd/rmtcall.c rpc.statd: Clean up: replace "if (!(foo = rtnl))".
nfs-utils: whitespace clean ups in support/nfs/rpcmisc.c
nfs-utils: Remove excess log reporting
nfs-utils: make makesock() static
nfs-utils: Clean up support/nfs/rpcmisc.c:closedown()
nfs-utils: remove disabled code from support/nfs/rpcmisc.c
nfs-utils: Remove unused function rpc_svcrun()
nfs-utils: remove unused function rpc_logcall() sm-notify command: clean up error logging
sm-notify command: getaddrinfo(3) addrinfo leak sm-notify command: include <config.h>
showmount command: clean up error returns from connect_nb()
showmount: destroy RPC client when finished
rpc.statd: eliminate --secure_statd
rpc.statd: refactor check to see if call is from loopback address

 configure.ac                |    9 --
 support/include/rpcmisc.h   |    3 -
 support/nfs/rpcdispatch.c   |   51 ----------
 support/nfs/rpcmisc.c       |  225 ++++++++++++++-----------------------------
 utils/showmount/showmount.c |   27 +++--
 utils/statd/callback.c      |    6 -
 utils/statd/monitor.c       |  100 ++++++-------------
 utils/statd/rmtcall.c       |   28 +++--
 utils/statd/simu.c          |   35 ++-----
 utils/statd/sm-notify.c     |   59 +++++++----
 utils/statd/statd.c         |    4 -
 utils/statd/statd.h         |    1 
 12 files changed, 191 insertions(+), 357 deletions(-)
Signature · 1 line
Chuck Lever

A message that is not in the archive (sent off-list or not yet received): 20080904165122.1471.24485.stgit-meopP2rzCrTwdl/1UfZZQIVfYA8g3rJ/@public.gmane.org

#2 lore raw

PATCH 1/16 rpc.statd: refactor check to see if call is from loopback address

Refactor common logic to check if SM_FOO request is from loopback address.

We'll have to do something about this for IPv6. On IPv6-capable systems, there will be only one AF_INET6 listener. The loopback caller will get either an IPv6 loopback address, or a mapped IPv4 loopback -- either way this will be an AF_INET6 address.

---

 utils/statd/monitor.c |   82 ++++++++++++++++++++++---------------------------
 1 files changed, 36 insertions(+), 46 deletions(-)
utils/statd/monitor.c +36 -46
index eadc434..5d4aa49 100644
@@ -29,6 +29,36 @@ notify_list *		rtnl = NULL;	/* Run-time notify list. */
 
 #define LINELEN (4*(8+1)+SM_PRIV_SIZE*2+1)
 
+#ifdef RESTRICTED_STATD
+/*
+ * Reject requests from non-loopback addresses in order
+ * to prevent attack described in CERT CA-99.05.
+ */
+static int
+caller_is_localhost(struct svc_req *rqstp)
+{
+	struct in_addr	caller;
+
+	caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
+	if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
+		note(N_WARNING,
+			"Call to statd from non-local host %s",
+			inet_ntoa(caller));
+		return 0;
+	}
+	return 1;
+}
+#else	/* RESTRICTED_STATD */
+/*
+ * No restrictions for remote callers.
+ */
+static int
+caller_is_localhost(struct svc_req *rqstp)
+{
+	return 1;
+}
+#endif	/* RESTRICTED_STATD */
+
 /*
  * Services SM_MON requests.
  */
@@ -45,31 +75,19 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
 	notify_list	*clnt;
 	struct in_addr	my_addr;
 	char		*dnsname;
-#ifdef RESTRICTED_STATD
-	struct in_addr	caller;
-#endif
 	struct hostent	*hostinfo = NULL;
 
 	/* Assume that we'll fail. */
 	result.res_stat = STAT_FAIL;
 	result.state = -1;	/* State is undefined for STAT_FAIL. */
 
-	/* Restrict access to statd.
-	 * In the light of CERT CA-99.05, we tighten access to
-	 * statd.			--okir
-	 */
 #ifdef RESTRICTED_STATD
-	/* 1.	Reject anyone not calling from 127.0.0.1.
+	/* 1.	Reject any remote callers.
 	 *	Ignore the my_name specified by the caller, and
 	 *	use "127.0.0.1" instead.
 	 */
-	caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
-	if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
-		note(N_WARNING,
-			"Call to statd from non-local host %s",
-			inet_ntoa(caller));
+	if (!caller_is_localhost(rqstp))
 		goto failure;
-	}
 	my_addr.s_addr = htonl(INADDR_LOOPBACK);
 
 	/* 2.	Reject any registrations for non-lockd services.
@@ -329,25 +347,12 @@ sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
 			*my_name  = argp->my_id.my_name;
 	struct my_id	*id = &argp->my_id;
 	char		*cp;
-#ifdef RESTRICTED_STATD
-	struct in_addr	caller;
-#endif
 
 	result.state = MY_STATE;
 
-#ifdef RESTRICTED_STATD
-	/* 1.	Reject anyone not calling from 127.0.0.1.
-	 *	Ignore the my_name specified by the caller, and
-	 *	use "127.0.0.1" instead.
-	 */
-	caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
-	if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
-		note(N_WARNING,
-			"Call to statd from non-local host %s",
-			inet_ntoa(caller));
+	if (!caller_is_localhost(rqstp))
 		goto failure;
-	}
-#endif
+
 	/* my_name must not have white space */
 	for (cp=my_name ; *cp ; cp++)
 		if (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
@@ -388,9 +393,7 @@ sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
 			clnt = NL_NEXT(clnt);
 	}
 
-#ifdef RESTRICTED_STATD
  failure:
-#endif
 	note(N_WARNING, "Received erroneous SM_UNMON request from %s for %s",
 		my_name, mon_name);
 	return (&result);
@@ -404,21 +407,9 @@ sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
 	static sm_stat  result;
 	notify_list	*clnt;
 	char		*my_name = argp->my_name;
-#ifdef RESTRICTED_STATD
-	struct in_addr	caller;
 
-	/* 1.	Reject anyone not calling from 127.0.0.1.
-	 *	Ignore the my_name specified by the caller, and
-	 *	use "127.0.0.1" instead.
-	 */
-	caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
-	if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
-		note(N_WARNING,
-			"Call to statd from non-local host %s",
-			inet_ntoa(caller));
+	if (!caller_is_localhost(rqstp))
 		goto failure;
-	}
-#endif
 
 	result.state = MY_STATE;
 
@@ -457,8 +448,7 @@ sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
 		dprintf(N_DEBUG, "SM_UNMON_ALL request from %s with no "
 			"SM_MON requests from it.", my_name);
 	}
-#ifdef RESTRICTED_STATD
+
  failure:
-#endif
 	return (&result);
 }
#3 lore raw

PATCH 2/16 rpc.statd: eliminate --secure_statd

In reply to a message not in the archive

Clean up: Remove RESTRICTED_STATD to help make IPv6 changes simpler. We keep the code behind RESTRICTED_STATD, and toss anything that is compiled out when it is set.

RESTRICTED_STATD was added almost 10 years ago in response to CERT CERT CA-99.05, which addresses exposures in rpc.statd that might allow an attacker to take advantage of buffer overflows in rpc.statd while it is running in privileged mode.

These days, I can't think of a reason why anyone would want to run rpc.statd without setting RESTRICTED_STATD. In addition, I don't think rpc.statd is ever tested without it.

Removing RESTRICTED_STATD will get rid of some address storage and comparison issues that will make IPv6 support simpler. Plus it will make our test matrix smaller!

---

 configure.ac          |    9 ---------
 utils/statd/monitor.c |   34 ----------------------------------
 utils/statd/simu.c    |   35 +++++++++++++----------------------
 3 files changed, 13 insertions(+), 65 deletions(-)
configure.ac +0 -9
index 6ae6c6d..1ab89db 100644
@@ -95,15 +95,6 @@ AC_ARG_ENABLE(kprefix,
 	test "$enableval" = "yes" && kprefix=k,
 	kprefix=)
 	AC_SUBST(kprefix)
-AC_ARG_ENABLE(secure-statd,
-	[AC_HELP_STRING([--enable-secure-statd],
-                        [Only lockd can use statd (security)])],
-	test "$enableval" = "yes" && secure_statd=yes,
-	secure_statd=yes)
-	if test "$secure_statd" = yes; then
-		AC_DEFINE(RESTRICTED_STATD, 1, [Define this if you want to enable various security checks in statd. These checks basically keep anyone but lockd from using this service.])
-	fi
-	AC_SUBST(secure_statd)
 AC_ARG_WITH(rpcgen,
 	[AC_HELP_STRING([--with-rpcgen=internal], [use internal rpcgen instead of system one])],
 	rpcgen_path=$withval,
utils/statd/monitor.c +0 -34
index 5d4aa49..d300338 100644
@@ -29,7 +29,6 @@ notify_list *		rtnl = NULL;	/* Run-time notify list. */
 
 #define LINELEN (4*(8+1)+SM_PRIV_SIZE*2+1)
 
-#ifdef RESTRICTED_STATD
 /*
  * Reject requests from non-loopback addresses in order
  * to prevent attack described in CERT CA-99.05.
@@ -48,16 +47,6 @@ caller_is_localhost(struct svc_req *rqstp)
 	}
 	return 1;
 }
-#else	/* RESTRICTED_STATD */
-/*
- * No restrictions for remote callers.
- */
-static int
-caller_is_localhost(struct svc_req *rqstp)
-{
-	return 1;
-}
-#endif	/* RESTRICTED_STATD */
 
 /*
  * Services SM_MON requests.
@@ -81,7 +70,6 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
 	result.res_stat = STAT_FAIL;
 	result.state = -1;	/* State is undefined for STAT_FAIL. */
 
-#ifdef RESTRICTED_STATD
 	/* 1.	Reject any remote callers.
 	 *	Ignore the my_name specified by the caller, and
 	 *	use "127.0.0.1" instead.
@@ -107,28 +95,6 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
 		goto failure;
 	}
 
-#if 0
-	This is not usable anymore.  Linux-kernel can be configured to use
-	host names with NSM so that multi-homed hosts are handled properly.
-		NeilBrown 15mar2007
-
-	/* 3.	mon_name must be an address in dotted quad.
-	 *	Again, specific to the linux kernel lockd.
-	 */
-	if (!inet_aton(mon_name, &mon_addr)) {
-		note(N_WARNING,
-			"Attempt to register host %s (not a dotted quad)",
-			mon_name);
-		goto failure;
-	}
-#endif
-#else
-	if (!(hostinfo = gethostbyname(my_name))) {
-		note(N_WARNING, "gethostbyname error for %s", my_name);
-		goto failure;
-	} else
-		my_addr = *(struct in_addr *) hostinfo->h_addr;
-#endif
 	/*
 	 * Check hostnames.  If I can't look them up, I won't monitor.  This
 	 * might not be legal, but it adds a little bit of safety and sanity.
utils/statd/simu.c +13 -22
index 82d794e..25e8bad 100644
@@ -22,35 +22,26 @@ void *
 sm_simu_crash_1_svc (void *argp, struct svc_req *rqstp)
 {
   static char *result = NULL;
+  struct in_addr caller;
+
+  caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
+  if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
+    note(N_WARNING, "Call to statd from non-local host %s",
+      inet_ntoa(caller));
+    goto failure;
+  }
+
+  if (ntohs(svc_getcaller(rqstp->rq_xprt)->sin_port) >= 1024) {
+    note(N_WARNING, "Call to statd-simu-crash from unprivileged port");
+    goto failure;
+  }
 
-#ifdef RESTRICTED_STATD
-	struct in_addr	caller;
-
-	/* 1.	Reject anyone not calling from 127.0.0.1.
-	 *	Ignore the my_name specified by the caller, and
-	 *	use "127.0.0.1" instead.
-	 */
-	caller = svc_getcaller(rqstp->rq_xprt)->sin_addr;
-	if (caller.s_addr != htonl(INADDR_LOOPBACK)) {
-		note(N_WARNING,
-			"Call to statd from non-local host %s",
-			inet_ntoa(caller));
-		goto failure;
-	}
-	if (ntohs(svc_getcaller(rqstp->rq_xprt)->sin_port) >= 1024) {
-		note(N_WARNING,
-		     "Call to statd-simu-crash from unprivileged port\n");
-		goto failure;
-	}
-#endif
   note (N_WARNING, "*** SIMULATING CRASH! ***");
   my_svc_exit ();
 
   if (rtnl)
     nlist_kill (&rtnl);
 
-#ifdef RESTRICTED_STATD
  failure:
-#endif
   return ((void *)&result);
 }
#4 lore raw

PATCH 3/16 showmount: destroy RPC client when finished

In reply to a message not in the archive

Clean up: call clnt_destroy() in the showmount command as needed to destroy the RPC client properly (and close the associated socket) before the program exits.

---

 utils/showmount/showmount.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
utils/showmount/showmount.c +4 -1
index 213a573..b6cea92 100644
@@ -393,6 +393,7 @@ int main(int argc, char **argv)
 			total_timeout);
 		if (clnt_stat != RPC_SUCCESS) {
 			clnt_perror(mclient, "rpc mount export");
+			clnt_destroy(mclient);
 			exit(1);
 		}
 		if (headers)
@@ -416,6 +417,7 @@ int main(int argc, char **argv)
 			printf("\n");
 			exportlist = exportlist->ex_next;
 		}
+		clnt_destroy(mclient);
 		exit(0);
 	}
 
@@ -426,8 +428,10 @@ int main(int argc, char **argv)
 		total_timeout);
 	if (clnt_stat != RPC_SUCCESS) {
 		clnt_perror(mclient, "rpc mount dump");
+		clnt_destroy(mclient);
 		exit(1);
 	}
+	clnt_destroy(mclient);
 
 	n = 0;
 	for (list = dumplist; list; list = list->ml_next)
@@ -481,4 +485,3 @@ int main(int argc, char **argv)
 	}
 	exit(0);
 }
-
#5 lore raw

PATCH 4/16 showmount command: clean up error returns from connect_nb()

In reply to a message not in the archive

Clean up connect_nb() in the showmount command.

Sometimes it returns -1 on error, and sometimes a negative errno. On error, it should always return one of these or the other, not both.

Similar functions in other parts of nfs-utils return -1 on error, and set errno; so let's do that here too.

---

 utils/showmount/showmount.c |   22 ++++++++++++----------
 1 files changed, 12 insertions(+), 10 deletions(-)
utils/showmount/showmount.c +12 -10
index b6cea92..eafcf4c 100644
@@ -83,7 +83,8 @@ static void usage(FILE *fp, int n)
  *  tout contains the timeout.  It will be modified to contain the time
  *  remaining (i.e. time provided - time elasped).
  *
- *  Returns 0 for success 
+ *  Returns zero on success; otherwise, -1 is returned and errno is set
+ *  to reflect the nature of the error.
  */
 static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
 {
@@ -107,7 +108,7 @@ static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
 	len = sizeof(struct sockaddr);
 	ret = connect(fd, (struct sockaddr *)addr, len);
 	if (ret < 0 && errno != EINPROGRESS) {
-		ret = -errno;
+		ret = -1;
 		goto done;
 	}
 
@@ -121,9 +122,8 @@ static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
 	ret = select(fd + 1, NULL, &rset, NULL, tout);
 	if (ret <= 0) {
 		if (ret == 0)
-			ret = -ETIMEDOUT;
-		else
-			ret = -errno;
+			errno = ETIMEDOUT;
+		ret = -1;
 		goto done;
 	}
 
@@ -133,13 +133,15 @@ static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
 		len = sizeof(ret);
 		status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
 		if (status < 0) {
-			ret = -errno;
+			ret = -1;
 			goto done;
 		}
 
 		/* Oops - something wrong with connect */
-		if (ret)
-			ret = -ret;
+		if (ret != 0) {
+			errno = ret;
+			ret = -1;
+		}
 	}
 
 done:
@@ -179,10 +181,10 @@ static unsigned short getport(struct sockaddr_in *addr,
 		tout.tv_sec = TIMEOUT_TCP;
 
 		ret = connect_nb(sock, &saddr, &tout);
-		if (ret < 0) {
-			close(sock);
+		if (ret != 0) {
 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 			rpc_createerr.cf_error.re_errno = errno;
+			close(sock);
 			return 0;
 		}
 		client = clnttcp_create(&saddr,
#6 lore raw

PATCH 5/16 sm-notify command: include <config.h>

In reply to a message not in the archive

Clean up: Include config.h as other source files do; instead of using "config.h" use the HAVE_CONFIG_H macro and include <config.h>.

---

 utils/statd/sm-notify.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
utils/statd/sm-notify.c +4 -2
index b75f11b..5a02394 100644
@@ -4,6 +4,10 @@
  * Copyright (C) 2004-2006 Olaf Kirch <[email protected]>
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
@@ -24,8 +28,6 @@
 #include <errno.h>
 #include <grp.h>
 
-#include "config.h"
-
 #ifndef BASEDIR
 # ifdef NFS_STATEDIR
 #  define BASEDIR		NFS_STATEDIR
#7 lore raw

PATCH 6/16 sm-notify command: getaddrinfo(3) addrinfo leak

In reply to a message not in the archive

Make sure the results of getaddrinfo(3) are properly freed in notify().

Note this is a one-time addrinfo allocation that would be automatically freed when sm-notify exits anyway, so this is more of a nit than a real bug fix.

---

 utils/statd/sm-notify.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
utils/statd/sm-notify.c +4 -1
index 5a02394..b69f4cc 100644
@@ -241,8 +241,11 @@ notify(void)
 				opt_srcaddr);
 			exit(1);
 		}
-		memcpy(&local_addr, ai->ai_addr, ai->ai_addrlen);
+
 		/* We know it's IPv4 at this point */
+		memcpy(&local_addr, ai->ai_addr, ai->ai_addrlen);
+
+		freeaddrinfo(ai);
 	}
 
 	/* Use source port if provided on the command line,
#8 lore raw

PATCH 7/16 sm-notify command: clean up error logging

In reply to a message not in the archive

Clean up a few issues with logging in sm-notify.c.

Sometimes in sm-notify, when a system call fails the problem is reported to stderr but not logged, and then usually sm-notify exits. In cases like this, there are probably more hosts to notify, but sm-notify dies silently.

Make sure these errors are logged, and that the log messages explain the nature of the problem.

Also, if sm-notify exits prematurely, make sure this is always reported at the LOG_ERR level, not at the LOG_WARNING level.

Remove a couple of unnecessary '\n' in the arguments of nsm_log() calls -- nsm_log() already appends an '\n' to the message.

Finally, use exit() consistently in main().

---

 utils/statd/sm-notify.c |   48 ++++++++++++++++++++++++++++-------------------
 1 files changed, 29 insertions(+), 19 deletions(-)
utils/statd/sm-notify.c +29 -19
index b69f4cc..e5ca904 100644
@@ -134,7 +134,7 @@ main(int argc, char **argv)
 			    _SM_STATE_PATH == NULL ||
 			    _SM_DIR_PATH == NULL ||
 			    _SM_BAK_PATH == NULL) {
-				nsm_log(LOG_WARNING, "unable to allocate memory");
+				nsm_log(LOG_ERR, "unable to allocate memory");
 				exit(1);
 			}
 			strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
@@ -151,7 +151,7 @@ main(int argc, char **argv)
 usage:		fprintf(stderr,
 			"Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
 			"            [-P /path/to/state/directory] [-v my_host_name]\n");
-		return 1;
+		exit(1);
 	}
 
 	if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) {
@@ -164,8 +164,9 @@ usage:		fprintf(stderr,
 		strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
 	} else
 	if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
-		perror("gethostname");
-		return 1;
+		nsm_log(LOG_ERR, "Failed to obtain name of local host: %s",
+			strerror(errno));
+		exit(1);
 	}
 
 	backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
@@ -183,9 +184,9 @@ usage:		fprintf(stderr,
 		log_syslog = 1;
 
 		if (daemon(0, 0) < 0) {
-			nsm_log(LOG_WARNING, "unable to background: %s",
+			nsm_log(LOG_ERR, "unable to background: %s",
 					strerror(errno));
-			return 1;
+			exit(1);
 		}
 
 		close(0);
@@ -204,10 +205,10 @@ usage:		fprintf(stderr,
 				"Unable to notify %s, giving up",
 				hp->name);
 		}
-		return 1;
+		exit(1);
 	}
 
-	return 0;
+	exit(0);
 }
 
 /*
@@ -224,7 +225,8 @@ notify(void)
  retry:
 	sock = socket(AF_INET, SOCK_DGRAM, 0);
 	if (sock < 0) {
-		perror("socket");
+		nsm_log(LOG_ERR, "Failed to create RPC socket: %s",
+			strerror(errno));
 		exit(1);
 	}
 	fcntl(sock, F_SETFL, O_NONBLOCK);
@@ -236,8 +238,8 @@ notify(void)
 	if (opt_srcaddr) {
 		struct addrinfo *ai = host_lookup(AF_INET, opt_srcaddr);
 		if (!ai) {
-			nsm_log(LOG_WARNING,
-				"Not a valid hostname or address: \"%s\"\n",
+			nsm_log(LOG_ERR,
+				"Not a valid hostname or address: \"%s\"",
 				opt_srcaddr);
 			exit(1);
 		}
@@ -253,7 +255,8 @@ notify(void)
 	if (opt_srcport) {
 		addr_set_port(&local_addr, opt_srcport);
 		if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
-			perror("bind");
+			nsm_log(LOG_ERR, "Failed to bind RPC socket: %s",
+				strerror(errno));
 			exit(1);
 		}
 	} else {
@@ -492,7 +495,8 @@ recv_reply(int sock)
 		 * packet)
 		 */
 		if (p <= end) {
-			nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
+			nsm_log(LOG_DEBUG, "Host %s notified successfully",
+					hp->name);
 			unlink(hp->path);
 			free(hp->name);
 			free(hp->path);
@@ -516,7 +520,8 @@ backup_hosts(const char *dirname, const char *bakname)
 	DIR		*dir;
 
 	if (!(dir = opendir(dirname))) {
-		perror(dirname);
+		nsm_log(LOG_WARNING,
+			"Failed to open %s: %s", dirname, strerror(errno));
 		return;
 	}
 
@@ -548,7 +553,8 @@ get_hosts(const char *dirname)
 	DIR		*dir;
 
 	if (!(dir = opendir(dirname))) {
-		perror(dirname);
+		nsm_log(LOG_WARNING,
+			"Failed to open %s: %s", dirname, strerror(errno));
 		return;
 	}
 
@@ -561,6 +567,10 @@ get_hosts(const char *dirname)
 			continue;
 		if (host == NULL)
 			host = calloc(1, sizeof(*host));
+		if (host == NULL) {
+			nsm_log(LOG_WARNING, "Unable to allocate memory");
+			return;
+		}
 
 		snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
 		if (stat(path, &stb) < 0)
@@ -665,17 +675,17 @@ nsm_get_state(int update)
 		snprintf(newfile, sizeof(newfile),
 				"%s.new", _SM_STATE_PATH);
 		if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
-			nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
+			nsm_log(LOG_ERR, "Cannot create %s: %m", newfile);
 			exit(1);
 		}
 		if (write(fd, &state, sizeof(state)) != sizeof(state)) {
-			nsm_log(LOG_WARNING,
+			nsm_log(LOG_ERR,
 				"Failed to write state to %s", newfile);
 			exit(1);
 		}
 		close(fd);
 		if (rename(newfile, _SM_STATE_PATH) < 0) {
-			nsm_log(LOG_WARNING,
+			nsm_log(LOG_ERR,
 				"Cannot create %s: %m", _SM_STATE_PATH);
 			exit(1);
 		}
@@ -784,7 +794,7 @@ static void drop_privs(void)
 
 	if (st.st_uid == 0) {
 		nsm_log(LOG_WARNING,
-			"sm-notify running as root. chown %s to choose different user\n",
+			"sm-notify running as root. chown %s to choose different user",
 		    _SM_DIR_PATH);
 		return;
 	}
#9 lore raw

PATCH 8/16 nfs-utils: remove unused function rpc_logcall()

In reply to a message not in the archive

Clean up: Eliminate rpc_logcall(), which has no callers.

---

 support/include/rpcmisc.h |    1 -
 support/nfs/rpcmisc.c     |   60 ---------------------------------------------
 2 files changed, 0 insertions(+), 61 deletions(-)
support/include/rpcmisc.h +0 -1
index a2b2b5a..665df8e 100644
@@ -50,7 +50,6 @@ void		rpc_svcrun(void);
 void		rpc_dispatch(struct svc_req *rq, SVCXPRT *xprt,
 				struct rpc_dtable *dtable, int nvers,
 				void *argp, void *resp);
-void		rpc_logcall(struct svc_req *, char *xname, char *args);
 
 extern int	_rpcpmstart;
 extern int	_rpcfdtype;
support/nfs/rpcmisc.c +0 -60

63 lines not on this page · raw message

#10 lore raw

PATCH 9/16 nfs-utils: Remove unused function rpc_svcrun()

In reply to a message not in the archive

Clean up: remove function that has been disabled (via #if 0) for almost a decade.

---

 support/include/rpcmisc.h |    1 -
 support/nfs/rpcdispatch.c |   51 ---------------------------------------------
 2 files changed, 0 insertions(+), 52 deletions(-)
support/include/rpcmisc.h +0 -1
index 665df8e..35c5011 100644
@@ -46,7 +46,6 @@ int		makesock(int port, int proto);
 void		rpc_init(char *name, int prog, int vers,
 				void (*dispatch)(struct svc_req *, SVCXPRT *),
 				int defport);
-void		rpc_svcrun(void);
 void		rpc_dispatch(struct svc_req *rq, SVCXPRT *xprt,
 				struct rpc_dtable *dtable, int nvers,
 				void *argp, void *resp);
support/nfs/rpcdispatch.c +0 -51

54 lines not on this page · raw message

#11 lore raw

PATCH 10/16 nfs-utils: remove disabled code from support/nfs/rpcmisc.c

In reply to a message not in the archive

After some recent discussions, we want to rely on the kernel's network layer to autotune socket buffers. Since this code is already disabled in support/nfs/rpcmisc.c (and has been for some time), let's just remove it.

---

 support/nfs/rpcmisc.c |   16 ----------------
 1 files changed, 0 insertions(+), 16 deletions(-)
support/nfs/rpcmisc.c +0 -16
index bd1a2db..40c2102 100644
@@ -192,22 +192,6 @@ int makesock(int port, int proto)
 			xlog(L_ERROR, "setsockopt failed: %s\n",
 			     strerror(errno));
 
-#if 0
-	/* I was told it didn't work with gigabit ethernet.
-	   Don't bothet with it.  H.J. */
-#ifdef SO_SNDBUF
-	{
-		int sblen, rblen;
-
-		/* 1024 for rpc & transport overheads */
-		sblen = rblen = socksz + 1024;
-		if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
-		    setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
-			xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
-	}
-#endif				/* SO_SNDBUF */
-#endif
-
 	if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
 		xlog(L_FATAL, "Could not bind name to socket: %s\n",
 					strerror(errno));
#12 lore raw

PATCH 11/16 nfs-utils: Clean up support/nfs/rpcmisc.c:closedown()

In reply to a message not in the archive

Clean up: update closedown()'s synopsis to modern C style, and move the function so we can remove the forward declaration.

---

 support/nfs/rpcmisc.c |   48 +++++++++++++++++++++++++-----------------------
 1 files changed, 25 insertions(+), 23 deletions(-)
support/nfs/rpcmisc.c +25 -23
index 40c2102..ad53a43 100644
@@ -38,7 +38,6 @@
 #define socklen_t int
 #endif
 
-static void	closedown(int sig);
 int	makesock(int port, int proto);
 
 #define _RPCSVC_CLOSEDOWN	120
@@ -46,6 +45,31 @@ int	_rpcpmstart = 0;
 int	_rpcfdtype = 0;
 int	_rpcsvcdirty = 0;
 
+static void
+closedown(int sig)
+{
+	(void) signal(sig, closedown);
+
+	if (_rpcsvcdirty == 0) {
+		static int size;
+		int i, openfd;
+
+		if (_rpcfdtype == SOCK_DGRAM)
+			exit(0);
+
+		if (size == 0)
+			size = getdtablesize();
+
+		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
+			if (FD_ISSET(i, &svc_fdset))
+				openfd++;
+		if (openfd <= 1)
+			exit(0);
+	}
+
+	(void) alarm(_RPCSVC_CLOSEDOWN);
+}
+
 void
 rpc_init(char *name, int prog, int vers,
 	 void (*dispatch)(struct svc_req *, register SVCXPRT *),
@@ -144,28 +168,6 @@ rpc_init(char *name, int prog, int vers,
 	}
 }
 
-static void closedown(sig)
-int sig;
-{
-	(void) signal(sig, closedown);
-	if (_rpcsvcdirty == 0) {
-		static int size;
-		int i, openfd;
-
-		if (_rpcfdtype == SOCK_DGRAM)
-			exit(0);
-		if (size == 0) {
-			size = getdtablesize();
-		}
-		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
-			if (FD_ISSET(i, &svc_fdset))
-				openfd++;
-		if (openfd <= 1)
-			exit(0);
-	}
-	(void) alarm(_RPCSVC_CLOSEDOWN);
-}
-
 int makesock(int port, int proto)
 {
 	struct sockaddr_in sin;
#13 lore raw

PATCH 12/16 nfs-utils: make makesock() static

In reply to a message not in the archive

Clean up: The makesock() function can become static since it is only used in rpcmisc.c, where it is defined. Fix some minor nits while we're in the area:

o Move it so we can remove it's forward declaration.

o Get rid of unneeded newlines in the xlog() format strings.

o Use htonl(INADDR_ANY) instead of INADDR_ANY to initialize sin_addr. Should make no run-time difference, but is slightly more proper, as the standard definition of INADDR_ANY is in host byte-order.

o Return RPC_ANYSOCK on error (this is -1) instead of open-coding -1.

o Remove the parentheses in the "return" statements.

---

 support/include/rpcmisc.h |    1 -
 support/nfs/rpcmisc.c     |   76 ++++++++++++++++++++++++---------------------
 2 files changed, 40 insertions(+), 37 deletions(-)
support/include/rpcmisc.h +0 -1
index 35c5011..5814a63 100644
@@ -42,7 +42,6 @@ struct rpc_dtable {
 	}
 
 
-int		makesock(int port, int proto);
 void		rpc_init(char *name, int prog, int vers,
 				void (*dispatch)(struct svc_req *, SVCXPRT *),
 				int defport);
support/nfs/rpcmisc.c +40 -36

91 lines not on this page · raw message

#14 lore raw

Re: [PATCH 12/16] nfs-utils: make makesock() static

Quoted 2 lines · On Thursday September 4, [email protected] wrote:

o Return RPC_ANYSOCK on error (this is -1) instead of open-coding -1.

I don't agree with this change.
Everything else (including the last) looks fine, though I personally wouldn't bother closing file descriptors and freeing memory just before exit.

RPC_ANYSOCK is an input value to various rpc library functions which means "I haven't opened a socket, you do it for me". It is not really a valid return value.

Every time makesock is called, the return status is tested with

     if (makesock(...) < 0)

To me, that says that the returned value should be e.g. -1, not RPC_ANYSOCK.

Thanks,
NeilBrown

A message that is not in the archive (sent off-list or not yet received): 18624.34805.396033.544222-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org

#16 lore raw

Re: [PATCH 12/16] nfs-utils: make makesock() static

Quoted 9 lines · Neil Brown
Quoted 2 lines · On Thursday September 4, [email protected] wrote:

o Return RPC_ANYSOCK on error (this is -1) instead of open-coding -1.

I don't agree with this change.

Everything else (including the last) looks fine, though I personally wouldn't bother closing file descriptors and freeing memory just before exit.

If nothing else, it makes interpreting the output of valgrind easier. :-)

Code in nfs-utils seems to last many many years, and is copied and pasted a lot without regard to context (ie code that may have been written for a single-shot application may suddenly find itself embedded in a long-running daemon). Considering that parts of nfs-utils are used in HA environments, I'd rather be safe then sorry.

Quoted 3 lines

RPC_ANYSOCK is an input value to various rpc library functions which means "I haven't opened a socket, you do it for me". It is not really a valid return value.

Well, FWIW it is treated as a return value by utils/mount/network.c:get_socket() and it's callers.

Quoted 5 lines

Every time makesock is called, the return status is tested with

    if (makesock(...) < 0)

To me, that says that the returned value should be e.g. -1, not RPC_ANYSOCK.

OK, I'll drop it. Thanks for the review.

--
Chuck Lever

#17 lore raw

PATCH 13/16 nfs-utils: Remove excess log reporting

In reply to a message not in the archive

Clean up: The makesock() function already reports an error if it can't create a socket. Remove the redundant error check and logging done in rpc_init() after a makesock() call.

---

 support/nfs/rpcmisc.c |   12 ++++--------
 1 files changed, 4 insertions(+), 8 deletions(-)
support/nfs/rpcmisc.c +4 -8
index 49e88e8..09bbbb7 100644
@@ -151,10 +151,8 @@ rpc_init(char *name, int prog, int vers,
 			}
 			if (defport == 0)
 				sock = RPC_ANYSOCK;
-			else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
-				xlog(L_FATAL, "%s: cannot make a UDP socket\n",
-						name);
-			}
+			else
+				sock = makesock(defport, IPPROTO_UDP);
 		}
 		if (sock == RPC_ANYSOCK)
 			sock = svcudp_socket (prog, 1);
@@ -181,10 +179,8 @@ rpc_init(char *name, int prog, int vers,
 			}
 			if (defport == 0)
 				sock = RPC_ANYSOCK;
-			else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
-				xlog(L_FATAL, "%s: cannot make a TCP socket\n",
-						name);
-			}
+			else
+				sock = makesock(defport, IPPROTO_TCP);
 		}
 		if (sock == RPC_ANYSOCK)
 			sock = svctcp_socket (prog, 1);
#18 lore raw

PATCH 14/16 nfs-utils: whitespace clean ups in support/nfs/rpcmisc.c

In reply to a message not in the archive

Clean up: fix a few spurious white space issues in support/nfs/rpcmisc.c.

---

 support/nfs/rpcmisc.c |   13 ++++++-------
 1 files changed, 6 insertions(+), 7 deletions(-)
support/nfs/rpcmisc.c +6 -7
index 09bbbb7..9b95e69 100644
@@ -1,9 +1,8 @@
 /*
- * support/nfs/rpcmisc.c
+ * Miscellaneous functions for RPC service startup and shutdown.
  *
- * Miscellaneous functions for RPC startup and shutdown.
  * This code is partially snarfed from rpcgen -s tcp -s udp,
- * partly written by Mark Shand, Donald Becker, and Rick 
+ * partly written by Mark Shand, Donald Becker, and Rick
  * Sladkey. It was tweaked slightly by Olaf Kirch to be
  * usable by both unfsd and mountd.
  *
@@ -122,7 +121,7 @@ rpc_init(char *name, int prog, int vers,
 	sock = 0;
 	if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
 	    && saddr.sin_family == AF_INET) {
-		socklen_t ssize = sizeof (int);
+		socklen_t ssize = sizeof(int);
 		int fdtype = 0;
 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
 				(char *)&fdtype, &ssize) == -1)
@@ -142,7 +141,7 @@ rpc_init(char *name, int prog, int vers,
 
 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
 		static SVCXPRT *last_transp = NULL;
- 
+
 		if (_rpcpmstart == 0) {
 			if (last_transp
 			    && (!defport || defport == last_transp->xp_port)) {
@@ -197,7 +196,7 @@ rpc_init(char *name, int prog, int vers,
 	}
 
 	if (_rpcpmstart) {
-		signal (SIGALRM, closedown);
-		alarm (_RPCSVC_CLOSEDOWN);
+		signal(SIGALRM, closedown);
+		alarm(_RPCSVC_CLOSEDOWN);
 	}
 }
#19 lore raw

PATCH 15/16 rpc.statd: Clean up: replace "if (!(foo = rtnl))".

In reply to a message not in the archive

Static code checkers flag this kind of thing because it's easy to confuse with "if (!(foo == rtnl))". In one of these cases, the combination of evaluation and assignment isn't even necessary.

While we are in the neighborhood, remove an extra argument to note() that is not called for in the passed-in format string.

---

 utils/statd/callback.c |    6 ++----
 utils/statd/monitor.c  |    6 ++++--
 2 files changed, 6 insertions(+), 6 deletions(-)
utils/statd/callback.c +2 -4
index d5d036e..505fdb3 100644
@@ -36,11 +36,9 @@ sm_notify_1_svc(struct stat_chge *argp, struct svc_req *rqstp)
 				argp->mon_name, argp->state);
 
 	/* quick check - don't bother if we're not monitoring anyone */
-	/* LH - this was != MULL, meaning that if anyone _was_ in our RTNL,
-	 * we'd never pass this point. */
-	if (!(lp = rtnl)) {
+	if (rtnl == NULL) {
 		note(N_WARNING, "SM_NOTIFY from %s while not monitoring any hosts.",
-				argp->mon_name, argp->state);
+				argp->mon_name);
 		return ((void *) &result);
 	}
 
utils/statd/monitor.c +4 -2
index d300338..a6a1d9c 100644
@@ -326,12 +326,13 @@ sm_unmon_1_svc(struct mon_id *argp, struct svc_req *rqstp)
 
 
 	/* Check if we're monitoring anyone. */
-	if (!(clnt = rtnl)) {
+	if (rtnl == NULL) {
 		note(N_WARNING,
 			"Received SM_UNMON request from %s for %s while not "
 			"monitoring any hosts.", my_name, argp->mon_name);
 		return (&result);
 	}
+	clnt = rtnl;
 
 	/*
 	 * OK, we are.  Now look for appropriate entry in run-time list.
@@ -379,11 +380,12 @@ sm_unmon_all_1_svc(struct my_id *argp, struct svc_req *rqstp)
 
 	result.state = MY_STATE;
 
-	if (!(clnt = rtnl)) {
+	if (rtnl == NULL) {
 		note(N_WARNING, "Received SM_UNMON_ALL request from %s "
 			"while not monitoring any hosts", my_name);
 		return (&result);
 	}
+	clnt = rtnl;
 
 	while ((clnt = nlist_gethost(clnt, my_name, 1))) {
 		if (NL_MY_PROC(clnt) == argp->my_proc &&
#20 lore raw

PATCH 16/16 rpc.statd: Stop overloading sockfd in utils/statd/rmtcall.c

In reply to a message not in the archive

The Linux kernel's lockd requires that rpc.statd perform notification callbacks from a privileged source port. To guarantee rpc.statd gets a privileged source port but runs unprivileged, it calls statd_get_socket() then drops root privileges before starting it's svc request processing loop.

Statd's svc request loop is the only caller of the process_foo() functions in utils/statd/rmtcall.c, but one of them, process_notify_list() attempts to invoke statd_get_socket() again.

In today's code, this is unneeded because statd_get_socket() is always invoked before my_svc_run(). However, if it ever succeeded, it would get an unprivileged source port anyway, causing the kernel to reject all subsequent requests from statd.

Thus the process_notify_list() function should not ever call statd_get_socket() because root privileges have been dropped by this point, and statd_get_socket() wouldn't get a privileged source port, causing the kernel to reject all subsequent SM_NOTIFY requests.

So all of the process_foo functions in utils/statd/rmtcall.c should use the global sockfd instead of a local copy, as it already has a privileged source port.

I've seen some unexplained behavior where statd starts making calls to the kernel via an unprivileged port. This could be one way that might occur.

---

 utils/statd/rmtcall.c |   28 ++++++++++++++++------------
 utils/statd/statd.c   |    4 ++--
 utils/statd/statd.h   |    1 +
 3 files changed, 19 insertions(+), 14 deletions(-)
utils/statd/rmtcall.c +16 -12
index eb1919a..748f5b8 100644
@@ -56,7 +56,15 @@ static unsigned long	xid = 0;	/* RPC XID counter */
 static int		sockfd = -1;	/* notify socket */
 
 /*
- * Initialize callback socket
+ * Initialize socket used to notify lockd of peer reboots.
+ *
+ * Returns the file descriptor of the new socket if successful;
+ * otherwise returns -1 and logs an error.
+ *
+ * Lockd rejects such requests if the source port is not privileged.
+ * statd_get_socket() must be invoked while statd still holds root
+ * privileges in order for the socket to acquire a privileged source
+ * port.
  */
 int
 statd_get_socket(void)
@@ -97,7 +105,7 @@ statd_get_socket(void)
 }
 
 static unsigned long
-xmit_call(int sockfd, struct sockaddr_in *sin,
+xmit_call(struct sockaddr_in *sin,
 	  u_int32_t prog, u_int32_t vers, u_int32_t proc,
 	  xdrproc_t func, void *obj)
 /* 		__u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
@@ -163,7 +171,7 @@ xmit_call(int sockfd, struct sockaddr_in *sin,
 }
 
 static notify_list *
-recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
+recv_rply(struct sockaddr_in *sin, u_long *portp)
 {
 	unsigned int		msgbuf[MAXMSGSIZE], msglen;
 	struct rpc_msg		mesg;
@@ -239,7 +247,7 @@ done:
  * Notify operation for a single list entry
  */
 static int
-process_entry(int sockfd, notify_list *lp)
+process_entry(notify_list *lp)
 {
 	struct sockaddr_in	sin;
 	struct status		new_status;
@@ -273,7 +281,7 @@ process_entry(int sockfd, notify_list *lp)
 	new_status.state    = NL_STATE(lp);
 	memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
 
-	lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
+	lp->xid = xmit_call(&sin, prog, vers, proc, func, objp);
 	if (!lp->xid) {
 		note(N_WARNING, "notify_host: failed to notify port %d\n",
 				ntohs(lp->port));
@@ -296,13 +304,13 @@ process_reply(FD_SET_TYPE *rfds)
 	if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
 		return 0;
 
-	if (!(lp = recv_rply(sockfd, &sin, &port)))
+	if (!(lp = recv_rply(&sin, &port)))
 		return 1;
 
 	if (lp->port == 0) {
 		if (port != 0) {
 			lp->port = htons((unsigned short) port);
-			process_entry(sockfd, lp);
+			process_entry(lp);
 			NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
 			nlist_remove(&notify, lp);
 			nlist_insert_timer(&notify, lp);
@@ -328,13 +336,9 @@ process_notify_list(void)
 {
 	notify_list	*entry;
 	time_t		now;
-	int		fd;
-
-	if ((fd = statd_get_socket()) < 0)
-		return 0;
 
 	while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
-		if (process_entry(fd, entry)) {
+		if (process_entry(entry)) {
 			NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
 			nlist_remove(&notify, entry);
 			nlist_insert_timer(&notify, entry);
utils/statd/statd.c +2 -2
index ea985e6..321f7a9 100644
@@ -75,7 +75,6 @@ static struct option longopts[] =
 };
 
 extern void sm_prog_1 (struct svc_req *, register SVCXPRT *);
-extern int statd_get_socket(void);
 static void load_state_number(void);
 
 #ifdef SIMULATIONS
@@ -477,7 +476,8 @@ int main (int argc, char **argv)
 		}
 
 	/* Make sure we have a privilege port for calling into the kernel */
-	statd_get_socket();
+	if (statd_get_socket() < 0)
+		exit(1);
 
 	/* If sm-notify didn't take all the state files, load
 	 * state information into our notify-list so we can
utils/statd/statd.h +1 -0
index 5d06d88..5a6e289 100644
@@ -48,6 +48,7 @@ extern void	change_state(void);
 extern void	my_svc_run(void);
 extern void	notify_hosts(void);
 extern void	shuffle_dirs(void);
+extern int	statd_get_socket(void);
 extern int	process_notify_list(void);
 extern int	process_reply(FD_SET_TYPE *);
 extern char *	xstrdup(const char *);