d8307d
commit 99135114ba23c3110b7e4e650fabdc5e639746b7
d8307d
Author: DJ Delorie <dj@redhat.com>
d8307d
Date:   Fri Jun 28 18:30:00 2019 -0500
d8307d
d8307d
    nss_db: fix endent wrt NULL mappings [BZ #24695] [BZ #24696]
d8307d
    
d8307d
    nss_db allows for getpwent et al to be called without a set*ent,
d8307d
    but it only works once.  After the last get*ent a set*ent is
d8307d
    required to restart, because the end*ent did not properly reset
d8307d
    the module.  Resetting it to NULL allows for a proper restart.
d8307d
    
d8307d
    If the database doesn't exist, however, end*ent erroniously called
d8307d
    munmap which set errno.
d8307d
    
d8307d
    The test case runs "makedb" inside the testroot, so needs selinux
d8307d
    DSOs installed.
d8307d
d8307d
diff -rupN a/nss/Makefile b/nss/Makefile
d8307d
--- a/nss/Makefile	2019-11-04 15:14:16.721221038 -0500
d8307d
+++ b/nss/Makefile	2019-11-04 15:15:46.447544678 -0500
d8307d
@@ -60,6 +60,10 @@ tests			= test-netdb test-digits-dots ts
d8307d
 			  tst-nss-test5
d8307d
 xtests			= bug-erange
d8307d
 
d8307d
+tests-container = \
d8307d
+			  tst-nss-db-endpwent \
d8307d
+			  tst-nss-db-endgrent
d8307d
+
d8307d
 # Tests which need libdl
d8307d
 ifeq (yes,$(build-shared))
d8307d
 tests += tst-nss-files-hosts-erange
d8307d
diff -rupN a/nss/nss_db/db-open.c b/nss/nss_db/db-open.c
d8307d
--- a/nss/nss_db/db-open.c	2018-08-01 01:10:47.000000000 -0400
d8307d
+++ b/nss/nss_db/db-open.c	2019-11-04 15:15:10.520213846 -0500
d8307d
@@ -63,5 +63,9 @@ internal_setent (const char *file, struc
d8307d
 void
d8307d
 internal_endent (struct nss_db_map *mapping)
d8307d
 {
d8307d
-  munmap (mapping->header, mapping->len);
d8307d
+  if (mapping->header != NULL)
d8307d
+    {
d8307d
+      munmap (mapping->header, mapping->len);
d8307d
+      mapping->header = NULL;
d8307d
+    }
d8307d
 }
d8307d
diff -rupN a/nss/tst-nss-db-endgrent.c b/nss/tst-nss-db-endgrent.c
d8307d
--- a/nss/tst-nss-db-endgrent.c	1969-12-31 19:00:00.000000000 -0500
d8307d
+++ b/nss/tst-nss-db-endgrent.c	2019-11-04 15:15:10.526214069 -0500
d8307d
@@ -0,0 +1,54 @@
d8307d
+/* Test for endgrent changing errno for BZ #24696
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <stdlib.h>
d8307d
+#include <sys/types.h>
d8307d
+#include <grp.h>
d8307d
+#include <unistd.h>
d8307d
+#include <errno.h>
d8307d
+
d8307d
+#include <support/check.h>
d8307d
+#include <support/support.h>
d8307d
+
d8307d
+/* The following test verifies that if the db NSS Service is initialized
d8307d
+   with no database (getgrent), that a subsequent closure (endgrent) does
d8307d
+   not set errno. In the case of the db service it is not an error to close
d8307d
+   the service and so it should not set errno.  */
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  /* Just make sure it's not there, although usually it won't be.  */
d8307d
+  unlink ("/var/db/group.db");
d8307d
+
d8307d
+  /* This, in conjunction with the testroot's nsswitch.conf, causes
d8307d
+     the nss_db module to be "connected" and initialized - but the
d8307d
+     testroot has no group.db, so no mapping will be created.  */
d8307d
+  getgrent ();
d8307d
+
d8307d
+  errno = 0;
d8307d
+
d8307d
+  /* Before the fix, this would call munmap (NULL) and set errno.  */
d8307d
+  endgrent ();
d8307d
+
d8307d
+  if (errno != 0)
d8307d
+    FAIL_EXIT1 ("endgrent set errno to %d\n", errno);
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+#include <support/test-driver.c>
d8307d
diff -rupN a/nss/tst-nss-db-endgrent.root/etc/nsswitch.conf b/nss/tst-nss-db-endgrent.root/etc/nsswitch.conf
d8307d
--- a/nss/tst-nss-db-endgrent.root/etc/nsswitch.conf	1969-12-31 19:00:00.000000000 -0500
d8307d
+++ b/nss/tst-nss-db-endgrent.root/etc/nsswitch.conf	2019-11-04 15:15:10.539214550 -0500
d8307d
@@ -0,0 +1 @@
d8307d
+group : db files
d8307d
diff -rupN a/nss/tst-nss-db-endpwent.c b/nss/tst-nss-db-endpwent.c
d8307d
--- a/nss/tst-nss-db-endpwent.c	1969-12-31 19:00:00.000000000 -0500
d8307d
+++ b/nss/tst-nss-db-endpwent.c	2019-11-04 15:15:10.545214772 -0500
d8307d
@@ -0,0 +1,66 @@
d8307d
+/* Test for endpwent->getpwent crash for BZ #24695
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library; if not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+#include <stdlib.h>
d8307d
+#include <string.h>
d8307d
+#include <sys/types.h>
d8307d
+#include <pwd.h>
d8307d
+
d8307d
+#include <support/support.h>
d8307d
+#include <support/check.h>
d8307d
+
d8307d
+/* It is entirely allowed to start with a getpwent call without
d8307d
+   resetting the state of the service via a call to setpwent.
d8307d
+   You can also call getpwent more times than you have entries in
d8307d
+   the service, and it should not fail.  This test iteratates the
d8307d
+   database once, gets to the end, and then attempts a second
d8307d
+   iteration to look for crashes.  */
d8307d
+
d8307d
+static void
d8307d
+try_it (void)
d8307d
+{
d8307d
+  struct passwd *pw;
d8307d
+
d8307d
+  /* setpwent is intentionally omitted here.  The first call to
d8307d
+     getpwent detects that it's first and initializes.  The second
d8307d
+     time try_it is called, this "first call" was not detected before
d8307d
+     the fix, and getpwent would crash.  */
d8307d
+
d8307d
+  while ((pw = getpwent ()) != NULL)
d8307d
+    ;
d8307d
+
d8307d
+  /* We only care if this segfaults or not.  */
d8307d
+  endpwent ();
d8307d
+}
d8307d
+
d8307d
+static int
d8307d
+do_test (void)
d8307d
+{
d8307d
+  char *cmd;
d8307d
+
d8307d
+  cmd = xasprintf ("%s/makedb -o /var/db/passwd.db /var/db/passwd.in",
d8307d
+		   support_bindir_prefix);
d8307d
+  system (cmd);
d8307d
+  free (cmd);
d8307d
+
d8307d
+  try_it ();
d8307d
+  try_it ();
d8307d
+
d8307d
+  return 0;
d8307d
+}
d8307d
+#include <support/test-driver.c>
d8307d
diff -rupN a/nss/tst-nss-db-endpwent.root/etc/nsswitch.conf b/nss/tst-nss-db-endpwent.root/etc/nsswitch.conf
d8307d
--- a/nss/tst-nss-db-endpwent.root/etc/nsswitch.conf	1969-12-31 19:00:00.000000000 -0500
d8307d
+++ b/nss/tst-nss-db-endpwent.root/etc/nsswitch.conf	2019-11-04 15:15:10.556215180 -0500
d8307d
@@ -0,0 +1 @@
d8307d
+passwd: db
d8307d
diff -rupN a/nss/tst-nss-db-endpwent.root/var/db/passwd.in b/nss/tst-nss-db-endpwent.root/var/db/passwd.in
d8307d
--- a/nss/tst-nss-db-endpwent.root/var/db/passwd.in	1969-12-31 19:00:00.000000000 -0500
d8307d
+++ b/nss/tst-nss-db-endpwent.root/var/db/passwd.in	2019-11-04 15:15:10.567215588 -0500
d8307d
@@ -0,0 +1,4 @@
d8307d
+.root root:x:0:0:root:/root:/bin/bash
d8307d
+=0 root:x:0:0:root:/root:/bin/bash
d8307d
+.bin bin:x:1:1:bin:/bin:/sbin/nologin
d8307d
+=1 bin:x:1:1:bin:/bin:/sbin/nologin
d8307d
diff -rupN a/support/Makefile b/support/Makefile
d8307d
--- a/support/Makefile	2019-11-04 15:14:20.416357911 -0500
d8307d
+++ b/support/Makefile	2019-11-04 15:15:10.574215847 -0500
d8307d
@@ -180,6 +180,11 @@ LINKS_DSO_PROGRAM = links-dso-program
d8307d
 LDLIBS-links-dso-program = -lstdc++ -lgcc -lgcc_s $(libunwind)
d8307d
 endif
d8307d
 
d8307d
+ifeq (yes,$(have-selinux))
d8307d
+LDLIBS-$(LINKS_DSO_PROGRAM) += -lselinux
d8307d
+endif
d8307d
+
d8307d
+
d8307d
 LDLIBS-test-container = $(libsupport)
d8307d
 
d8307d
 others += test-container
d8307d
diff -rupN a/support/links-dso-program-c.c b/support/links-dso-program-c.c
d8307d
--- a/support/links-dso-program-c.c	2019-11-04 15:14:17.073234077 -0500
d8307d
+++ b/support/links-dso-program-c.c	2019-11-04 15:15:10.580216069 -0500
d8307d
@@ -1,9 +1,26 @@
d8307d
 #include <stdio.h>
d8307d
 
d8307d
+/* makedb needs selinux dso's.  */
d8307d
+#ifdef HAVE_SELINUX
d8307d
+# include <selinux/selinux.h>
d8307d
+#endif
d8307d
+
d8307d
+/* The purpose of this file is to indicate to the build system which
d8307d
+   shared objects need to be copied into the testroot, such as gcc or
d8307d
+   selinux support libraries.  This program is never executed, only
d8307d
+   scanned for dependencies on shared objects, so the code below may
d8307d
+   seem weird - it's written to survive gcc optimization and force
d8307d
+   such dependencies.
d8307d
+*/
d8307d
+
d8307d
 int
d8307d
 main (int argc, char **argv)
d8307d
 {
d8307d
   /* Complexity to keep gcc from optimizing this away.  */
d8307d
   printf ("This is a test %s.\n", argc > 1 ? argv[1] : "null");
d8307d
+#ifdef HAVE_SELINUX
d8307d
+  /* This exists to force libselinux.so to be required.  */
d8307d
+  printf ("selinux %d\n", is_selinux_enabled ());
d8307d
+#endif
d8307d
   return 0;
d8307d
 }
d8307d
diff -rupN a/support/links-dso-program.cc b/support/links-dso-program.cc
d8307d
--- a/support/links-dso-program.cc	2019-11-04 15:14:17.079234300 -0500
d8307d
+++ b/support/links-dso-program.cc	2019-11-04 15:15:10.587216328 -0500
d8307d
@@ -1,11 +1,28 @@
d8307d
 #include <iostream>
d8307d
 
d8307d
+/* makedb needs selinux dso's.  */
d8307d
+#ifdef HAVE_SELINUX
d8307d
+# include <selinux/selinux.h>
d8307d
+#endif
d8307d
+
d8307d
 using namespace std;
d8307d
 
d8307d
+/* The purpose of this file is to indicate to the build system which
d8307d
+   shared objects need to be copied into the testroot, such as gcc or
d8307d
+   selinux support libraries.  This program is never executed, only
d8307d
+   scanned for dependencies on shared objects, so the code below may
d8307d
+   seem weird - it's written to survive gcc optimization and force
d8307d
+   such dependencies.
d8307d
+*/
d8307d
+
d8307d
 int
d8307d
 main (int argc, char **argv)
d8307d
 {
d8307d
   /* Complexity to keep gcc from optimizing this away.  */
d8307d
   cout << (argc > 1 ? argv[1] : "null");
d8307d
+#ifdef HAVE_SELINUX
d8307d
+  /* This exists to force libselinux.so to be required.  */
d8307d
+  cout << "selinux " << is_selinux_enabled ();
d8307d
+#endif
d8307d
   return 0;
d8307d
 }