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