[PATCH 3/3] Add tests for bz 32573, 32575

Tobias Stoeckmann tobias@stoeckmann.org
Sun Jan 19 15:05:11 GMT 2025


Check validity of /etc/networks parser.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
 nss/Makefile                |  1 +
 nss/tst-nss-files-network.c | 66 +++++++++++++++++++++++++++++++++++++
 support/namespace.h         |  2 ++
 support/support_chroot.c    |  2 ++
 4 files changed, 71 insertions(+)
 create mode 100644 nss/tst-nss-files-network.c

diff --git a/nss/Makefile b/nss/Makefile
index 3ee51f309e..09ce94aae7 100644
--- a/nss/Makefile
+++ b/nss/Makefile
@@ -367,6 +367,7 @@ tests += tst-nss-files-hosts-multi
 tests += tst-nss-files-hosts-getent
 tests += tst-nss-files-alias-leak
 tests += tst-nss-files-alias-truncated
+tests += tst-nss-files-network
 # tst_fgetgrent currently only works with shared libraries
 test-srcs :=  tst_fgetgrent
 ifeq ($(run-built-tests),yes)
diff --git a/nss/tst-nss-files-network.c b/nss/tst-nss-files-network.c
new file mode 100644
index 0000000000..c298c0b732
--- /dev/null
+++ b/nss/tst-nss-files-network.c
@@ -0,0 +1,66 @@
+/* Check handling of long and truncated numbers in /etc/networks (bug 24059).
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <netdb.h>
+#include <gnu/lib-names.h>
+#include <nss.h>
+#include <stddef.h>
+#include <support/check.h>
+#include <support/check_nss.h>
+#include <support/namespace.h>
+#include <support/test-driver.h>
+#include <support/xdlfcn.h>
+#include <support/xunistd.h>
+
+struct support_chroot *chroot_env;
+
+static void
+prepare (int argc, char **argv)
+{
+  chroot_env = support_chroot_create
+    ((struct support_chroot_configuration)
+     {
+       .networks = "net1 x0000000000Ff.077\n" /* legal 255.63.0.0 */
+        "net2 xFF00000000.0.0.0\n" /* illegal */
+     });
+
+}
+
+static int
+do_test (void)
+{
+  support_become_root ();
+  if (!support_can_chroot ())
+    return EXIT_UNSUPPORTED;
+
+  __nss_configure_lookup ("networks", "files");
+  xdlopen (LIBNSS_FILES_SO, RTLD_NOW);
+
+  xchroot (chroot_env->path_chroot);
+
+  check_netent ("net1", getnetbyname ("net1"),
+    "name: net1\n"
+    "net: 0xff3f0000\n");
+  check_netent ("net2", getnetbyname ("net2"), "error: HOST_NOT_FOUND\n");
+
+  support_chroot_free (chroot_env);
+  return 0;
+}
+
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/support/namespace.h b/support/namespace.h
index 456157a4e0..6b9b226f95 100644
--- a/support/namespace.h
+++ b/support/namespace.h
@@ -75,6 +75,7 @@ struct support_chroot_configuration
   const char *hosts;            /* /etc/hosts.  */
   const char *host_conf;        /* /etc/host.conf.  */
   const char *aliases;          /* /etc/aliases.  */
+  const char *networks;         /* /etc/networks.  */
 };

 /* The result of the creation of a chroot.  */
@@ -92,6 +93,7 @@ struct support_chroot
   char *path_hosts;             /* /etc/hosts.  */
   char *path_host_conf;         /* /etc/host.conf.  */
   char *path_aliases;           /* /etc/aliases.  */
+  char *path_networks;          /* /etc/networks.  */
 };

 /* Create a chroot environment.  The returned data should be freed
diff --git a/support/support_chroot.c b/support/support_chroot.c
index 2637dd1522..92c2a453aa 100644
--- a/support/support_chroot.c
+++ b/support/support_chroot.c
@@ -57,6 +57,7 @@ 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, "networks", conf.networks, &chroot->path_networks);

   free (path_etc);

@@ -79,5 +80,6 @@ support_chroot_free (struct support_chroot *chroot)
   free (chroot->path_hosts);
   free (chroot->path_host_conf);
   free (chroot->path_aliases);
+  free (chroot->path_networks);
   free (chroot);
 }
--
2.48.1



More information about the Libc-alpha mailing list