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