From patchwork Thu Nov 19 12:28:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Liebler X-Patchwork-Id: 41130 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7530F3958C31; Thu, 19 Nov 2020 12:28:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7530F3958C31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1605788925; bh=mZkv9Z0PV+WO69gYZIubRGkLacQnSUtsOy75BWMMT5M=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=a07lJbrys2WDugY80IqWvc0IvPsDuH3sSx5VfaC8dkDUmPlw6VDqtFc6NG9wxf0BB FBXDCFc6+wNC3ErUlmUGEIRm+kB621oI+joKqNB9vhj4KL1DLnQRQLyZuZEFFqD2pC /3cfX88vuSWzjk8xvwuYvKj3/zR3tacIuKeYzOh4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 5FA8638708C6 for ; Thu, 19 Nov 2020 12:28:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5FA8638708C6 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0AJC1lOv001426 for ; Thu, 19 Nov 2020 07:28:41 -0500 Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 34wg6ee1ex-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 19 Nov 2020 07:28:41 -0500 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 0AJCDEwU029596 for ; Thu, 19 Nov 2020 12:28:39 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma04ams.nl.ibm.com with ESMTP id 34t6v8d097-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 19 Nov 2020 12:28:39 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 0AJCSaiB9241090 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Nov 2020 12:28:36 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4E7D6A405F; Thu, 19 Nov 2020 12:28:36 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 25342A4054; Thu, 19 Nov 2020 12:28:36 +0000 (GMT) Received: from t3560005.lnxne.boe (unknown [9.152.108.100]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 19 Nov 2020 12:28:36 +0000 (GMT) To: libc-alpha@sourceware.org Subject: [PATCH] Use libnss_files.so for tests posix/bug-ga2 and resolv/tst-leaks2 [BZ #26821] Date: Thu, 19 Nov 2020 13:28:29 +0100 Message-Id: <20201119122829.428645-1-stli@linux.ibm.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312, 18.0.737 definitions=2020-11-19_08:2020-11-19, 2020-11-19 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 suspectscore=4 priorityscore=1501 mlxlogscore=999 malwarescore=0 lowpriorityscore=0 impostorscore=0 spamscore=0 bulkscore=0 phishscore=0 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011190087 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Stefan Liebler via Libc-alpha From: Stefan Liebler Reply-To: Stefan Liebler Cc: Stefan Liebler Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" 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 . */ #include #include -#include +#include #include +#include +#include +#include +#include +#include +#include -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 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 #include #include +#include +#include +#include +#include +#include +#include +#include -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 +#define PREPARE prepare +#include 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 #include +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); }