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

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