446cf2
From 2add4235ef674988948155f9a8f60a8c7b09bcff Mon Sep 17 00:00:00 2001
446cf2
From: Florian Weimer <fweimer@redhat.com>
446cf2
Date: Thu, 16 Jul 2020 17:31:20 +0200
446cf2
Subject: [PATCH 08/11] gshadow: Implement fgetsgent_r using __nss_fgetent_r
446cf2
 (bug 20338)
446cf2
446cf2
Tested-by: Carlos O'Donell <carlos@redhat.com>
446cf2
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
---
446cf2
 gshadow/Makefile          |   2 +-
446cf2
 gshadow/fgetsgent_r.c     |  41 ++--------
446cf2
 gshadow/tst-fgetsgent_r.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++
446cf2
 3 files changed, 198 insertions(+), 36 deletions(-)
446cf2
 create mode 100644 gshadow/tst-fgetsgent_r.c
446cf2
446cf2
diff -rupN a/gshadow/Makefile b/gshadow/Makefile
446cf2
--- a/gshadow/Makefile	2018-08-01 01:10:47.000000000 -0400
446cf2
+++ b/gshadow/Makefile	2020-09-14 18:00:57.167145887 -0400
446cf2
@@ -26,7 +26,7 @@ headers		= gshadow.h
446cf2
 routines	= getsgent getsgnam sgetsgent fgetsgent putsgent \
446cf2
 		  getsgent_r getsgnam_r sgetsgent_r fgetsgent_r
446cf2
 
446cf2
-tests = tst-gshadow tst-putsgent
446cf2
+tests = tst-gshadow tst-putsgent tst-fgetsgent_r
446cf2
 
446cf2
 CFLAGS-getsgent_r.c += -fexceptions
446cf2
 CFLAGS-getsgent.c += -fexceptions
446cf2
diff -rupN a/gshadow/fgetsgent_r.c b/gshadow/fgetsgent_r.c
446cf2
--- a/gshadow/fgetsgent_r.c	2018-08-01 01:10:47.000000000 -0400
446cf2
+++ b/gshadow/fgetsgent_r.c	2020-09-14 18:45:59.189353065 -0400
446cf2
@@ -36,40 +36,11 @@ int
446cf2
 __fgetsgent_r (FILE *stream, struct sgrp *resbuf, char *buffer, size_t buflen,
446cf2
 	       struct sgrp **result)
446cf2
 {
446cf2
-  char *p;
446cf2
-
446cf2
-  _IO_flockfile (stream);
446cf2
-  do
446cf2
-    {
446cf2
-      buffer[buflen - 1] = '\xff';
446cf2
-      p = fgets_unlocked (buffer, buflen, stream);
446cf2
-      if (p == NULL && feof_unlocked (stream))
446cf2
-	{
446cf2
-	  _IO_funlockfile (stream);
446cf2
-	  *result = NULL;
446cf2
-	  __set_errno (ENOENT);
446cf2
-	  return errno;
446cf2
-	}
446cf2
-      if (p == NULL || buffer[buflen - 1] != '\xff')
446cf2
-	{
446cf2
-	  _IO_funlockfile (stream);
446cf2
-	  *result = NULL;
446cf2
-	  __set_errno (ERANGE);
446cf2
-	  return errno;
446cf2
-	}
446cf2
-
446cf2
-      /* Skip leading blanks.  */
446cf2
-      while (isspace (*p))
446cf2
-	++p;
446cf2
-    } while (*p == '\0' || *p == '#' ||	/* Ignore empty and comment lines.  */
446cf2
-	     /* Parse the line.  If it is invalid, loop to
446cf2
-		get the next line of the file to parse.  */
446cf2
-	     ! parse_line (buffer, (void *) resbuf, (void *) buffer, buflen,
446cf2
-			   &errno));
446cf2
-
446cf2
-  _IO_funlockfile (stream);
446cf2
-
446cf2
-  *result = resbuf;
446cf2
-  return 0;
446cf2
+  int ret = __nss_fgetent_r (stream, resbuf, buffer, buflen, parse_line);
446cf2
+  if (ret == 0)
446cf2
+    *result = resbuf;
446cf2
+  else
446cf2
+    *result = NULL;
446cf2
+  return ret;
446cf2
 }
