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