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