|
|
1feee8 |
commit 06afa5e09fbd984ed45ae6fc6ca050d544aba780
|
|
|
1feee8 |
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
|
|
|
1feee8 |
Date: Wed Aug 25 11:17:06 2021 -0300
|
|
|
1feee8 |
|
|
|
1feee8 |
io: Fix ftw internal realloc buffer (BZ #28126)
|
|
|
1feee8 |
|
|
|
1feee8 |
The 106ff08526d3ca did not take in consideration the buffer might be
|
|
|
1feee8 |
reallocated if the total path is larger than PATH_MAX. The realloc
|
|
|
1feee8 |
uses 'dirbuf', where 'dirstreams' is the allocated buffer.
|
|
|
1feee8 |
|
|
|
1feee8 |
Checked on x86_64-linux-gnu.
|
|
|
1feee8 |
|
|
|
1feee8 |
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
|
|
|
1feee8 |
(cherry picked from commit 1836bb2ebf62bd9a3588f2ed2d851c8ae810097a)
|
|
|
1feee8 |
|
|
|
1feee8 |
diff --git a/io/Makefile b/io/Makefile
|
|
|
1feee8 |
index 01968b81042e01e4..5284a1282dd07e3d 100644
|
|
|
1feee8 |
--- a/io/Makefile
|
|
|
1feee8 |
+++ b/io/Makefile
|
|
|
1feee8 |
@@ -79,6 +79,7 @@ tests := test-utime test-stat test-stat2 test-lfs tst-getcwd \
|
|
|
1feee8 |
tst-futimens \
|
|
|
1feee8 |
tst-utimensat \
|
|
|
1feee8 |
tst-closefrom \
|
|
|
1feee8 |
+ tst-ftw-bz28126
|
|
|
1feee8 |
|
|
|
1feee8 |
tests-time64 := \
|
|
|
1feee8 |
tst-fcntl-time64 \
|
|
|
1feee8 |
diff --git a/io/ftw.c b/io/ftw.c
|
|
|
1feee8 |
index ce1c6a14a306152a..cf08d9f101657df0 100644
|
|
|
1feee8 |
--- a/io/ftw.c
|
|
|
1feee8 |
+++ b/io/ftw.c
|
|
|
1feee8 |
@@ -204,6 +204,20 @@ struct ftw_data
|
|
|
1feee8 |
void *known_objects;
|
|
|
1feee8 |
};
|
|
|
1feee8 |
|
|
|
1feee8 |
+static bool
|
|
|
1feee8 |
+ftw_allocate (struct ftw_data *data, size_t newsize)
|
|
|
1feee8 |
+{
|
|
|
1feee8 |
+ void *newp = realloc (data->dirstreams, data->maxdir
|
|
|
1feee8 |
+ * sizeof (struct dir_data *)
|
|
|
1feee8 |
+ + newsize);
|
|
|
1feee8 |
+ if (newp == NULL)
|
|
|
1feee8 |
+ return false;
|
|
|
1feee8 |
+ data->dirstreams = newp;
|
|
|
1feee8 |
+ data->dirbufsize = newsize;
|
|
|
1feee8 |
+ data->dirbuf = (char *) data->dirstreams
|
|
|
1feee8 |
+ + data->maxdir * sizeof (struct dir_data *);
|
|
|
1feee8 |
+ return true;
|
|
|
1feee8 |
+}
|
|
|
1feee8 |
|
|
|
1feee8 |
/* Internally we use the FTW_* constants used for `nftw'. When invoked
|
|
|
1feee8 |
as `ftw', map each flag to the subset of values used by `ftw'. */
|
|
|
1feee8 |
@@ -389,17 +403,9 @@ process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
|
|
|
1feee8 |
return 0;
|
|
|
1feee8 |
|
|
|
1feee8 |
new_buflen = data->ftw.base + namlen + 2;
|
|
|
1feee8 |
- if (data->dirbufsize < new_buflen)
|
|
|
1feee8 |
- {
|
|
|
1feee8 |
- /* Enlarge the buffer. */
|
|
|
1feee8 |
- char *newp;
|
|
|
1feee8 |
-
|
|
|
1feee8 |
- data->dirbufsize = 2 * new_buflen;
|
|
|
1feee8 |
- newp = (char *) realloc (data->dirbuf, data->dirbufsize);
|
|
|
1feee8 |
- if (newp == NULL)
|
|
|
1feee8 |
- return -1;
|
|
|
1feee8 |
- data->dirbuf = newp;
|
|
|
1feee8 |
- }
|
|
|
1feee8 |
+ if (data->dirbufsize < new_buflen
|
|
|
1feee8 |
+ && !ftw_allocate (data, 2 * new_buflen))
|
|
|
1feee8 |
+ return -1;
|
|
|
1feee8 |
|
|
|
1feee8 |
*((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0';
|
|
|
1feee8 |
|
|
|
1feee8 |
@@ -629,7 +635,7 @@ __attribute ((noinline))
|
|
|
1feee8 |
ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
|
|
|
1feee8 |
int flags)
|
|
|
1feee8 |
{
|
|
|
1feee8 |
- struct ftw_data data;
|
|
|
1feee8 |
+ struct ftw_data data = { .dirstreams = NULL };
|
|
|
1feee8 |
struct STRUCT_STAT st;
|
|
|
1feee8 |
int result = 0;
|
|
|
1feee8 |
int save_err;
|
|
|
1feee8 |
@@ -647,16 +653,9 @@ ftw_startup (const char *dir, int is_nftw, void *func, int descriptors,
|
|
|
1feee8 |
data.maxdir = descriptors < 1 ? 1 : descriptors;
|
|
|
1feee8 |
data.actdir = 0;
|
|
|
1feee8 |
/* PATH_MAX is always defined when we get here. */
|
|
|
1feee8 |
- data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX);
|
|
|
1feee8 |
- data.dirstreams = malloc (data.maxdir * sizeof (struct dir_data *)
|
|
|
1feee8 |
- + data.dirbufsize);
|
|
|
1feee8 |
- if (data.dirstreams == NULL)
|
|
|
1feee8 |
+ if (!ftw_allocate (&data, MAX (2 * strlen (dir), PATH_MAX)))
|
|
|
1feee8 |
return -1;
|
|
|
1feee8 |
-
|
|
|
1feee8 |
memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *));
|
|
|
1feee8 |
-
|
|
|
1feee8 |
- data.dirbuf = (char *) data.dirstreams
|
|
|
1feee8 |
- + data.maxdir * sizeof (struct dir_data *);
|
|
|
1feee8 |
cp = __stpcpy (data.dirbuf, dir);
|
|
|
1feee8 |
/* Strip trailing slashes. */
|
|
|
1feee8 |
while (cp > data.dirbuf + 1 && cp[-1] == '/')
|
|
|
1feee8 |
diff --git a/io/tst-ftw-bz28126.c b/io/tst-ftw-bz28126.c
|
|
|
1feee8 |
new file mode 100644
|
|
|
1feee8 |
index 0000000000000000..94044ab9d1d0275b
|
|
|
1feee8 |
--- /dev/null
|
|
|
1feee8 |
+++ b/io/tst-ftw-bz28126.c
|
|
|
1feee8 |
@@ -0,0 +1,97 @@
|
|
|
1feee8 |
+/* Check if internal buffer reallocation work for large paths (BZ #28126)
|
|
|
1feee8 |
+ Copyright (C) 2021 Free Software Foundation, Inc.
|
|
|
1feee8 |
+ This file is part of the GNU C Library.
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
1feee8 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
1feee8 |
+ License as published by the Free Software Foundation; either
|
|
|
1feee8 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
1feee8 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
1feee8 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
1feee8 |
+ Lesser General Public License for more details.
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
1feee8 |
+ License along with the GNU C Library; if not, see
|
|
|
1feee8 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+#include <errno.h>
|
|
|
1feee8 |
+#include <ftw.h>
|
|
|
1feee8 |
+#include <limits.h>
|
|
|
1feee8 |
+#include <string.h>
|
|
|
1feee8 |
+#include <stdlib.h>
|
|
|
1feee8 |
+#include <support/check.h>
|
|
|
1feee8 |
+#include <support/support.h>
|
|
|
1feee8 |
+#include <support/temp_file.h>
|
|
|
1feee8 |
+#include <support/xunistd.h>
|
|
|
1feee8 |
+#include <stdio.h>
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+static int
|
|
|
1feee8 |
+my_func (const char *file, const struct stat *sb, int flag)
|
|
|
1feee8 |
+{
|
|
|
1feee8 |
+ return 0;
|
|
|
1feee8 |
+}
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+static const char folder[NAME_MAX] = { [0 ... 253] = 'a', [254] = '\0' };
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+#define NSUBFOLDERS 16
|
|
|
1feee8 |
+static int nsubfolders;
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+static void
|
|
|
1feee8 |
+do_cleanup (void)
|
|
|
1feee8 |
+{
|
|
|
1feee8 |
+ xchdir ("..");
|
|
|
1feee8 |
+ for (int i = 0; i < nsubfolders; i++)
|
|
|
1feee8 |
+ {
|
|
|
1feee8 |
+ remove (folder);
|
|
|
1feee8 |
+ xchdir ("..");
|
|
|
1feee8 |
+ }
|
|
|
1feee8 |
+ remove (folder);
|
|
|
1feee8 |
+}
|
|
|
1feee8 |
+#define CLEANUP_HANDLER do_cleanup
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+static void
|
|
|
1feee8 |
+check_mkdir (const char *path)
|
|
|
1feee8 |
+{
|
|
|
1feee8 |
+ int r = mkdir (path, 0777);
|
|
|
1feee8 |
+ /* Some filesystem such as overlayfs does not support larger path required
|
|
|
1feee8 |
+ to trigger the internal buffer reallocation. */
|
|
|
1feee8 |
+ if (r != 0)
|
|
|
1feee8 |
+ {
|
|
|
1feee8 |
+ if (errno == ENAMETOOLONG)
|
|
|
1feee8 |
+ FAIL_UNSUPPORTED ("the filesystem does not support the required"
|
|
|
1feee8 |
+ "large path");
|
|
|
1feee8 |
+ else
|
|
|
1feee8 |
+ FAIL_EXIT1 ("mkdir (\"%s\", 0%o): %m", folder, 0777);
|
|
|
1feee8 |
+ }
|
|
|
1feee8 |
+}
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+static int
|
|
|
1feee8 |
+do_test (void)
|
|
|
1feee8 |
+{
|
|
|
1feee8 |
+ char *tempdir = support_create_temp_directory ("tst-bz28126");
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ /* Create path with various subfolders to force an internal buffer
|
|
|
1feee8 |
+ reallocation within ntfw. */
|
|
|
1feee8 |
+ char *path = xasprintf ("%s/%s", tempdir, folder);
|
|
|
1feee8 |
+ check_mkdir (path);
|
|
|
1feee8 |
+ xchdir (path);
|
|
|
1feee8 |
+ free (path);
|
|
|
1feee8 |
+ for (int i = 0; i < NSUBFOLDERS - 1; i++)
|
|
|
1feee8 |
+ {
|
|
|
1feee8 |
+ check_mkdir (folder);
|
|
|
1feee8 |
+ xchdir (folder);
|
|
|
1feee8 |
+ nsubfolders++;
|
|
|
1feee8 |
+ }
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ TEST_COMPARE (ftw (tempdir, my_func, 20), 0);
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ free (tempdir);
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ do_cleanup ();
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+ return 0;
|
|
|
1feee8 |
+}
|
|
|
1feee8 |
+
|
|
|
1feee8 |
+#include <support/test-driver.c>
|