50f89d
commit 2f498f3d140ab5152bd784df2be7af7d9c5e63ed
50f89d
Author: Florian Weimer <fweimer@redhat.com>
50f89d
Date:   Tue Aug 14 10:57:48 2018 +0200
50f89d
50f89d
    nss_files: Fix file stream leak in aliases lookup [BZ #23521]
50f89d
    
50f89d
    In order to get a clean test case, it was necessary to fix partially
50f89d
    fixed bug 23522 as well.
50f89d
    
50f89d
    (cherry picked from commit e95c6f61920a0f9237cfb292fa44ad500e1df09b)
50f89d
50f89d
diff --git a/nss/Makefile b/nss/Makefile
50f89d
index 66fac7f5b8a4c0d8..5209fc0456dd6786 100644
50f89d
--- a/nss/Makefile
50f89d
+++ b/nss/Makefile
50f89d
@@ -65,6 +65,7 @@ ifeq (yes,$(build-shared))
50f89d
 tests += tst-nss-files-hosts-erange
50f89d
 tests += tst-nss-files-hosts-multi
50f89d
 tests += tst-nss-files-hosts-getent
50f89d
+tests += tst-nss-files-alias-leak
50f89d
 endif
50f89d
 
50f89d
 # If we have a thread library then we can test cancellation against
50f89d
@@ -171,3 +172,5 @@ endif
50f89d
 $(objpfx)tst-nss-files-hosts-erange: $(libdl)
50f89d
 $(objpfx)tst-nss-files-hosts-multi: $(libdl)
50f89d
 $(objpfx)tst-nss-files-hosts-getent: $(libdl)
50f89d
+$(objpfx)tst-nss-files-alias-leak: $(libdl)
50f89d
+$(objpfx)tst-nss-files-alias-leak.out: $(objpfx)/libnss_files.so
50f89d
diff --git a/nss/nss_files/files-alias.c b/nss/nss_files/files-alias.c
50f89d
index cfd34b66b921bbff..35b0bfc5d2479ab6 100644
50f89d
--- a/nss/nss_files/files-alias.c
50f89d
+++ b/nss/nss_files/files-alias.c
50f89d
@@ -221,6 +221,13 @@ get_next_alias (FILE *stream, const char *match, struct aliasent *result,
50f89d
 			{
50f89d
 			  while (! feof_unlocked (listfile))
50f89d
 			    {
50f89d
+			      if (room_left < 2)
50f89d
+				{
50f89d
+				  free (old_line);
50f89d
+				  fclose (listfile);
50f89d
+				  goto no_more_room;
50f89d
+				}
50f89d
+
50f89d
 			      first_unused[room_left - 1] = '\xff';
50f89d
 			      line = fgets_unlocked (first_unused, room_left,
50f89d
 						     listfile);
50f89d
@@ -229,6 +236,7 @@ get_next_alias (FILE *stream, const char *match, struct aliasent *result,
50f89d
 			      if (first_unused[room_left - 1] != '\xff')
50f89d
 				{
50f89d
 				  free (old_line);
50f89d
+				  fclose (listfile);
50f89d
 				  goto no_more_room;
50f89d
 				}
50f89d
 
50f89d
@@ -256,6 +264,7 @@ get_next_alias (FILE *stream, const char *match, struct aliasent *result,
50f89d
 						       + __alignof__ (char *)))
50f89d
 					{
50f89d
 					  free (old_line);
50f89d
+					  fclose (listfile);
50f89d
 					  goto no_more_room;
50f89d
 					}
50f89d
 				      room_left -= ((first_unused - cp)
50f89d
diff --git a/nss/tst-nss-files-alias-leak.c b/nss/tst-nss-files-alias-leak.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..26d38e2dba1ddaf3
50f89d
--- /dev/null
50f89d
+++ b/nss/tst-nss-files-alias-leak.c
50f89d
@@ -0,0 +1,237 @@
50f89d
+/* Check for file descriptor leak in alias :include: processing (bug 23521).
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <aliases.h>
50f89d
+#include <array_length.h>
50f89d
+#include <dlfcn.h>
50f89d
+#include <errno.h>
50f89d
+#include <gnu/lib-names.h>
50f89d
+#include <nss.h>
50f89d
+#include <stdlib.h>
50f89d
+#include <string.h>
50f89d
+#include <support/check.h>
50f89d
+#include <support/namespace.h>
50f89d
+#include <support/support.h>
50f89d
+#include <support/temp_file.h>
50f89d
+#include <support/test-driver.h>
50f89d
+#include <support/xstdio.h>
50f89d
+#include <support/xunistd.h>
50f89d
+
50f89d
+static struct support_chroot *chroot_env;
50f89d
+
50f89d
+/* Number of the aliases for the "many" user.  This must be large
50f89d
+   enough to trigger reallocation for the pointer array, but result in
50f89d
+   answers below the maximum size tried in do_test.  */
50f89d
+enum { many_aliases = 30 };
50f89d
+
50f89d
+static void
50f89d
+prepare (int argc, char **argv)
50f89d
+{
50f89d
+  chroot_env = support_chroot_create
50f89d
+    ((struct support_chroot_configuration) { } );
50f89d
+
50f89d
+  char *path = xasprintf ("%s/etc/aliases", chroot_env->path_chroot);
50f89d
+  add_temp_file (path);
50f89d
+  support_write_file_string
50f89d
+    (path,
50f89d
+     "user1: :include:/etc/aliases.user1\n"
50f89d
+     "user2: :include:/etc/aliases.user2\n"
50f89d
+     "comment: comment1, :include:/etc/aliases.comment\n"
50f89d
+     "many: :include:/etc/aliases.many\n");
50f89d
+  free (path);
50f89d
+
50f89d
+  path = xasprintf ("%s/etc/aliases.user1", chroot_env->path_chroot);
50f89d
+  add_temp_file (path);
50f89d
+  support_write_file_string (path, "alias1\n");
50f89d
+  free (path);
50f89d
+
50f89d
+  path = xasprintf ("%s/etc/aliases.user2", chroot_env->path_chroot);
50f89d
+  add_temp_file (path);
50f89d
+  support_write_file_string (path, "alias1a, alias2\n");
50f89d
+  free (path);
50f89d
+
50f89d
+  path = xasprintf ("%s/etc/aliases.comment", chroot_env->path_chroot);
50f89d
+  add_temp_file (path);
50f89d
+  support_write_file_string
50f89d
+    (path,
50f89d
+     /* The line must be longer than the line with the :include:
50f89d
+        directive in /etc/aliases.  */
50f89d
+     "# Long line.  ##############################################\n"
50f89d
+     "comment2\n");
50f89d
+  free (path);
50f89d
+
50f89d
+  path = xasprintf ("%s/etc/aliases.many", chroot_env->path_chroot);
50f89d
+  add_temp_file (path);
50f89d
+  FILE *fp = xfopen (path, "w");
50f89d
+  for (int i = 0; i < many_aliases; ++i)
50f89d
+    fprintf (fp, "a%d\n", i);
50f89d
+  TEST_VERIFY_EXIT (! ferror (fp));
50f89d
+  xfclose (fp);
50f89d
+  free (path);
50f89d
+}
50f89d
+
50f89d
+/* The names of the users to test.  */
50f89d
+static const char *users[] = { "user1", "user2", "comment", "many" };
50f89d
+
50f89d
+static void
50f89d
+check_aliases (int id, const struct aliasent *e)
50f89d
+{
50f89d
+  TEST_VERIFY_EXIT (id >= 0 || id < array_length (users));
50f89d
+  const char *name = users[id];
50f89d
+  TEST_COMPARE_BLOB (e->alias_name, strlen (e->alias_name),
50f89d
+                     name, strlen (name));
50f89d
+
50f89d
+  switch (id)
50f89d
+    {
50f89d
+    case 0:
50f89d
+      TEST_COMPARE (e->alias_members_len, 1);
50f89d
+      TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
50f89d
+                         "alias1", strlen ("alias1"));
50f89d
+      break;
50f89d
+
50f89d
+    case 1:
50f89d
+      TEST_COMPARE (e->alias_members_len, 2);
50f89d
+      TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
50f89d
+                         "alias1a", strlen ("alias1a"));
50f89d
+      TEST_COMPARE_BLOB (e->alias_members[1], strlen (e->alias_members[1]),
50f89d
+                         "alias2", strlen ("alias2"));
50f89d
+      break;
50f89d
+
50f89d
+    case 2:
50f89d
+      TEST_COMPARE (e->alias_members_len, 2);
50f89d
+      TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
50f89d
+                         "comment1", strlen ("comment1"));
50f89d
+      TEST_COMPARE_BLOB (e->alias_members[1], strlen (e->alias_members[1]),
50f89d
+                         "comment2", strlen ("comment2"));
50f89d
+      break;
50f89d
+
50f89d
+    case 3:
50f89d
+      TEST_COMPARE (e->alias_members_len, many_aliases);
50f89d
+      for (int i = 0; i < e->alias_members_len; ++i)
50f89d
+        {
50f89d
+          char alias[30];
50f89d
+          int len = snprintf (alias, sizeof (alias), "a%d", i);
50f89d
+          TEST_VERIFY_EXIT (len > 0);
50f89d
+          TEST_COMPARE_BLOB (e->alias_members[i], strlen (e->alias_members[i]),
50f89d
+                             alias, len);
50f89d
+        }
50f89d
+      break;
50f89d
+    }
50f89d
+}
50f89d
+
50f89d
+static int
50f89d
+do_test (void)
50f89d
+{
50f89d
+  /* Make sure we don't try to load the module in the chroot.  */
50f89d
+  if (dlopen (LIBNSS_FILES_SO, RTLD_NOW) == NULL)
50f89d
+    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
50f89d
+
50f89d
+  /* Some of these descriptors will become unavailable if there is a
50f89d
+     file descriptor leak.  10 is chosen somewhat arbitrarily.  The
50f89d
+     array must be longer than the number of files opened by nss_files
50f89d
+     at the same time (currently that number is 2).  */
50f89d
+  int next_descriptors[10];
50f89d
+  for (size_t i = 0; i < array_length (next_descriptors); ++i)
50f89d
+    {
50f89d
+      next_descriptors[i] = dup (0);
50f89d
+      TEST_VERIFY_EXIT (next_descriptors[i] > 0);
50f89d
+    }
50f89d
+  for (size_t i = 0; i < array_length (next_descriptors); ++i)
50f89d
+    xclose (next_descriptors[i]);
50f89d
+
50f89d
+  support_become_root ();
50f89d
+  if (!support_can_chroot ())
50f89d
+    return EXIT_UNSUPPORTED;
50f89d
+
50f89d
+  __nss_configure_lookup ("aliases", "files");
50f89d
+
50f89d
+  xchroot (chroot_env->path_chroot);
50f89d
+
50f89d
+  /* Attempt various buffer sizes.  If the operation succeeds, we
50f89d
+     expect correct data.  */
50f89d
+  for (int id = 0; id < array_length (users); ++id)
50f89d
+    {
50f89d
+      bool found = false;
50f89d
+      for (size_t size = 1; size <= 1000; ++size)
50f89d
+        {
50f89d
+          void *buffer = malloc (size);
50f89d
+          struct aliasent result;
50f89d
+          struct aliasent *res;
50f89d
+          errno = EINVAL;
50f89d
+          int ret = getaliasbyname_r (users[id], &result, buffer, size, &res;;
50f89d
+          if (ret == 0)
50f89d
+            {
50f89d
+              if (res != NULL)
50f89d
+                {
50f89d
+                  found = true;
50f89d
+                  check_aliases (id, res);
50f89d
+                }
50f89d
+              else
50f89d
+                {
50f89d
+                  support_record_failure ();
50f89d
+                  printf ("error: failed lookup for user \"%s\", size %zu\n",
50f89d
+                          users[id], size);
50f89d
+                }
50f89d
+            }
50f89d
+          else if (ret != ERANGE)
50f89d
+            {
50f89d
+              support_record_failure ();
50f89d
+              printf ("error: invalid return code %d (user \%s\", size %zu)\n",
50f89d
+                      ret, users[id], size);
50f89d
+            }
50f89d
+          free (buffer);
50f89d
+
50f89d
+          /* Make sure that we did not have a file descriptor leak.  */
50f89d
+          for (size_t i = 0; i < array_length (next_descriptors); ++i)
50f89d
+            {
50f89d
+              int new_fd = dup (0);
50f89d
+              if (new_fd != next_descriptors[i])
50f89d
+                {
50f89d
+                  support_record_failure ();
50f89d
+                  printf ("error: descriptor %d at index %zu leaked"
50f89d
+                          " (user \"%s\", size %zu)\n",
50f89d
+                          next_descriptors[i], i, users[id], size);
50f89d
+
50f89d
+                  /* Close unexpected descriptor, the leak probing
50f89d
+                     descriptors, and the leaked descriptor
50f89d
+                     next_descriptors[i].  */
50f89d
+                  xclose (new_fd);
50f89d
+                  for (size_t j = 0; j <= i; ++j)
50f89d
+                    xclose (next_descriptors[j]);
50f89d
+                  goto next_size;
50f89d
+                }
50f89d
+            }
50f89d
+          for (size_t i = 0; i < array_length (next_descriptors); ++i)
50f89d
+            xclose (next_descriptors[i]);
50f89d
+
50f89d
+        next_size:
50f89d
+          ;
50f89d
+        }
50f89d
+      if (!found)
50f89d
+        {
50f89d
+          support_record_failure ();
50f89d
+          printf ("error: user %s not found\n", users[id]);
50f89d
+        }
50f89d
+    }
50f89d
+
50f89d
+  support_chroot_free (chroot_env);
50f89d
+  return 0;
50f89d
+}
50f89d
+
50f89d
+#define PREPARE prepare
50f89d
+#include <support/test-driver.c>