]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Fix parsing of numeric hosts in gethostbyname_r
authorAndreas Schwab <schwab@suse.de>
Mon, 21 Jan 2013 16:41:28 +0000 (17:41 +0100)
committerAdhemerval Zanella <azanella@linux.vnet.ibm.com>
Thu, 29 Jan 2015 18:36:01 +0000 (13:36 -0500)
Conflicts:
ChangeLog
NEWS

ChangeLog
NEWS
nss/Makefile
nss/digits_dots.c
nss/getXXbyYY_r.c
nss/test-digits-dots.c [new file with mode: 0644]

index bef6c5c780c59e63b7b8aa3b633233e16ec41c89..b15b0ab3fd49383b2244041b2ad2335b0b314dd1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2013-05-21  Andreas Schwab  <schwab@suse.de>
+
+       [BZ #15014]
+       * nss/getXXbyYY_r.c (INTERNAL (REENTRANT_NAME))
+       [HANDLE_DIGITS_DOTS]: Set any_service when digits-dots parsing was
+       successful.
+       * nss/digits_dots.c (__nss_hostname_digits_dots): Remove
+       redundant variable declarations and reallocation of buffer when
+       parsing as IPv6 address.  Always set NSS status when called from
+       reentrant functions.  Use NETDB_INTERNAL instead of TRY_AGAIN when
+       buffer too small.  Correct computation of needed size.
+       * nss/Makefile (tests): Add test-digits-dots.
+       * nss/test-digits-dots.c: New test.
+
 2012-11-19  Siddhesh Poyarekar  <siddhesh@redhat.com>
 
        [BZ #14719]
diff --git a/NEWS b/NEWS
index a9f0b50cf55b62253dd31ec92c567f5759643b76..7b466bfd90c48b561ed80b738be7a407726d6153 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,8 +10,8 @@ Version 2.16.1
 * The following bugs are resolved with this release:
 
   6530, 14195, 14547, 14459, 14476, 14562, 14621, 14648, 14699, 14719, 14756,
-  14831, 15078, 15754, 15755, 16072, 16431, 16617, 17048, 17137, 17187, 17325,
-  17625, 17630.
+  14831, 15014, 15078, 15754, 15755, 16072, 16431, 16617, 17048, 17137,
+  17187, 17325, 17625, 17630.
 
 * CVE-2104-7817 The wordexp function could ignore the WRDE_NOCMD flag
   under certain input conditions resulting in the execution of a shell for
index a272ebe13d434dd5f029fe41451eeae9a6cdb079..6cfbe9717ab52eaf1aac41c31900128ce2cbd690 100644 (file)
@@ -38,7 +38,7 @@ install-bin             := getent makedb
 makedb-modules = xmalloc hash-string
 extra-objs             += $(makedb-modules:=.o)
 
-tests                  = test-netdb tst-nss-test1
+tests                  = test-netdb tst-nss-test1 test-digits-dots
 xtests                 = bug-erange
 
 include ../Makeconfig
index 5b31ebc7e1f09c24c2231034674b6d42106907e6..ee530c69610d5393726ef25e73127b6a1a669cac 100644 (file)
@@ -46,7 +46,10 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
     {
       if (h_errnop)
        *h_errnop = NETDB_INTERNAL;
-      *result = NULL;
+      if (buffer_size == NULL)
+       *status = NSS_STATUS_TRYAGAIN;
+      else
+       *result = NULL;
       return -1;
     }
 
@@ -83,14 +86,16 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
        }
 
       size_needed = (sizeof (*host_addr)
-                    + sizeof (*h_addr_ptrs) + strlen (name) + 1);
+                    + sizeof (*h_addr_ptrs)
+                    + sizeof (*h_alias_ptr) + strlen (name) + 1);
 
       if (buffer_size == NULL)
         {
          if (buflen < size_needed)
            {
+             *status = NSS_STATUS_TRYAGAIN;
              if (h_errnop != NULL)
-               *h_errnop = TRY_AGAIN;
+               *h_errnop = NETDB_INTERNAL;
              __set_errno (ERANGE);
              goto done;
            }
@@ -109,7 +114,7 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
              *buffer_size = 0;
              __set_errno (save);
              if (h_errnop != NULL)
-               *h_errnop = TRY_AGAIN;
+               *h_errnop = NETDB_INTERNAL;
              *result = NULL;
              goto done;
            }