446cf2
 weak_alias (__fgetsgent_r, fgetsgent_r)
446cf2
diff -rupN a/gshadow/tst-fgetsgent_r.c b/gshadow/tst-fgetsgent_r.c
446cf2
--- a/gshadow/tst-fgetsgent_r.c	1969-12-31 19:00:00.000000000 -0500
446cf2
+++ b/gshadow/tst-fgetsgent_r.c	2020-09-14 18:00:57.174146151 -0400
446cf2
@@ -0,0 +1,191 @@
446cf2
+/* Test for fgetsgent_r and buffer sizes.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <array_length.h>
446cf2
+#include <errno.h>
446cf2
+#include <gshadow.h>
446cf2
+#include <stdbool.h>
446cf2
+#include <stdlib.h>
446cf2
+#include <support/check.h>
446cf2
+#include <support/support.h>
446cf2
+#include <support/temp_file.h>
446cf2
+#include <support/xmemstream.h>
446cf2
+#include <support/xstdio.h>
446cf2
+
446cf2
+/* Turn a parsed struct back into a line string.  The returned string
446cf2
+   should be freed.  */
446cf2
+static char *
446cf2
+format_ent (const struct sgrp *e)
446cf2
+{
446cf2
+  struct xmemstream stream;
446cf2
+  xopen_memstream (&stream);
446cf2
+  TEST_COMPARE (putsgent (e, stream.out), 0);
446cf2
+  xfclose_memstream (&stream);
446cf2
+  return stream.buffer;
446cf2
+}
446cf2
+
446cf2
+/* An entry in the input file along with the expected output.  */
446cf2
+struct input
446cf2
+{
446cf2
+  const char *line;		/* Line in the file.  */
446cf2
+  const char *expected;		/* Expected output.  NULL if skipped.  */
446cf2
+};
446cf2
+
446cf2
+const struct input inputs[] =
446cf2
+  {
446cf2
+   /* Regular entries.  */
446cf2
+   { "g1:x1::\n", "g1:x1::\n" },
446cf2
+   { "g2:x2:a1:\n", "g2:x2:a1:\n" },
446cf2
+   { "g3:x3:a2:u1\n", "g3:x3:a2:u1\n" },
446cf2
+   { "g4:x4:a3,a4:u2,u3,u4\n", "g4:x4:a3,a4:u2,u3,u4\n" },
446cf2
+
446cf2
+   /* Comments and empty lines.  */
446cf2
+   { "\n", NULL },
446cf2
+   { " \n", NULL },
446cf2
+   { "\t\n", NULL },
446cf2
+   { "#g:x::\n", NULL },
446cf2
+   { " #g:x::\n", NULL },
446cf2
+   { "\t#g:x::\n", NULL },
446cf2
+   { " \t#g:x::\n", NULL },
446cf2
+
446cf2
+   /* Marker for synchronization.  */
446cf2
+   { "g5:x5::\n", "g5:x5::\n" },
446cf2
+
446cf2
+   /* Leading whitespace.  */
446cf2
+   { " g6:x6::\n", "g6:x6::\n" },
446cf2
+   { "\tg7:x7::\n", "g7:x7::\n" },
446cf2
+
446cf2
+   /* This is expected to trigger buffer exhaustion during parsing
446cf2
+      (bug 20338).  */
446cf2
+   {
446cf2
+    "g8:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:u5,u6,u7,u8,u9:\n",
446cf2
+    "g8:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:u5,u6,u7,u8,u9:\n",
446cf2
+   },
446cf2
+   {
446cf2
+    "g9:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::a5,a6,a7,a8,a9,a10\n",
446cf2
+    "g9:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::a5,a6,a7,a8,a9,a10\n",
446cf2
+   },
446cf2
+  };
446cf2
+
446cf2
+/* Writes the test data to a temporary file and returns its name.  The
446cf2
+   returned pointer should be freed.  */
446cf2
+static char *
446cf2
+create_test_file (void)
446cf2
+{
446cf2
+  char *path;
446cf2
+  int fd = create_temp_file ("tst-fgetsgent_r-", &path);
446cf2
+  FILE *fp = fdopen (fd, "w");
446cf2
+  TEST_VERIFY_EXIT (fp != NULL);
446cf2
+
446cf2
+  for (size_t i = 0; i < array_length (inputs); ++i)
446cf2
+    fputs (inputs[i].line, fp);
446cf2
+
446cf2
+  xfclose (fp);
446cf2
+  return path;
446cf2
+}
446cf2
+
446cf2
+/* Read the test file with the indicated start buffer size.  Return
446cf2
+   true if the buffer size had to be increased during reading.  */
446cf2
+static bool
446cf2
+run_test (const char *path, size_t buffer_size)
446cf2
+{
446cf2
+  bool resized = false;
446cf2
+  FILE *fp = xfopen (path, "r");
446cf2
+
446cf2
+  /* This avoids repeated lseek system calls (bug 26257).  */
446cf2
+  TEST_COMPARE (fseeko64 (fp, 0, SEEK_SET), 0);
446cf2
+
446cf2
+  size_t i = 0;
446cf2
+  while (true)
446cf2
+    {
446cf2
+      /* Skip over unused expected entries.  */
446cf2
+      while (i < array_length (inputs) && inputs[i].expected == NULL)
446cf2
+	++i;
446cf2
+
446cf2
+      /* Store the data on the heap, to help valgrind to detect
446cf2
+	 invalid accesses.  */
446cf2
+      struct sgrp *result_storage = xmalloc (sizeof (*result_storage));
446cf2
+      char *buffer = xmalloc (buffer_size);
446cf2
+      struct sgrp **result_pointer_storage
446cf2
+	= xmalloc (sizeof (*result_pointer_storage));
446cf2
+
446cf2
+      int ret = fgetsgent_r (fp, result_storage, buffer, buffer_size,
446cf2
+			     result_pointer_storage);
446cf2
+      if (ret == 0)
446cf2
+	{
446cf2
+	  TEST_VERIFY (*result_pointer_storage != NULL);
446cf2
+	  TEST_VERIFY (i < array_length (inputs));
446cf2
+	  if (*result_pointer_storage != NULL
446cf2
+	      && i < array_length (inputs))
446cf2
+	    {
446cf2
+	      char * actual = format_ent (*result_pointer_storage);
446cf2
+	      TEST_COMPARE_STRING (inputs[i].expected, actual);
446cf2
+	      free (actual);
446cf2
+	      ++i;
446cf2
+	    }
446cf2
+	  else
446cf2
+	    break;
446cf2
+	}
446cf2
+      else
446cf2
+	{
446cf2
+	  TEST_VERIFY (*result_pointer_storage == NULL);
446cf2
+	  TEST_COMPARE (ret, errno);
446cf2
+
446cf2
+	  if (ret == ENOENT)
446cf2
+	    {
446cf2
+	      TEST_COMPARE (i, array_length (inputs));
446cf2
+	      free (result_pointer_storage);
446cf2
+	      free (buffer);
446cf2
+	      free (result_storage);
446cf2
+	      break;
446cf2
+	    }
446cf2
+	  else if (ret == ERANGE)
446cf2
+	    {
446cf2
+	      resized = true;
446cf2
+	      ++buffer_size;
446cf2
+	    }
446cf2
+	  else
446cf2
+	    FAIL_EXIT1 ("read failure: %m");
446cf2
+	}
446cf2
+
446cf2
+      free (result_pointer_storage);
446cf2
+      free (buffer);
446cf2
+      free (result_storage);
446cf2
+    }
446cf2
+
446cf2
+  return resized;
446cf2
+}
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  char *path = create_test_file ();
446cf2
+
446cf2
+  for (size_t buffer_size = 3; ; ++buffer_size)
446cf2
+    {
446cf2
+      bool resized = run_test (path, buffer_size);
446cf2
+      if (!resized)
446cf2
+	break;
446cf2
+    }
446cf2
+
446cf2
+  free (path);
446cf2
+
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>