]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Fix error handling in getaddrinfo.
authorStan Shebs <stanshebs@google.com>
Fri, 5 Aug 2016 21:12:59 +0000 (14:12 -0700)
committerStan Shebs <stanshebs@google.com>
Fri, 5 Aug 2016 21:12:59 +0000 (14:12 -0700)
README.google
posix/Makefile
posix/tst-getaddrinfo6.c [new file with mode: 0644]
sysdeps/posix/getaddrinfo.c

index 68ca935bdfb6ab61bd7bf3edfc36d193dd93f934..b8d22bc81a11000aa2089e54aeec75c1f3e647ca 100644 (file)
@@ -601,3 +601,9 @@ sysdeps/posix/getaddrinfo.c
   For b/30090956, fix stack overflow in hostent conversion (BZ20010, CVE-2016-3706)
   https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=4ab2ab03d4351914ee53248dc5aef4a8c88ff8b9
   (stanshebs, backport)
+
+sysdeps/posix/getaddrinfo.c
+posix/Makefile
+posix/tst-getaddrinfo6.c
+  For b/29615899, fix error handling in getaddrinfo.
+  (stanshebs, google-local)
index 05dc92c2b64f2168fcdeaca9fb854cf6f0b72003..e344d85e29b04306d1e4b024bc8142dd0eaa203f 100644 (file)
@@ -86,7 +86,7 @@ tests         := tstgetopt testfnm runtests runptests      \
                   tst-getaddrinfo3 tst-fnmatch2 tst-cpucount tst-cpuset \
                   bug-getopt1 bug-getopt2 bug-getopt3 bug-getopt4 \
                   bug-getopt5 tst-getopt_long1 bug-regex34 bug-regex35 \
-                  tst-pathconf tst-getaddrinfo4 tst-fnmatch3
+                  tst-pathconf tst-getaddrinfo4 tst-fnmatch3 tst-getaddrinfo6
 xtests         := bug-ga2
 ifeq (yes,$(build-shared))
 test-srcs      := globtest
diff --git a/posix/tst-getaddrinfo6.c b/posix/tst-getaddrinfo6.c
new file mode 100644 (file)
index 0000000..b515544
--- /dev/null
@@ -0,0 +1,115 @@
+/* Check that getaddrinfo still works after recovery from failure.
+   Copyright (C) 2016 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/>.  */
+
+/* Thanks to Dave Clausen for the test concept.  */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <errno.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#define NUMFDS 10
+int fds[NUMFDS];
+
+static int
+do_test (void)
+{
+  char hostname[1024];
+  struct rlimit rlim;
+  int gaierr;
+  struct addrinfo hints, *ai;
+
+  gethostname (hostname, sizeof (hostname));
+
+  /* Bring the limit of open files down to a small predictable value.  */
+
+  if (getrlimit (RLIMIT_NOFILE, &rlim) < 0)
+    {
+      printf ("error: getrlimit: %m\n");
+      return 1;
+    }
+  rlim.rlim_cur = NUMFDS;
+  if (setrlimit (RLIMIT_NOFILE, &rlim) < 0)
+    {
+      printf ("error: setrlimit: %m\n");
+      return 1;
+    }
+
+  memset (&hints, '\0', sizeof (hints));
+  hints.ai_family = AF_UNSPEC;
+  hints.ai_socktype = SOCK_STREAM;
+  hints.ai_flags = AI_CANONNAME;
+
+  gaierr = getaddrinfo (hostname, NULL, &hints, &ai);
+  if (gaierr != 0)
+    {
+      printf ("error: getaddrinfo: %s\n", gai_strerror (gaierr));
+      return 1;
+    }
+
+  // Create file descriptors until we completely run out.
+
+  memset (fds, '\0', sizeof (fds));
+  for (int i = 0; i < NUMFDS; ++i)
+    {
+      int fd = dup (STDOUT_FILENO);
+
+      if (fd == -1 && errno == EMFILE)
+       break;
+
+      fds[i] = fd;
+    }
+
+  gaierr = getaddrinfo (hostname, NULL, &hints, &ai);
+  if (gaierr != 0)
+    {
+      printf ("error (expected): getaddrinfo: %s\n", gai_strerror (gaierr));
+      /* Don't give up, we expected this error.  */
+    }
+
+  // Now close the fds, freeing them up for use.
+
+  for (int i = 0; i < NUMFDS; ++i)
+    {
+      if (fds[i] == 0)
+       break;
+
+      close (fds[i]);
+    }
+
+  /* This one should succeed again.  */
+
+  gaierr = getaddrinfo (hostname, NULL, &hints, &ai);
+  if (gaierr != 0)
+    {
+      printf ("error: getaddrinfo: %s\n", gai_strerror (gaierr));
+      return 1;
+    }
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+
+#include "../test-skeleton.c"
index 2471a1a458dc46c4b4da3411d295609730559e39..1c5328352ec624e68f3cc46e67d68e360fa6d2a8 100644 (file)
@@ -1067,7 +1067,7 @@ gaih_inet (const char *name, const struct gaih_service *service,
 
          _res.options |= old_res_options & RES_USE_INET6;
 
-         if (h_errno == NETDB_INTERNAL)
+         if (status == NSS_STATUS_UNAVAIL && h_errno == NETDB_INTERNAL)
            {
              result = -EAI_SYSTEM;
              goto free_and_return;