@@ -149,7 +154,9 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
                  if (! ok)
                    {
                      *h_errnop = HOST_NOT_FOUND;
-                     if (buffer_size)
+                     if (buffer_size == NULL)
+                       *status = NSS_STATUS_NOTFOUND;
+                     else
                        *result = NULL;
                      goto done;
                    }
@@ -190,7 +197,7 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
                  if (buffer_size == NULL)
                    *status = NSS_STATUS_SUCCESS;
                  else
-                  *result = resbuf;
+                   *result = resbuf;
                  goto done;
                }
 
@@ -201,15 +208,6 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
 
       if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
        {
-         const char *cp;
-         char *hostname;
-         typedef unsigned char host_addr_t[16];
-         host_addr_t *host_addr;
-         typedef char *host_addr_list_t[2];
-         host_addr_list_t *h_addr_ptrs;
-         size_t size_needed;
-         int addr_size;
-
          switch (af)
            {
            default:
@@ -225,7 +223,10 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
              /* This is not possible.  We cannot represent an IPv6 address
                 in an `struct in_addr' variable.  */
              *h_errnop = HOST_NOT_FOUND;
-             *result = NULL;
+             if (buffer_size == NULL)
+               *status = NSS_STATUS_NOTFOUND;
+             else
+               *result = NULL;
              goto done;
 
            case AF_INET6:
@@ -233,42 +234,6 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
              break;
            }
 
-         size_needed = (sizeof (*host_addr)
-                        + sizeof (*h_addr_ptrs) + strlen (name) + 1);
-
-         if (buffer_size == NULL && buflen < size_needed)
-           {
-             if (h_errnop != NULL)
-               *h_errnop = TRY_AGAIN;
-             __set_errno (ERANGE);
-             goto done;
-           }
-         else if (buffer_size != NULL && *buffer_size < size_needed)
-           {
-             char *new_buf;
-             *buffer_size = size_needed;
-             new_buf = realloc (*buffer, *buffer_size);
-
-             if (new_buf == NULL)
-               {
-                 save = errno;
-                 free (*buffer);
-                 __set_errno (save);
-                 *buffer = NULL;
-                 *buffer_size = 0;
-                 *result = NULL;
-                 goto done;
-               }
-             *buffer = new_buf;
-           }
-
-         memset (*buffer, '\0', size_needed);
-
-         host_addr = (host_addr_t *) *buffer;
-         h_addr_ptrs = (host_addr_list_t *)
-           ((char *) host_addr + sizeof (*host_addr));
-         hostname = (char *) h_addr_ptrs + sizeof (*h_addr_ptrs);
-
          for (cp = name;; ++cp)
            {
              if (!*cp)
@@ -281,7 +246,9 @@ __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
                  if (inet_pton (AF_INET6, name, host_addr) <= 0)
                    {
                      *h_errnop = HOST_NOT_FOUND;
-                     if (buffer_size)
+                     if (buffer_size == NULL)
+                       *status = NSS_STATUS_NOTFOUND;
+                     else
                        *result = NULL;
                      goto done;
                    }
index 50c04068f230c85f544037d453118d0e0d178344..c39c47ca4326189dfc6a313566b1758e39398671 100644 (file)
@@ -179,6 +179,9 @@ INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
     case -1:
       return errno;
     case 1:
+#ifdef NEED_H_ERRNO
+      any_service = true;
+#endif
       goto done;
     }
 #endif
@@ -280,7 +283,7 @@ done:
     /* Either we failed to lookup the functions or the functions themselves
        had a system error.  Set NETDB_INTERNAL here to let the caller know
        that the errno may have the real reason for failure.  */
-      *h_errnop = NETDB_INTERNAL;
+    *h_errnop = NETDB_INTERNAL;
   else if (status != NSS_STATUS_SUCCESS && !any_service)
     /* We were not able to use any service.  */
     *h_errnop = NO_RECOVERY;
diff --git a/nss/test-digits-dots.c b/nss/test-digits-dots.c
new file mode 100644 (file)
index 0000000..1efa344
--- /dev/null
@@ -0,0 +1,38 @@
+/* Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* Testcase for BZ #15014 */
+
+#include <stdlib.h>
+#include <netdb.h>
+#include <errno.h>
+
+static int
+do_test (void)
+{
+  char buf[32];
+  struct hostent *result = NULL;
+  struct hostent ret;
+  int h_err = 0;
+  int err;
+
+  err = gethostbyname_r ("1.2.3.4", &ret, buf, sizeof (buf), &result, &h_err);
+  return err == ERANGE && h_err == NETDB_INTERNAL ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"