diff options
-rw-r--r-- | README.google | 6 | ||||
-rw-r--r-- | posix/Makefile | 2 | ||||
-rw-r--r-- | posix/tst-getaddrinfo6.c | 115 | ||||
-rw-r--r-- | sysdeps/posix/getaddrinfo.c | 2 |
4 files changed, 123 insertions, 2 deletions
diff --git a/README.google b/README.google index 68ca935..b8d22bc 100644 --- a/README.google +++ b/README.google @@ -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) diff --git a/posix/Makefile b/posix/Makefile index 05dc92c..e344d85 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -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 index 0000000..b515544 --- /dev/null +++ b/posix/tst-getaddrinfo6.c @@ -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" diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c index 2471a1a..1c53283 100644 --- a/sysdeps/posix/getaddrinfo.c +++ b/sysdeps/posix/getaddrinfo.c @@ -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; |