diff mbox series

Use libnss_files.so for tests posix/bug-ga2 and resolv/tst-leaks2 [BZ #26821]

Message ID 20201119122829.428645-1-stli@linux.ibm.com
State New
Headers show
Series Use libnss_files.so for tests posix/bug-ga2 and resolv/tst-leaks2 [BZ #26821] | expand

Commit Message

Stefan Liebler Nov. 19, 2020, 12:28 p.m. UTC
The tests posix/bug-ga2-mem and resolv/mtrace-tst-leaks2 are failing on
fedora 33 as mtrace reports memory leaks.

The /etc/nsswitch.conf differs between
Fedora 32: hosts:      files dns myhostname
Fedora 33: hosts:      files resolve [!UNAVAIL=return] myhostname dns

Therefore /lib64/libnss_resolve.so.2 (from systemd) and the dependencies
libgcc_s.so.1 and libpthread.so.0 are loaded.

Usually all malloc'ed resources from getaddrinfo / gethostbyname are freed
and the libraries are dlclose'd in nss/nsswitch.c:libc_freeres_fn (free_mem).
Unfortunately, /lib64/libnss_resolve.so.2 is marked with DF_1_NODELETE.
As this library is not unmapped, you'll see "Memory not freed".

Therefore those tests are now only rely on libnss_files.so by preparing
a chroot with all required configuration files and executing the test in
a subprocess.

This patch also added support for /etc/services and /etc/nsswitch.conf
in struct support_chroot_configuration.

Note:
We can't use __nss_configure_lookup as it would lead to not setting up
nsswitch.c:service_table before calling __getservbyname_r / __gethostbyname_r.
nsswitch.c:nss_load_library would then add resources to default_table which
are not freed by nsswitch.c:libc_freeres_fn (free_mem)
---
 posix/Makefile           |  1 +
 posix/bug-ga2.c          | 68 +++++++++++++++++++++++++++++++++++-----
 resolv/Makefile          |  1 +
 resolv/tst-leaks2.c      | 63 +++++++++++++++++++++++++++++++++----
 support/namespace.h      |  4 +++
 support/support_chroot.c |  9 +++++-
 6 files changed, 132 insertions(+), 14 deletions(-)

Comments

H.J. Lu Nov. 19, 2020, 3:11 p.m. UTC | #1
On Thu, Nov 19, 2020 at 4:32 AM Stefan Liebler via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> The tests posix/bug-ga2-mem and resolv/mtrace-tst-leaks2 are failing on
> fedora 33 as mtrace reports memory leaks.
>
> The /etc/nsswitch.conf differs between
> Fedora 32: hosts:      files dns myhostname
> Fedora 33: hosts:      files resolve [!UNAVAIL=return] myhostname dns
>
> Therefore /lib64/libnss_resolve.so.2 (from systemd) and the dependencies
> libgcc_s.so.1 and libpthread.so.0 are loaded.
>
> Usually all malloc'ed resources from getaddrinfo / gethostbyname are freed
> and the libraries are dlclose'd in nss/nsswitch.c:libc_freeres_fn (free_mem).
> Unfortunately, /lib64/libnss_resolve.so.2 is marked with DF_1_NODELETE.
> As this library is not unmapped, you'll see "Memory not freed".
>
> Therefore those tests are now only rely on libnss_files.so by preparing
> a chroot with all required configuration files and executing the test in
> a subprocess.
>
> This patch also added support for /etc/services and /etc/nsswitch.conf
> in struct support_chroot_configuration.
>
> Note:
> We can't use __nss_configure_lookup as it would lead to not setting up
> nsswitch.c:service_table before calling __getservbyname_r / __gethostbyname_r.
> nsswitch.c:nss_load_library would then add resources to default_table which
> are not freed by nsswitch.c:libc_freeres_fn (free_mem)
> ---
>  posix/Makefile           |  1 +
>  posix/bug-ga2.c          | 68 +++++++++++++++++++++++++++++++++++-----
>  resolv/Makefile          |  1 +
>  resolv/tst-leaks2.c      | 63 +++++++++++++++++++++++++++++++++----
>  support/namespace.h      |  4 +++
>  support/support_chroot.c |  9 +++++-
>  6 files changed, 132 insertions(+), 14 deletions(-)
>
> diff --git a/posix/Makefile b/posix/Makefile
> index 693082ed28..0ef4e7f4be 100644
> --- a/posix/Makefile
> +++ b/posix/Makefile
> @@ -361,6 +361,7 @@ $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out
>         $(common-objpfx)malloc/mtrace $(objpfx)bug-ga2.mtrace > $@; \
>         $(evaluate-test)
>
> +$(objpfx)bug-ga2: $(libdl)
>  bug-ga2-ENV = MALLOC_TRACE=$(objpfx)bug-ga2.mtrace
>
>  bug-glob2-ENV = MALLOC_TRACE=$(objpfx)bug-glob2.mtrace
> diff --git a/posix/bug-ga2.c b/posix/bug-ga2.c
> index 5ea759b8ce..889f100358 100644
> --- a/posix/bug-ga2.c
> +++ b/posix/bug-ga2.c
> @@ -1,16 +1,46 @@
>  /* Test case by Sam Varshavchik <mrsam@courier-mta.com>.  */
>  #include <mcheck.h>
>  #include <netdb.h>
> -#include <stdio.h>
> +#include <stdlib.h>
>  #include <string.h>
> +#include <dlfcn.h>
> +#include <gnu/lib-names.h>
> +#include <support/check.h>
> +#include <support/namespace.h>
> +#include <support/test-driver.h>
> +#include <support/xunistd.h>
>
> -int
> -main (void)
> +struct support_chroot *chroot_env;
> +
> +static void
> +prepare (int argc, char **argv)
> +{
> +  /* Generate a /etc/nsswitch.conf instead of using __nss_configure_lookup
> +     as it would lead to not setting up nsswitch.c:service_table before calling
> +     __getservbyname_r.  nss_load_library would then add resources to
> +     default_table which are not freed by
> +     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
> +  chroot_env = support_chroot_create
> +    ((struct support_chroot_configuration)
> +     {
> +       .nsswitch_conf =
> +       "services: files\n"
> +       "hosts: files\n",
> +       .host_conf = "multi on\n",
> +       .hosts = "192.0.2.1 www.gnu.org\n",
> +       .services = "http 80/tcp\n"
> +     });
> +}
> +
> +static void
> +subprocess_test (void *closure)
>  {
>    struct addrinfo hints, *res;
>    int i, ret;
>
>    mtrace ();
> +  xchroot (chroot_env->path_chroot);
> +
>    for (i = 0; i < 100; i++)
>      {
>        memset (&hints, 0, sizeof (hints));
> @@ -20,11 +50,35 @@ main (void)
>        ret = getaddrinfo ("www.gnu.org", "http", &hints, &res);
>
>        if (ret)
> -       {
> -         printf ("%s\n", gai_strerror (ret));
> -         return 1;
> -       }
> +       FAIL_EXIT1 ("%s\n", gai_strerror (ret));
> +
>        freeaddrinfo (res);
>      }
> +
> +  /* Ensure that the resources malloc'ed by getaddrinfo are freed by
> +     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
> +  exit (EXIT_SUCCESS);
> +}
> +
> +
> +static int
> +do_test (void)
> +{
> +  support_become_root ();
> +  if (!support_can_chroot ())
> +    return EXIT_UNSUPPORTED;
> +
> +  /* Load the libraries now as those are not available in chroot.  */
> +  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
> +    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
> +

Can you copy the needed shared libraries to chroot?
Carlos O'Donell Nov. 19, 2020, 3:34 p.m. UTC | #2
On 11/19/20 10:11 AM, H.J. Lu via Libc-alpha wrote:
> On Thu, Nov 19, 2020 at 4:32 AM Stefan Liebler via Libc-alpha
> <libc-alpha@sourceware.org> wrote:
>>
>> The tests posix/bug-ga2-mem and resolv/mtrace-tst-leaks2 are failing on
>> fedora 33 as mtrace reports memory leaks.
>>
>> The /etc/nsswitch.conf differs between
>> Fedora 32: hosts:      files dns myhostname
>> Fedora 33: hosts:      files resolve [!UNAVAIL=return] myhostname dns
>>
>> Therefore /lib64/libnss_resolve.so.2 (from systemd) and the dependencies
>> libgcc_s.so.1 and libpthread.so.0 are loaded.
>>
>> Usually all malloc'ed resources from getaddrinfo / gethostbyname are freed
>> and the libraries are dlclose'd in nss/nsswitch.c:libc_freeres_fn (free_mem).
>> Unfortunately, /lib64/libnss_resolve.so.2 is marked with DF_1_NODELETE.
>> As this library is not unmapped, you'll see "Memory not freed".
>>
>> Therefore those tests are now only rely on libnss_files.so by preparing
>> a chroot with all required configuration files and executing the test in
>> a subprocess.
>>
>> This patch also added support for /etc/services and /etc/nsswitch.conf
>> in struct support_chroot_configuration.
>>
>> Note:
>> We can't use __nss_configure_lookup as it would lead to not setting up
>> nsswitch.c:service_table before calling __getservbyname_r / __gethostbyname_r.
>> nsswitch.c:nss_load_library would then add resources to default_table which
>> are not freed by nsswitch.c:libc_freeres_fn (free_mem)
>> ---
>>  posix/Makefile           |  1 +
>>  posix/bug-ga2.c          | 68 +++++++++++++++++++++++++++++++++++-----
>>  resolv/Makefile          |  1 +
>>  resolv/tst-leaks2.c      | 63 +++++++++++++++++++++++++++++++++----
>>  support/namespace.h      |  4 +++
>>  support/support_chroot.c |  9 +++++-
>>  6 files changed, 132 insertions(+), 14 deletions(-)
>>
>> diff --git a/posix/Makefile b/posix/Makefile
>> index 693082ed28..0ef4e7f4be 100644
>> --- a/posix/Makefile
>> +++ b/posix/Makefile
>> @@ -361,6 +361,7 @@ $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out
>>         $(common-objpfx)malloc/mtrace $(objpfx)bug-ga2.mtrace > $@; \
>>         $(evaluate-test)
>>
>> +$(objpfx)bug-ga2: $(libdl)
>>  bug-ga2-ENV = MALLOC_TRACE=$(objpfx)bug-ga2.mtrace
>>
>>  bug-glob2-ENV = MALLOC_TRACE=$(objpfx)bug-glob2.mtrace
>> diff --git a/posix/bug-ga2.c b/posix/bug-ga2.c
>> index 5ea759b8ce..889f100358 100644
>> --- a/posix/bug-ga2.c
>> +++ b/posix/bug-ga2.c
>> @@ -1,16 +1,46 @@
>>  /* Test case by Sam Varshavchik <mrsam@courier-mta.com>.  */
>>  #include <mcheck.h>
>>  #include <netdb.h>
>> -#include <stdio.h>
>> +#include <stdlib.h>
>>  #include <string.h>
>> +#include <dlfcn.h>
>> +#include <gnu/lib-names.h>
>> +#include <support/check.h>
>> +#include <support/namespace.h>
>> +#include <support/test-driver.h>
>> +#include <support/xunistd.h>
>>
>> -int
>> -main (void)
>> +struct support_chroot *chroot_env;
>> +
>> +static void
>> +prepare (int argc, char **argv)
>> +{
>> +  /* Generate a /etc/nsswitch.conf instead of using __nss_configure_lookup
>> +     as it would lead to not setting up nsswitch.c:service_table before calling
>> +     __getservbyname_r.  nss_load_library would then add resources to
>> +     default_table which are not freed by
>> +     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
>> +  chroot_env = support_chroot_create
>> +    ((struct support_chroot_configuration)
>> +     {
>> +       .nsswitch_conf =
>> +       "services: files\n"
>> +       "hosts: files\n",
>> +       .host_conf = "multi on\n",
>> +       .hosts = "192.0.2.1 www.gnu.org\n",
>> +       .services = "http 80/tcp\n"
>> +     });
>> +}
>> +
>> +static void
>> +subprocess_test (void *closure)
>>  {
>>    struct addrinfo hints, *res;
>>    int i, ret;
>>
>>    mtrace ();
>> +  xchroot (chroot_env->path_chroot);
>> +
>>    for (i = 0; i < 100; i++)
>>      {
>>        memset (&hints, 0, sizeof (hints));
>> @@ -20,11 +50,35 @@ main (void)
>>        ret = getaddrinfo ("www.gnu.org", "http", &hints, &res);
>>
>>        if (ret)
>> -       {
>> -         printf ("%s\n", gai_strerror (ret));
>> -         return 1;
>> -       }
>> +       FAIL_EXIT1 ("%s\n", gai_strerror (ret));
>> +
>>        freeaddrinfo (res);
>>      }
>> +
>> +  /* Ensure that the resources malloc'ed by getaddrinfo are freed by
>> +     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
>> +  exit (EXIT_SUCCESS);
>> +}
>> +
>> +
>> +static int
>> +do_test (void)
>> +{
>> +  support_become_root ();
>> +  if (!support_can_chroot ())
>> +    return EXIT_UNSUPPORTED;
>> +
>> +  /* Load the libraries now as those are not available in chroot.  */
>> +  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
>> +    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
>> +
> 
> Can you copy the needed shared libraries to chroot?
 
You can make it a test-container test which has the installed
glibc in the sysroot, and provide a nsswitch.conf for the test that
does exactly what you want.

Examples:
./nss/tst-nss-db-endgrent.root/etc/nsswitch.conf
./nss/tst-nss-db-endpwent.root/etc/nsswitch.conf
./nss/tst-nss-test3.root/etc/nsswitch.conf
Stefan Liebler Nov. 20, 2020, 12:10 p.m. UTC | #3
On 11/19/20 4:34 PM, Carlos O'Donell wrote:
> On 11/19/20 10:11 AM, H.J. Lu via Libc-alpha wrote:
>> On Thu, Nov 19, 2020 at 4:32 AM Stefan Liebler via Libc-alpha
>> <libc-alpha@sourceware.org> wrote:
>>>
>>> The tests posix/bug-ga2-mem and resolv/mtrace-tst-leaks2 are failing on
>>> fedora 33 as mtrace reports memory leaks.
>>>
>>> The /etc/nsswitch.conf differs between
>>> Fedora 32: hosts:      files dns myhostname
>>> Fedora 33: hosts:      files resolve [!UNAVAIL=return] myhostname dns
>>>
>>> Therefore /lib64/libnss_resolve.so.2 (from systemd) and the dependencies
>>> libgcc_s.so.1 and libpthread.so.0 are loaded.
>>>
>>> Usually all malloc'ed resources from getaddrinfo / gethostbyname are freed
>>> and the libraries are dlclose'd in nss/nsswitch.c:libc_freeres_fn (free_mem).
>>> Unfortunately, /lib64/libnss_resolve.so.2 is marked with DF_1_NODELETE.
>>> As this library is not unmapped, you'll see "Memory not freed".
>>>
>>> Therefore those tests are now only rely on libnss_files.so by preparing
>>> a chroot with all required configuration files and executing the test in
>>> a subprocess.
>>>
>>> This patch also added support for /etc/services and /etc/nsswitch.conf
>>> in struct support_chroot_configuration.
>>>
>>> Note:
>>> We can't use __nss_configure_lookup as it would lead to not setting up
>>> nsswitch.c:service_table before calling __getservbyname_r / __gethostbyname_r.
>>> nsswitch.c:nss_load_library would then add resources to default_table which
>>> are not freed by nsswitch.c:libc_freeres_fn (free_mem)
>>> ---
>>>  posix/Makefile           |  1 +
>>>  posix/bug-ga2.c          | 68 +++++++++++++++++++++++++++++++++++-----
>>>  resolv/Makefile          |  1 +
>>>  resolv/tst-leaks2.c      | 63 +++++++++++++++++++++++++++++++++----
>>>  support/namespace.h      |  4 +++
>>>  support/support_chroot.c |  9 +++++-
>>>  6 files changed, 132 insertions(+), 14 deletions(-)
>>>
>>> diff --git a/posix/Makefile b/posix/Makefile
>>> index 693082ed28..0ef4e7f4be 100644
>>> --- a/posix/Makefile
>>> +++ b/posix/Makefile
>>> @@ -361,6 +361,7 @@ $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out
>>>         $(common-objpfx)malloc/mtrace $(objpfx)bug-ga2.mtrace > $@; \
>>>         $(evaluate-test)
>>>
>>> +$(objpfx)bug-ga2: $(libdl)
>>>  bug-ga2-ENV = MALLOC_TRACE=$(objpfx)bug-ga2.mtrace
>>>
>>>  bug-glob2-ENV = MALLOC_TRACE=$(objpfx)bug-glob2.mtrace
>>> diff --git a/posix/bug-ga2.c b/posix/bug-ga2.c
>>> index 5ea759b8ce..889f100358 100644
>>> --- a/posix/bug-ga2.c
>>> +++ b/posix/bug-ga2.c
>>> @@ -1,16 +1,46 @@
>>>  /* Test case by Sam Varshavchik <mrsam@courier-mta.com>.  */
>>>  #include <mcheck.h>
>>>  #include <netdb.h>
>>> -#include <stdio.h>
>>> +#include <stdlib.h>
>>>  #include <string.h>
>>> +#include <dlfcn.h>
>>> +#include <gnu/lib-names.h>
>>> +#include <support/check.h>
>>> +#include <support/namespace.h>
>>> +#include <support/test-driver.h>
>>> +#include <support/xunistd.h>
>>>
>>> -int
>>> -main (void)
>>> +struct support_chroot *chroot_env;
>>> +
>>> +static void
>>> +prepare (int argc, char **argv)
>>> +{
>>> +  /* Generate a /etc/nsswitch.conf instead of using __nss_configure_lookup
>>> +     as it would lead to not setting up nsswitch.c:service_table before calling
>>> +     __getservbyname_r.  nss_load_library would then add resources to
>>> +     default_table which are not freed by
>>> +     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
>>> +  chroot_env = support_chroot_create
>>> +    ((struct support_chroot_configuration)
>>> +     {
>>> +       .nsswitch_conf =
>>> +       "services: files\n"
>>> +       "hosts: files\n",
>>> +       .host_conf = "multi on\n",
>>> +       .hosts = "192.0.2.1 www.gnu.org\n",
>>> +       .services = "http 80/tcp\n"
>>> +     });
>>> +}
>>> +
>>> +static void
>>> +subprocess_test (void *closure)
>>>  {
>>>    struct addrinfo hints, *res;
>>>    int i, ret;
>>>
>>>    mtrace ();
>>> +  xchroot (chroot_env->path_chroot);
>>> +
>>>    for (i = 0; i < 100; i++)
>>>      {
>>>        memset (&hints, 0, sizeof (hints));
>>> @@ -20,11 +50,35 @@ main (void)
>>>        ret = getaddrinfo ("www.gnu.org", "http", &hints, &res);
>>>
>>>        if (ret)
>>> -       {
>>> -         printf ("%s\n", gai_strerror (ret));
>>> -         return 1;
>>> -       }
>>> +       FAIL_EXIT1 ("%s\n", gai_strerror (ret));
>>> +
>>>        freeaddrinfo (res);
>>>      }
>>> +
>>> +  /* Ensure that the resources malloc'ed by getaddrinfo are freed by
>>> +     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
>>> +  exit (EXIT_SUCCESS);
>>> +}
>>> +
>>> +
>>> +static int
>>> +do_test (void)
>>> +{
>>> +  support_become_root ();
>>> +  if (!support_can_chroot ())
>>> +    return EXIT_UNSUPPORTED;
>>> +
>>> +  /* Load the libraries now as those are not available in chroot.  */
>>> +  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
>>> +    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
>>> +
>>
>> Can you copy the needed shared libraries to chroot?
>  
> You can make it a test-container test which has the installed
> glibc in the sysroot, and provide a nsswitch.conf for the test that
> does exactly what you want.
> 
> Examples:
> ./nss/tst-nss-db-endgrent.root/etc/nsswitch.conf
> ./nss/tst-nss-db-endpwent.root/etc/nsswitch.conf
> ./nss/tst-nss-test3.root/etc/nsswitch.conf
> 
> 

Sure. The test is now a container test and libnss_files is loaded as
usual. I've just send a new patch (unfortunately I've missed to add "-v2"):
"[PATCH] Use libnss_files.so for tests posix/bug-ga2 and
resolv/tst-leaks2 [BZ #26821]"
https://sourceware.org/pipermail/libc-alpha/2020-November/119860.html

Bye,
Stefan
diff mbox series

Patch

diff --git a/posix/Makefile b/posix/Makefile
index 693082ed28..0ef4e7f4be 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -361,6 +361,7 @@  $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out
 	$(common-objpfx)malloc/mtrace $(objpfx)bug-ga2.mtrace > $@; \
 	$(evaluate-test)
 
+$(objpfx)bug-ga2: $(libdl)
 bug-ga2-ENV = MALLOC_TRACE=$(objpfx)bug-ga2.mtrace
 
 bug-glob2-ENV = MALLOC_TRACE=$(objpfx)bug-glob2.mtrace
diff --git a/posix/bug-ga2.c b/posix/bug-ga2.c
index 5ea759b8ce..889f100358 100644
--- a/posix/bug-ga2.c
+++ b/posix/bug-ga2.c
@@ -1,16 +1,46 @@ 
 /* Test case by Sam Varshavchik <mrsam@courier-mta.com>.  */
 #include <mcheck.h>
 #include <netdb.h>
-#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
 
-int
-main (void)
+struct support_chroot *chroot_env;
+
+static void
+prepare (int argc, char **argv)
+{
+  /* Generate a /etc/nsswitch.conf instead of using __nss_configure_lookup
+     as it would lead to not setting up nsswitch.c:service_table before calling
+     __getservbyname_r.  nss_load_library would then add resources to
+     default_table which are not freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  chroot_env = support_chroot_create
+    ((struct support_chroot_configuration)
+     {
+       .nsswitch_conf =
+	"services: files\n"
+	"hosts: files\n",
+       .host_conf = "multi on\n",
+       .hosts = "192.0.2.1 www.gnu.org\n",
+       .services = "http 80/tcp\n"
+     });
+}
+
+static void
+subprocess_test (void *closure)
 {
   struct addrinfo hints, *res;
   int i, ret;
 
   mtrace ();
+  xchroot (chroot_env->path_chroot);
+
   for (i = 0; i < 100; i++)
     {
       memset (&hints, 0, sizeof (hints));
@@ -20,11 +50,35 @@  main (void)
       ret = getaddrinfo ("www.gnu.org", "http", &hints, &res);
 
       if (ret)
-	{
-	  printf ("%s\n", gai_strerror (ret));
-	  return 1;
-	}
+	FAIL_EXIT1 ("%s\n", gai_strerror (ret));
+
       freeaddrinfo (res);
     }
+
+  /* Ensure that the resources malloc'ed by getaddrinfo are freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  exit (EXIT_SUCCESS);
+}
+
+
+static int
+do_test (void)
+{
+  support_become_root ();
+  if (!support_can_chroot ())
+    return EXIT_UNSUPPORTED;
+
+  /* Load the libraries now as those are not available in chroot.  */
+  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
+    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
+
+  /* Run the chroot'ing test in a subprocess in order to be able to delete the
+     temporary files.  */
+  support_isolate_in_subprocess (subprocess_test, NULL);
+
+  support_chroot_free (chroot_env);
   return 0;
 }
+
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/resolv/Makefile b/resolv/Makefile
index dbd8f8bf4f..533d36eebf 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -157,6 +157,7 @@  $(objpfx)mtrace-tst-leaks.out: $(objpfx)tst-leaks.out
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks.mtrace > $@; \
 	$(evaluate-test)
 
+$(objpfx)tst-leaks2: $(libdl)
 tst-leaks2-ENV = MALLOC_TRACE=$(objpfx)tst-leaks2.mtrace
 $(objpfx)mtrace-tst-leaks2.out: $(objpfx)tst-leaks2.out
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks2.mtrace > $@; \
diff --git a/resolv/tst-leaks2.c b/resolv/tst-leaks2.c
index a2f5db3adc..8f7a807572 100644
--- a/resolv/tst-leaks2.c
+++ b/resolv/tst-leaks2.c
@@ -21,20 +21,71 @@ 
 #include <mcheck.h>
 #include <netdb.h>
 #include <resolv.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
 
-static int
-do_test (void)
+struct support_chroot *chroot_env;
+
+static void
+prepare (int argc, char **argv)
+{
+  /* Generate a /etc/nsswitch.conf file instead of using __nss_configure_lookup
+     as it would lead to not setting up nsswitch.c:service_table before calling
+     __gethostbyname_r. nss_load_library would then add resources to
+     default_table which are not freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem)  */
+  chroot_env = support_chroot_create
+    ((struct support_chroot_configuration)
+     {
+       .nsswitch_conf = "hosts: files\n",
+       .host_conf = "multi on\n",
+       .hosts = "192.0.2.1 www.gnu.org\n",
+     });
+}
+
+static void
+subprocess_test (void *closure)
 {
   mtrace ();
+  xchroot (chroot_env->path_chroot);
+
   for (int i = 0; i < 20; ++i)
     {
       res_init ();
-      gethostbyname ("www.gnu.org");
+      if (gethostbyname ("www.gnu.org") == NULL)
+	FAIL_EXIT1 ("%s\n", hstrerror (h_errno));
     }
+
+  /* Ensure that the resources malloc'ed by gethostbyname are freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  exit (EXIT_SUCCESS);
+}
+
+
+static int
+do_test (void)
+{
+  support_become_root ();
+  if (!support_can_chroot ())
+    return EXIT_UNSUPPORTED;
+
+  /* Load the libraries now as those are not available in chroot.  */
+  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
+    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
+
+  /* Run the chroot'ing test in a subprocess in order to be able to delete the
+     temporary files.  */
+  support_isolate_in_subprocess (subprocess_test, NULL);
+
+  support_chroot_free (chroot_env);
   return 0;
 }
 
-#define TEST_FUNCTION do_test ()
 #define TIMEOUT 30
-/* This defines the `main' function and some more.  */
-#include <test-skeleton.c>
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/support/namespace.h b/support/namespace.h
index 5210343877..1c74dcaa4b 100644
--- a/support/namespace.h
+++ b/support/namespace.h
@@ -75,6 +75,8 @@  struct support_chroot_configuration
   const char *hosts;            /* /etc/hosts.  */
   const char *host_conf;        /* /etc/host.conf.  */
   const char *aliases;          /* /etc/aliases.  */
+  const char *services;         /* /etc/services.  */
+  const char *nsswitch_conf;    /* /etc/nsswitch.conf.  */
 };
 
 /* The result of the creation of a chroot.  */
@@ -92,6 +94,8 @@  struct support_chroot
   char *path_hosts;             /* /etc/hosts.  */
   char *path_host_conf;         /* /etc/host.conf.  */
   char *path_aliases;           /* /etc/aliases.  */
+  char *path_services;          /* /etc/services.  */
+  char *path_nsswitch_conf;     /* /etc/nsswitch.conf.  */
 };
 
 /* Create a chroot environment.  The returned data should be freed
diff --git a/support/support_chroot.c b/support/support_chroot.c
index 4f435c1ac1..e6cabd30c5 100644
--- a/support/support_chroot.c
+++ b/support/support_chroot.c
@@ -24,6 +24,8 @@ 
 #include <support/test-driver.h>
 #include <support/xunistd.h>
 
+extern const char *__progname;
+
 /* If CONTENTS is not NULL, write it to the file at DIRECTORY/RELPATH,
    and store the name in *ABSPATH.  If CONTENTS is NULL, store NULL in
    *ABSPATH.  */
@@ -45,7 +47,7 @@  struct support_chroot *
 support_chroot_create (struct support_chroot_configuration conf)
 {
   struct support_chroot *chroot = xmalloc (sizeof (*chroot));
-  chroot->path_chroot = support_create_temp_directory ("tst-resolv-res_init-");
+  chroot->path_chroot = support_create_temp_directory (__progname);
 
   /* Create the /etc directory in the chroot environment.  */
   char *path_etc = xasprintf ("%s/etc", chroot->path_chroot);
@@ -57,6 +59,9 @@  support_chroot_create (struct support_chroot_configuration conf)
   write_file (path_etc, "hosts", conf.hosts, &chroot->path_hosts);
   write_file (path_etc, "host.conf", conf.host_conf, &chroot->path_host_conf);
   write_file (path_etc, "aliases", conf.aliases, &chroot->path_aliases);
+  write_file (path_etc, "services", conf.services, &chroot->path_services);
+  write_file (path_etc, "nsswitch.conf", conf.nsswitch_conf,
+	      &chroot->path_nsswitch_conf);
 
   free (path_etc);
 
@@ -79,5 +84,7 @@  support_chroot_free (struct support_chroot *chroot)
   free (chroot->path_hosts);
   free (chroot->path_host_conf);
   free (chroot->path_aliases);
+  free (chroot->path_services);
+  free (chroot->path_nsswitch_conf);
   free (chroot);
 }