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