This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH]:bindresvport should check /etc/services


Hi All,

The glibc routine bindresvport(), in it's current implementation, randomly
tries to a bind to a port number in the range 600-1023. It does not check
if the selected port number has an entry in /etc/services. Due to this a
server using bindresvport() might grab a port which belongs to a well
known
service as described in /etc/services. And should the well known service
start later it would fail to bind to it's port.

The patch below would take care of this situation by making bindresvport()
ignore all ports which have an entry in /etc/services. This is achieved by
calling getservbyport() which returns NULL if a specified port number does
not have an entry in /etc/services.

----------------- START OF PATCH ------------------------------------------

2002-11-19      Raj Inguva      <irajasek@in.ibm.com>
        *sunrpc/bindrsvprt.c (bindrsvport): Before binding to a reserved
        port, check if an entry exists in /etc/services.

*** glibc-2.2.5/sunrpc/bindrsvprt.c Thu Apr 15 19:23:30 1999
--- glibc-2.2.5.fixed/sunrpc/bindrsvprt.c Fri Nov 22 17:23:05 2002
***************
*** 37,40 ****
--- 37,41 ----
  #include <sys/socket.h>
  #include <netinet/in.h>
+ #include <netdb.h>

  /*
***************
*** 45,49 ****
  {
    int res;
!   static short port;
    struct sockaddr_in myaddr;
    int i;
--- 46,50 ----
  {
    int res;
!   static unsigned short port;
    struct sockaddr_in myaddr;
    int i;
***************
*** 74,77 ****
--- 75,87 ----
    for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; ++i)
      {
+       if(getservbyport(htons(port),NULL))
+     {
+       port++;
+       if (port > ENDPORT)
+         {
+           port = STARTPORT;
+         }
+       continue;
+     }
        sin->sin_port = htons (port++);
        if (port > ENDPORT)
--------------------------------------------- END OF PATCH
----------------------------------------------------------------------

Please Cc me on your comments/feedback.

Best Regards,
Raj Inguva


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]