00db10
commit b57525f1a376149840f740a31535681c07152ba4
00db10
Author: Dmitry V. Levin <ldv@altlinux.org>
00db10
Date:   Thu Jun 18 21:40:46 2015 +0000
00db10
00db10
    Fix potential hanging of gethostbyaddr_r/gethostbyname_r
00db10
    
00db10
    When "reorder" resolver option is enabled, threads of a multi-threaded
00db10
    process could hang in gethostbyaddr_r, gethostbyname_r, or
00db10
    gethostbyname2_r.
00db10
    
00db10
    Due to a trivial bug in _res_hconf_reorder_addrs, simultaneous
00db10
    invocations of this function in a multi-threaded process could result to
00db10
    _res_hconf_reorder_addrs returning without releasing the lock it holds,
00db10
    causing other threads to block indefinitely while waiting for the lock
00db10
    that is not going to be released.
00db10
    
00db10
    [BZ #17977]
00db10
    * resolv/res_hconf.c (_res_hconf_reorder_addrs): Fix unlocking
00db10
    when initializing interface list, based on the bug analysis
00db10
    and the patch proposed by Eric Newton.
00db10
    * resolv/tst-res_hconf_reorder.c: New test.
00db10
    * resolv/Makefile [$(have-thread-library) = yes] (tests): Add
00db10
    tst-res_hconf_reorder.
00db10
    ($(objpfx)tst-res_hconf_reorder): Depend on $(libdl)
00db10
    and $(shared-thread-library).
00db10
    (tst-res_hconf_reorder-ENV): New variable.
00db10
00db10
Index: glibc-2.17-c758a686/resolv/Makefile
00db10
===================================================================
00db10
--- glibc-2.17-c758a686.orig/resolv/Makefile
00db10
+++ glibc-2.17-c758a686/resolv/Makefile
00db10
@@ -40,6 +40,7 @@ extra-libs := libresolv libnss_dns
00db10
 ifeq ($(have-thread-library),yes)
00db10
 extra-libs += libanl
00db10
 routines += gai_sigqueue
00db10
+tests += tst-res_hconf_reorder
00db10
 endif
00db10
 extra-libs-others = $(extra-libs)
00db10
 libresolv-routines := gethnamaddr res_comp res_debug	\
00db10
@@ -108,6 +108,9 @@ $(objpfx)libanl.so: $(common-objpfx)libc
00db10
 
00db10
 $(objpfx)ga_test: $(objpfx)libanl.so $(shared-thread-library)
00db10
 
00db10
+$(objpfx)tst-res_hconf_reorder: $(libdl) $(shared-thread-library)
00db10
+tst-res_hconf_reorder-ENV = RESOLV_REORDER=on
00db10
+
00db10
 $(objpfx)tst-leaks: $(objpfx)libresolv.so
00db10
 tst-leaks-ENV = MALLOC_TRACE=$(objpfx)tst-leaks.mtrace
00db10
 $(objpfx)mtrace-tst-leaks: $(objpfx)tst-leaks.out
00db10
Index: glibc-2.17-c758a686/resolv/res_hconf.c
00db10
===================================================================
00db10
--- glibc-2.17-c758a686.orig/resolv/res_hconf.c
00db10
+++ glibc-2.17-c758a686/resolv/res_hconf.c
00db10
@@ -462,10 +462,10 @@ _res_hconf_reorder_addrs (struct hostent
00db10
 	  errno = save;
00db10
 
00db10
 	  num_ifs = new_num_ifs;
00db10
-
00db10
-	  __libc_lock_unlock (lock);
00db10
 	}
00db10
 
00db10
+      __libc_lock_unlock (lock);
00db10
+
00db10
       __close (sd);
00db10
     }
00db10
 
00db10
Index: glibc-2.17-c758a686/resolv/tst-res_hconf_reorder.c
00db10
===================================================================
00db10
--- /dev/null
00db10
+++ glibc-2.17-c758a686/resolv/tst-res_hconf_reorder.c
00db10
@@ -0,0 +1,112 @@
00db10
+/* BZ #17977 _res_hconf_reorder_addrs test.
00db10
+
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <errno.h>
00db10
+#include <stdio.h>
00db10
+#include <string.h>
00db10
+#include <time.h>
00db10
+#include <dlfcn.h>
00db10
+#include <pthread.h>
00db10
+#include <netdb.h>
00db10
+#include <netinet/in.h>
00db10
+#include <sys/socket.h>
00db10
+
00db10
+static struct timespec ts;
00db10
+
00db10
+/* The first thread that gets a lock in _res_hconf_reorder_addrs()
00db10
+   should hold the lock long enough to make two other threads blocked.
00db10
+   This is achieved by slowing down realloc(3) that is called several times
00db10
+   by _res_hconf_reorder_addrs().  */
00db10
+
00db10
+void *
00db10
+realloc (void *ptr, size_t len)
00db10
+{
00db10
+  static void *(*fun) (void *, size_t);
00db10
+
00db10
+  if (!fun)
00db10
+    fun = dlsym (RTLD_NEXT, "realloc");
00db10
+
00db10
+  if (ts.tv_nsec)
00db10
+    nanosleep (&ts, NULL);
00db10
+
00db10
+  return (*fun) (ptr, len);
00db10
+}
00db10
+
00db10
+static void *
00db10
+resolve (void *arg)
00db10
+{
00db10
+  struct in_addr addr;
00db10
+  struct hostent ent;
00db10
+  struct hostent *result;
00db10
+  int err;
00db10
+  char buf[1024];
00db10
+
00db10
+  addr.s_addr = htonl (INADDR_LOOPBACK);
00db10
+  (void) gethostbyaddr_r ((void *) &addr, sizeof (addr), AF_INET,
00db10
+		          &ent, buf, sizeof (buf), &result, &err;;
00db10
+  return arg;
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  #define N 3
00db10
+  pthread_t thr[N];
00db10
+  unsigned int i;
00db10
+  int result = 0;
00db10
+
00db10
+  /* turn on realloc slowdown */
00db10
+  ts.tv_nsec = 100000000;
00db10
+
00db10
+  for (i = 0; i < N; ++i)
00db10
+    {
00db10
+      int rc = pthread_create (&thr[i], NULL, resolve, NULL);
00db10
+
00db10
+      if (rc)
00db10
+	{
00db10
+	  printf ("pthread_create: %s\n", strerror(rc));
00db10
+	  exit (1);
00db10
+	}
00db10
+    }
00db10
+
00db10
+  for (i = 0; i < N; ++i)
00db10
+    {
00db10
+      void *retval;
00db10
+      int rc = pthread_join (thr[i], &retval);
00db10
+
00db10
+      if (rc)
00db10
+	{
00db10
+	  printf ("pthread_join: %s\n", strerror(rc));
00db10
+	  exit (1);
00db10
+	}
00db10
+      if (retval)
00db10
+	{
00db10
+	  printf ("thread %u exit status %p\n", i, retval);
00db10
+	  result = 1;
00db10
+	}
00db10
+    }
00db10
+
00db10
+  /* turn off realloc slowdown, no longer needed */
00db10
+  ts.tv_nsec = 0;
00db10
+
00db10
+  return result;
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"