linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guillem Jover <gjover@sipwise.com>
To: libtirpc-devel@lists.sourceforge.net
Cc: linux-nfs@vger.kernel.org
Subject: [PATCH] Do not bind to reserved ports registered in /etc/services
Date: Wed, 10 Jan 2018 01:49:20 +0100	[thread overview]
Message-ID: <20180110004920.11100-1-gjover@sipwise.com> (raw)

When using the bindresvport() function a privileged port will be looked
for and bound to a socket. The problem is that any service using a static
privileged port registered in the /etc/services file, can get its port
taken over by libtirpc users, making the other service fail to start.

Starting the other service before libtircp users is not an option as
this does not cover restart situations, for example during package
upgrades or HA switchovers and similar.

In addition honoring the /etc/services registry is already done for
example by rpc.statd, so it seems obvious to make libtirpc follow this
same pattern too.

Signed-off-by: Guillem Jover <gjover@sipwise.com>
---
 src/bindresvport.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/bindresvport.c b/src/bindresvport.c
index 2d8f2bc..98e5f40 100644
--- a/src/bindresvport.c
+++ b/src/bindresvport.c
@@ -40,6 +40,7 @@
 #include <netinet/in.h>
 
 #include <errno.h>
+#include <netdb.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -73,12 +74,15 @@ bindresvport_sa(sd, sa)
         int sd;
         struct sockaddr *sa;
 {
-        int res, af;
+        int res, af, so_proto;
+        socklen_t so_proto_len;
         struct sockaddr_storage myaddr;
 	struct sockaddr_in *sin;
 #ifdef INET6
 	struct sockaddr_in6 *sin6;
 #endif
+	struct servent *se;
+	const char *se_proto;
 	u_int16_t *portp;
 	static u_int16_t port;
 	static short startport = STARTPORT;
@@ -125,6 +129,25 @@ bindresvport_sa(sd, sa)
         }
         sa->sa_family = af;
 
+        so_proto_len = sizeof(so_proto);
+        if (getsockopt(sd, SOL_SOCKET, SO_PROTOCOL, &so_proto, &so_proto_len) == -1) {
+                mutex_unlock(&port_lock);
+                return -1;      /* errno is correctly set */
+        }
+        switch (so_proto) {
+        case IPPROTO_UDP:
+        case IPPROTO_UDPLITE:
+                se_proto = "udp";
+                break;
+        case IPPROTO_TCP:
+                se_proto = "tcp";
+                break;
+        default:
+                errno = ENOPROTOOPT;
+                mutex_unlock(&port_lock);
+                return -1;
+        }
+
         if (port == 0) {
                 port = (getpid() % NPORTS) + STARTPORT;
         }
@@ -135,6 +158,9 @@ bindresvport_sa(sd, sa)
                 *portp = htons(port++);
                  if (port > endport) 
                         port = startport;
+                se = getservbyport(*portp, se_proto);
+                if (se != NULL)
+                        continue;
                 res = bind(sd, sa, salen);
 		if (res >= 0 || errno != EADDRINUSE)
 	                break;
-- 
2.15.1


             reply	other threads:[~2018-01-10  1:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-10  0:49 Guillem Jover [this message]
2018-01-11 15:18 ` [PATCH] Do not bind to reserved ports registered in /etc/services Steve Dickson
2018-01-12 18:41   ` Guillem Jover
2018-01-12 19:12     ` [Libtirpc-devel] " Thorsten Kukuk
2018-01-12 19:19     ` Tom Talpey
2018-02-08 18:07       ` Chuck Lever
2018-02-08 18:36         ` Chuck Lever
2018-03-06 18:09           ` Chuck Lever
2018-03-08 20:24             ` J. Bruce Fields
2018-03-08 21:26               ` J. Bruce Fields
2018-03-08 21:28                 ` [Libtirpc-devel] " Chuck Lever
2018-03-08 21:35                   ` Bruce Fields
2018-01-11 15:50 ` Chuck Lever
2018-01-12 18:05   ` Guillem Jover
2018-01-12 19:12     ` Chuck Lever
2018-01-12 21:12       ` [Libtirpc-devel] " Thorsten Kukuk
2018-01-12 21:14         ` Chuck Lever
2018-01-12 21:30           ` Matt Benjamin
2018-01-12 22:08         ` Steve Dickson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180110004920.11100-1-gjover@sipwise.com \
    --to=gjover@sipwise.com \
    --cc=libtirpc-devel@lists.sourceforge.net \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).