Blame SOURCES/nfs-utils-1.3.0-mountd-dos.patch

64c563
diff -up nfs-utils-1.3.0/support/include/nfslib.h.orig nfs-utils-1.3.0/support/include/nfslib.h
64c563
--- nfs-utils-1.3.0/support/include/nfslib.h.orig	2014-03-25 11:12:07.000000000 -0400
64c563
+++ nfs-utils-1.3.0/support/include/nfslib.h	2014-11-14 11:16:06.785633197 -0500
64c563
@@ -174,6 +174,7 @@ void closeall(int min);
64c563
 
64c563
 int			svctcp_socket (u_long __number, int __reuse);
64c563
 int			svcudp_socket (u_long __number);
64c563
+int			svcsock_nonblock (int __sock);
64c563
 
64c563
 /* Misc shared code prototypes */
64c563
 size_t  strlcat(char *, const char *, size_t);
64c563
diff -up nfs-utils-1.3.0/support/nfs/rpcmisc.c.orig nfs-utils-1.3.0/support/nfs/rpcmisc.c
64c563
--- nfs-utils-1.3.0/support/nfs/rpcmisc.c.orig	2014-03-25 11:12:07.000000000 -0400
64c563
+++ nfs-utils-1.3.0/support/nfs/rpcmisc.c	2014-11-14 11:16:06.785633197 -0500
64c563
@@ -104,7 +104,7 @@ makesock(int port, int proto)
64c563
 		return -1;
64c563
 	}
64c563
 
64c563
-	return sock;
64c563
+	return svcsock_nonblock(sock);
64c563
 }
64c563
 
64c563
 void
64c563
diff -up nfs-utils-1.3.0/support/nfs/svc_create.c.orig nfs-utils-1.3.0/support/nfs/svc_create.c
64c563
--- nfs-utils-1.3.0/support/nfs/svc_create.c.orig	2014-03-25 11:12:07.000000000 -0400
64c563
+++ nfs-utils-1.3.0/support/nfs/svc_create.c	2014-11-14 11:16:06.785633197 -0500
64c563
@@ -49,6 +49,8 @@
64c563
 
64c563
 #ifdef HAVE_LIBTIRPC
64c563
 
64c563
+#include <rpc/rpc_com.h>
64c563
+
64c563
 #define SVC_CREATE_XPRT_CACHE_SIZE	(8)
64c563
 static SVCXPRT *svc_create_xprt_cache[SVC_CREATE_XPRT_CACHE_SIZE] = { NULL, };
64c563
 
64c563
@@ -277,6 +279,12 @@ svc_create_nconf_rand_port(const char *n
64c563
 			"(%s, %u, %s)", name, version, nconf->nc_netid);
64c563
 		return 0;
64c563
 	}
64c563
+	if (svcsock_nonblock(xprt->xp_fd) < 0) {
64c563
+		/* close() already done by svcsock_nonblock() */
64c563
+		xprt->xp_fd = RPC_ANYFD;
64c563
+		SVC_DESTROY(xprt);
64c563
+		return 0;
64c563
+	}
64c563
 
