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