64c563
 	if (!svc_reg(xprt, program, version, dispatch, nconf)) {
64c563
 		/* svc_reg(3) destroys @xprt in this case */
64c563
@@ -332,6 +340,7 @@ svc_create_nconf_fixed_port(const char *
64c563
 		int fd;
64c563
 
64c563
 		fd = svc_create_sock(ai->ai_addr, ai->ai_addrlen, nconf);
64c563
+		fd = svcsock_nonblock(fd);
64c563
 		if (fd == -1)
64c563
 			goto out_free;
64c563
 
64c563
@@ -394,6 +403,7 @@ nfs_svc_create(char *name, const rpcprog
64c563
 	const struct sigaction create_sigaction = {
64c563
 		.sa_handler	= SIG_IGN,
64c563
 	};
64c563
+	int maxrec = RPC_MAXDATASIZE;
64c563
 	unsigned int visible, up, servport;
64c563
 	struct netconfig *nconf;
64c563
 	void *handlep;
64c563
@@ -405,6 +415,20 @@ nfs_svc_create(char *name, const rpcprog
64c563
 	 */
64c563
 	(void)sigaction(SIGPIPE, &create_sigaction, NULL);
64c563
 
64c563
+	/*
64c563
+	 * Setting MAXREC also enables non-blocking mode for tcp connections.
64c563
+	 * This avoids DOS attacks by a client sending many requests but never
64c563
+	 * reading the reply:
64c563
+	 * - if a second request already is present for reading in the socket,
64c563
+	 *   after the first request just was read, libtirpc will break the
64c563
+	 *   connection. Thus an attacker can't simply send requests as fast as
64c563
+	 *   he can without waiting for the response.
64c563
+	 * - if the write buffer of the socket is full, the next write() will
64c563
+	 *   fail with EAGAIN. libtirpc will retry the write in a loop for max.
64c563
+	 *   2 seconds. If write still fails, the connection will be closed.
64c563
+	 */   
64c563
+	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
64c563
+
64c563
 	handlep = setnetconfig();
64c563
 	if (handlep == NULL) {
64c563
 		xlog(L_ERROR, "Failed to access local netconfig database: %s",
64c563
diff -up nfs-utils-1.3.0/support/nfs/svc_socket.c.orig nfs-utils-1.3.0/support/nfs/svc_socket.c
64c563
--- nfs-utils-1.3.0/support/nfs/svc_socket.c.orig	2014-03-25 11:12:07.000000000 -0400
64c563
+++ nfs-utils-1.3.0/support/nfs/svc_socket.c	2014-11-14 11:16:06.785633197 -0500
64c563
@@ -67,6 +67,39 @@ int getservport(u_long number, const cha
64c563
 	return 0;
64c563
 }
64c563
 
64c563
+int
64c563
+svcsock_nonblock(int sock)
64c563
+{
64c563
+	int flags;
64c563
+
64c563
+	if (sock < 0)
64c563
+		return sock;
64c563
+
64c563
+	/* This socket might be shared among multiple processes
64c563
+	 * if mountd is run multi-threaded.  So it is safest to
64c563
+	 * make it non-blocking, else all threads might wake
64c563
+	 * one will get the data, and the others will block
64c563
+	 * indefinitely.
64c563
+	 * In all cases, transaction on this socket are atomic
64c563
+	 * (accept for TCP, packet-read and packet-write for UDP)
64c563
+	 * so O_NONBLOCK will not confuse unprepared code causing
64c563
+	 * it to corrupt messages.
64c563
+	 * It generally safest to have O_NONBLOCK when doing an accept
64c563
+	 * as if we get a RST after the SYN and before accept runs,
64c563
+	 * we can block despite being told there was an acceptable
64c563
+	 * connection.
64c563
+	 */
64c563
+	if ((flags = fcntl(sock, F_GETFL)) < 0)
64c563
+		perror(_("svc_socket: can't get socket flags"));
64c563
+	else if (fcntl(sock, F_SETFL, flags|O_NONBLOCK) < 0)
64c563
+		perror(_("svc_socket: can't set socket flags"));
64c563
+	else
64c563
+		return sock;
64c563
+
64c563
+	(void) __close(sock);
64c563
+	return -1;
64c563
+}
64c563
+
64c563
 static int
64c563
 svc_socket (u_long number, int type, int protocol, int reuse)
64c563
 {
64c563
@@ -104,38 +137,7 @@ svc_socket (u_long number, int type, int
64c563
       sock = -1;
64c563
     }
64c563
 
64c563
-  if (sock >= 0)
64c563
-    {
64c563
-	    /* This socket might be shared among multiple processes
64c563
-	     * if mountd is run multi-threaded.  So it is safest to
64c563
-	     * make it non-blocking, else all threads might wake
64c563
-	     * one will get the data, and the others will block
64c563
-	     * indefinitely.
64c563
-	     * In all cases, transaction on this socket are atomic
64c563
-	     * (accept for TCP, packet-read and packet-write for UDP)
64c563
-	     * so O_NONBLOCK will not confuse unprepared code causing
64c563
-	     * it to corrupt messages.
64c563
-	     * It generally safest to have O_NONBLOCK when doing an accept
64c563
-	     * as if we get a RST after the SYN and before accept runs,
64c563
-	     * we can block despite being told there was an acceptable
64c563
-	     * connection.
64c563
-	     */
64c563
-	int flags;
64c563
-	if ((flags = fcntl(sock, F_GETFL)) < 0)
64c563
-	  {
64c563
-	      perror (_("svc_socket: can't get socket flags"));
64c563
-	      (void) __close (sock);
64c563
-	      sock = -1;
64c563
-	  }
64c563
-	else if (fcntl(sock, F_SETFL, flags|O_NONBLOCK) < 0)
64c563
-	  {
64c563
-	      perror (_("svc_socket: can't set socket flags"));
64c563
-	      (void) __close (sock);
64c563
-	      sock = -1;
64c563
-	  }
64c563
-    }
64c563
-
64c563
-  return sock;
64c563
+  return svcsock_nonblock(sock);
64c563
 }
64c563
 
64c563
 /*